简体   繁体   English

在PHP的帮助下将水印添加到png图像

[英]adding watermark to png images with the help of php

I want to watermark png images with a png image file. 我想用png图像文件给png图像加水印。 The following code does work with jpeg files, but png files. 以下代码适用于jpeg文件,但适用于png文件。 I have tried to change the code a bit (following) without luck. 我尝试过一点(如下)更改代码,但是没有运气。

i.e. $image=imagecreatefromjpeg($original_image); into $image=imagecreatefrompng($original_image);

Can you give me hints where the problem lies? 你能给我提示问题出在哪里吗?

Here is my php code: 这是我的PHP代码:

<?php

function watermark($original_image,$original_watermark,$destination="")
{
    /*
        create the image from out original image
    */
    $image=imagecreatefromjpeg($original_image);
    /*
        get the image size for watermark resize if neccessary
    */
    list($imagewidth,$imageheight)=getimagesize($original_image);

    /*
        create the watermark
    */
    $watermark  =   imagecreatefrompng($original_watermark);
    /*
        determine the watermark width and height
    */
    list($watermarkwidth,$watermarkheight)=getimagesize($original_watermark);

    /*
        if the watermark is bigger than the original image, we simply resize it
    */
    if($watermarkwidth>$imagewidth || $watermarkheight>$imageheight)
    {
        /*
            some simple resize math
        */
        $water_resize_factor = $imagewidth / $watermarkwidth;
        $new_watermarkwidth  = $watermarkwidth * $water_resize_factor;
        $new_watermarkheight = $watermarkheight * $water_resize_factor;
        /*
            the new watermark creation takes place starting from here
        */
        $new_watermark = imagecreatetruecolor($new_watermarkwidth , $new_watermarkheight);
        /*
            imagealphablending is important in order to keep
            our png image (the watewrmark) transparent
        */
        imagealphablending($new_watermark , false);
        imagecopyresampled($new_watermark , $watermark, 0, 0, 0, 0,
                                            $new_watermarkwidth,$new_watermarkheight,
                                            $watermarkwidth, $watermarkheight);
        /*
            assign the new values to the old variables
        */
        $watermarkwidth  = $new_watermarkwidth;
        $watermarkheight = $new_watermarkheight;
        $watermark       = $new_watermark;
    }
    /*
        we establish the position of the watermark over the image
    */
    $startwidth     =   ($imagewidth    -   $watermarkwidth)  / 2;
    $startheight    =   ($imageheight   -   $watermarkheight) / 2;

    imagecopy($image, $watermark, $startwidth, $startheight, 0, 0,
                $watermarkwidth, $watermarkheight);
    /*
        if we have a destination image, we save it on the server...
    */
    if(!empty($destination))
        imagejpeg($image,$destination);
    /*
        ... else we output the image
    */
    else
        imagejpeg($image);
}

?>

Here's what I use to watermark my images. 这是我用来给图像加水印的内容。 Make sure the $Destination contains ".png" at the end as the image creator creates a png image. 在图像创建者创建png图像时,请确保$ Destination在结尾处包含“ .png”。

Example: 例:

$Source = "http://www.personal.psu.edu/jyy5075/plant4.jpg"

$Destination = "/images/watermarked1.png"

$WatermarkText = "Hurray!"

<?php
function CreateWatermark($Source, $Destination, $WatermarkText)
{   
    // Load the stamp and the photo to apply the watermark to
    $im = CreateImage($Source);

    if ($im != NULL)
    {           
        // First we create our stamp image manually from GD
        $stamp = imagecreatetruecolor(100, 30);
        imagefilledrectangle($stamp, 0, 0, 100, 50, 0x000000);
        imagestring($stamp, 4, 5, 5, $WatermarkText, 0xFFFFFF);

        // Set the margins for the stamp and get the height/width of the stamp image
        $marge_right = 100;
        $marge_bottom = 100;
        $sx = imagesx($stamp);
        $sy = imagesy($stamp);

        // Merge the stamp onto our photo with an opacity of 50%
        imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);

        // Save the image to file and free memory
        imagepng($im, $Destination);
        imagedestroy($im);
    }
}

function CreateImage($Source)
{
    $types= array('jpg', 'jpeg', 'png', 'gif'); //store all the image extension types in array
    $im = NULL;
    $ext = pathinfo($URL, PATHINFO_EXTENSION);

    if(in_array($ext,$types)) //check image extension not in the array $type
    {
        switch ($ext)
        {
            case ('jpg'):
            {
                $im = imagecreatefromjpeg($URL);
                break;

            }
            case ('jpeg'):
            {
                $im = imagecreatefromjpeg($URL);
                break;
            }
            case ('png'):
            {
                $im = imagecreatefrompng($URL);
                break;
            }
            case ('gif'):
            {
                $im = imagecreatefromgif($URL);
                break;
            }
            default:
            {
                $im = NULL;
                break;
            }
        }               
    }   

    return $im; 
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM