Skip to content
Advertisement

Watermark fading with transparent background

Have some script that display images on page with png watermark, but when i want to fade watermark png image to 50% there is white background display (but watermark fading to 50%) and when i want to change the size of watermark image the watermark png just gone and not displayed. Please help me to solve my problemwith fading and size changing. Ертл you to all.

<?php                                                   

/* Image source */
$src = strtolower($_GET['src']);

/* If no image src redirect to start page */
if(isset($src) && !empty($src) && $src !=0 && $src !=1) {

    header('content-type: image/jpeg');
    $path = pathinfo($src);
    $watermark = imagecreatefrompng('karantino.png');
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
    $image = imagecreatetruecolor($watermark_width, $watermark_height);

    if ($path["extension"]=="png") {

        $image = "no_img.jpg";

    }else if ($path["extension"] == "jpg" || $path["extension"] == "jpeg"){

        $image = imagecreatefromjpeg($src);
        $size = getimagesize($src);
        $dest_x = $size[0] - $watermark_width-10;
        $dest_y = $size[1] - $watermark_height-10;
        $alpha_channel = imagecolorallocatealpha($image, 255, 255, 255, 127);
        imagecolortransparent($image, $alpha_channel);
        imagefill($image, 0, 0, $alpha_channel);
        imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
        imagesavealpha($image);
        imagejpeg($image);
        imagedestroy($image);
        imagedestroy($watermark);
    }   

} else{

    header("HTTP/1.1 301 Moved Permanently");
    header ("Location: /");
}

/*

to use this code, just create php file with random name (i use wt.php) and put code like this in your HTML page <img src="wt.php?src=your_jpg_file.jpg">

*/

?>

Advertisement

Answer

I find a new working suggestion for me:

    <?php

    header("Content-type:image/jpeg");
    $src = $_GET["src"];


    /* If no image src redirect to start page, else do watermark */
    if(isset($src) && !empty($src) && $src!=0 && preg_match("/.(jpg|jpeg|png)$/", $src)){

        $path = pathinfo($src);

        if ($path["extension"] == "png") {

            $stamp = imagecreatefrompng("../../favicon.png");
            $im = imagecreatefrompng($src);

            $marge_right = 10;
            $marge_bottom = 10;
            $sx = imagesx($stamp);
            $sy = imagesy($stamp);

            imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right,
            imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp),
            imagesy($stamp));

            imagepng($im);
            imagedestroy($im);
        }

        else if ($path["extension"] == "jpg" || $path["extension"] == "jpeg"){

            $stamp = imagecreatefrompng("../../favicon.png");
            $im = imagecreatefromjpeg($src);

            $marge_right = 10;
            $marge_bottom = 10;
            $sx = imagesx($stamp);
            $sy = imagesy($stamp);

            imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right,
            imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp),
            imagesy($stamp));

            imagepng($im);
            imagedestroy($im);
        }

        else{

            /* If unknow format */
            $im = imagecreatefrompng("../important/no-photo.png");
            imagepng($im);
            imagedestroy($im);
        }
    }

    else{

        /* Redirect to start page if no string */
        header("HTTP/1.1 301 Moved Permanently");
        header ("Location: /");
    }

    /*

    to use this code, just create php file with random name and put code like this in your HTML page <img src="wt.php?src=your_jpg_file.jpg">

    */
?>

But still have a problem. When i use code like this <img src="wt.php?src=/img/my_img/your_jpg_file.jpg"> instead of this <img src="wt.php?src=your_jpg_file.jpg"> the script didn’t display any image.

Any ideas?

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement