Page 1 of 1

Problem with resize and watermark

Posted: Mon Jul 25, 2005 12:25 am
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);
		}
	}

Posted: Mon Jul 25, 2005 3:24 am
by onion2k
What version of GD is your server running?
Does you watermark image have transparency?

Posted: Mon Jul 25, 2005 6:04 am
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.

Posted: Mon Jul 25, 2005 7:05 pm
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