Hoping someone out there can help. I recently updated to the latest version of PHP5 and now the program I was using for watermarking has stopped working.

If I change the program below to imagecreatefromgif() it works fine, but I lose transparency.

The following program slaps a png watermark on top of any image that is hotlinked (via .htaccess). It's been working great for years.

Is there another way to accomplish this that is compatible with this version of PHP5?

FreeBSD 6.2
Apache2.2.13
PHP Version 5.2.11
- GD Version bundled (2.0.34 compatible)

- PNG Support enabled

<?php
//Set placement of watermark.
//$horizontal can be left, right, or center. Default is right.
//$vertical can be top, bottom, or center. Default is bottom.
$horizontal = 'right';
$vertical = 'bottom';

header('content-type: image/jpeg');
$watermark = imagecreatefrompng('watermarkst24b.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$src = $_GET["src"];
$image = imagecreatefromjpeg($src);
$size = getimagesize($src);

switch ($horizontal) {
case 'left':
$dest_x = 5;
break;
case 'right':
$dest_x = $size[0] - $watermark_width - 5;
break;
case 'center':
$dest_x = ($size[0] / 2) - ($watermark_width / 2);
break;
default:
$dest_x = $size[0] - $watermark_width - 5;
}

switch ($vertical) {
case 'top':
$dest_y = 5;
break;
case 'bottom':
$dest_y = $size[1] - $watermark_height - 5;
break;
case 'center':
$dest_y = ($size[1] / 2) - ($watermark_height / 2);
break;
default:
$dest_y = $size[1] - $watermark_height - 5;
}

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>

This is from the Apache22 error.log

PHP Warning:  imagecreatefrompng() [<a href='function.imagecreatefrompng'>function.imagecreatefrompng</a>]: gd-png:  fatal libpng error: [00][00][00][00]: unknown critical chunk in /path/watermark.php on line 9
PHP Warning:  imagecreatefrompng() [<a href='function.imagecreatefrompng'>function.imagecreatefrompng</a>]: gd-png error: setjmp returns error condition in /path/watermark.php on line 
PHP Warning:  imagecreatefrompng() [<a href='function.imagecreatefrompng'>function.imagecreatefrompng</a>]: 'watermarkst24b.png' is not a valid PNG file in /path/watermark.php on line 9
PHP Warning:  imagesx(): supplied argument is not a valid Image resource in /path/watermark.php on line 10
PHP Warning:  imagesy(): supplied argument is not a valid Image resource in /path/watermark.php on line 11
PHP Warning:  imagecopy(): supplied argument is not a valid Image resource in /path/watermark.php on line 44
PHP Warning:  imagedestroy(): supplied argument is not a valid Image resource in /path/watermark.php on line 47

    It seems to be complaining about the watermark image you're using. Maybe you could try recreating that image and see if it makes any difference?

      On the surface it appears that way, but it's a png created in photoshop. Been using it forever...and of course tried other pngs. It just doesn't work. Was definately the PHP upgrade the broke it.

      Here is a somewhat related bug report
      http://bugs.php.net/bug.php?id=43508

      I'm not very good with PHP...is there some way I can test if the function works at all? What troubleshooting can I do? I'd prefer to not have to roll back to a previous build.

      It's very possible that it could be some configuration on my server, but I'm at a loss on where to look at this point. I've found quite a few similar complaints, but no resolutions.

        The reported fault isn't coming from PHP but from GD-Lib itself and PHP is just passing it on; in fact GD-Lib in turn is passing on an error from libpng, which is pretty much the definitive implementation of the format. Somewhere between your watermark image and libpng something is getting corrupted (the chunk identifier - 0,0,0,0 - is completely bogus).

        Like NogDog says, try regenerating the watermark image, compare the file with the old one; try another PNG file (with and without transparency)

        I just looked at my system (5.3.0, save version of GD-Lib) and the libpng version is 1.2.37; what's yours?

        The bug you linked to doesn't look very relevant; it's two years old and for PHP 5.2.5 (and revolves around the submitter not getting the difference between alpha channel and transparent colour) - what were you using before you upgraded?

          Current png-1.2.40.

          Ok, I loaded one of my VM backups and confirmed the watermarking was working with the following.

          FreeBSD 6.2
          Apache 2.2.11
          PHP Version 5.2.8
          - GD Version bundled (2.0.34 compatible)
          - PNG Support enabled
          png-1.2.34

          I do recall reading someone about someone having issues with png-1.2.38/39 and stating rolling back to 1.2.37 resolved their issue.

          I can try rolling back to 1.2.37. I'm not sure how to do that yet.

          Thanks for the support so far.

            rolled back to png 1.2.35. Still having the issue. Could this be something in my php.ini?

              Tried php.ini from working backup - issue not resolved
              Rolled back to php 5.2.8 - issue not resolved
              Created new very basic non-trasparent png's for testing. One 8bit and one 24bit. - issue not resolved

              What else is involved here?

                I'm going to mark this thread as resolved.

                I deinstalled PHP and all php extensions. Made sure all php extensions were gone and that everything was cleaned up. Then reinstalled php 5.2.8 and minimum extensions. Dynamic watermarking now works again.

                At this point I'm not sure if there is actually an issue with the later versions of php5. So I'll hold off on updating for a bit.

                  a year later

                  So here I am, a trip around the sun later. I ran into the same issue after updating a bunch of stuff on the server. Ironically I stumbled across my own thread while tyring to find an answer. I'm sure next year I'll have the same issue. :o

                  This time I figured out that the issue. I unloaded all the php extentions except for gd.so. Then I re-enabled the other extensions one at a time. Anything after gd.so caused the imagecreatfrompng() function to break. I then enabled all the extensions again and moved gd.so to the end. Everything works great now

                  Hopefully this update will help someone else out!

                    Sorry, I need to update this again. It seems I jumped the gun in posting the above solution. After restarting my browser, I found the watermarking was actually broken again. I went back to disabling the extensions one at a time and restarting apache and my browser each time. The problem goes away when I disable the extension=pdf.so extension. Anyway, this should be enough to get people on the right track. Good luck.

                      Write a Reply...