简体   繁体   中英

Resizing a dynamic watermark png in php

I'm attempting to automatically resize a watermark to cover 1/4 of an image. I have the watermark code working, but I can't get it to resize properly.

<?php

$image = $_GET['src'];
$path_parts = pathinfo($image);
$extension=strtolower($path_parts['extension']);
$size = (imagesx($image)/2);

$stamp = ImageCreateFromPNG("watermark.png"); 
ImageAlphaBlending($stamp,true); 
ImageSaveAlpha($stamp,true); 



$w = imagesx($stamp); 
$h = imagesy($stamp); 

if( $w==0 or $h==0 ) die("ERROR - zero image size"); 

$percent = $size / (($w>$h)?$w:$h); 
$nw = intval($w*$percent); 
$nh = intval($h*$percent); 

$stamp_resized = ImageCreateTrueColor($nw,$nh); 

ImageAlphaBlending($stamp_resized,false); 
ImageSaveAlpha($stamp_resized,true); 

if(!empty($transparent_color)) 
{ 
    $transparent_new = ImageColorAllocate($stamp_resized,$transparent_color['red'],$transparent_color['green'],$transparent_color['blue']); 
    $transparent_new_index = ImageColorTransparent($stamp_resized,$transparent_new); 
    ImageFill($stamp_resized, 0,0, $transparent_new_index); 
} 

if(ImageCopyResized($stamp_resized,$stamp, 0,0,0,0, $nw,$nh, $w,$h )) 
{ 
    ImageDestroy($stamp); 
    $stamp = $stamp_resized; 
} 




//Everything from here on works perfect
if(file_exists($image)){
    if ($extension == 'gif')$im = imagecreatefromgif($_GET['src']);
    if ($extension == 'jpg')  {

        $im = imagecreatefromjpeg($_GET['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, $sx, $sy);

    }
}
else{
$im = imagecreatefromgif('images/no_picture.gif');
}



// Output and free memory
header('Content-type: image/jpeg');
imagejpeg($im);  
imagedestroy($im);
?>

I checked the error log, and had these errors:

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagesx() expects parameter 1 to be resource, string given in {path removed}/watermark.php on line 15

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagecreatetruecolor(): Invalid image dimensions in {path removed}/watermark.php on line 32

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagealphablending() expects parameter 1 to be resource, boolean given in {path removed}/watermark.php on line 34

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagesavealpha() expects parameter 1 to be resource, boolean given in {path removed}/watermark.phpp on line 35

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagecopyresized() expects parameter 1 to be resource, boolean given in {path removed}/watermark.php on line 44

Turns out I was passing imagesx the path to the image ($image), not the image resource($im) itself. I redid the code a bit, so that the jpg is loaded before the image is resized.

That fixed all the cascading errors, and it works fine now. Moral of the story, check the error log.

It looks like

 $size = (imagesx($image)/2);

Might be your problem since imagesx requires an image resource generated by an image creation function and you gave it a src.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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