Problem with resize and watermark

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Problem with resize and watermark

Post by SBro »

I have the following script which resizes and watermarks images, it all works fine, however when I add the line to only resize if required, the outputted image is just a black image with the watermark, anyone know why this is, and how I get around it? I assume it's because my script needs to execute imagecopyresampled() for some reason for the watermarking to work, and thus when it is not executed, I get a black (watermarked) image, thanks.

Code: Select all

/**
	 * Create watermark on image, resizes and resamples also
	 * @return void
	 * @param array $images
	 * @param int $max
	 * @param int $quality
	 * @param string $watermark
	 **/
	function watermark($images, $max, $quality, $wmark) {
		foreach ($images as $img) {
			// get sizes
			list ($orig_width, $orig_height) = getimagesize($img);
			list ($width, $height) = $this->get_sizes($orig_width, $orig_height, $max);
			
			// original
			$image_p = imagecreatetruecolor($width, $height);
			$image = imagecreatefromjpeg($img);
			
			// resize
			if ($orig_width > $width) imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
			

			// watermark details
			$watermark = imagecreatefrompng($wmark); 
			$watermark_width = imagesx($watermark);  
			$watermark_height = imagesy($watermark);  
			
			// where to place watermark
			$dest_x = $width - $watermark_width - 5;  
			$dest_y = $height - $watermark_height - 5;  
			
			// create watermark
			if (!imagecopymerge($image_p, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100)) {
				// error
				error::log(ERR_MED, 'Resizing and watermarking of '.$img.' unable to be completed.', ERR_SHOW);  
				exit();
			}
			imagejpeg($image_p, $img, $quality);
			
			// destroy
			imagedestroy($image_p);  
			imagedestroy($watermark);
		}
	}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

What version of GD is your server running?
Does you watermark image have transparency?
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Is the camera you used to take pictures a cell phone camera? I've had problems with such images. Some phones add some data before 0xd9. The thubnailed images become all black. After opening an resaving in Gimp everything works fine.
Here is a Gimp 2 notice
Corrupt JPEG data: 222 extraneous bytes before marker 0xd9
Most frustrating thing is that, not too long ago I saw a script that had a workaround of this. But at that time it was of no importace to me so I didn't copy it. Now I can't remember where it was. Damn.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

No they weren't taken with a camera phone. The watermark does have transparency, however I just tried watermarking an image without transparency and the same problem occurs. The version of GD I am using is: 2.0.28 compatible
Post Reply