Resizing and watermarking images

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Resizing and watermarking images

Post by evilmonkey »

Hello. I wrote a script that will resize and watermark an image. It resizes, but doesn't watermark. I was wondering if a guru can tell me what I'm doing wrong. Thank you very much. :)

Code: Select all

<?php
function savepic ($max_width, $max_height, $file, $filename){
	/*image resizing code
	*returns: jpeg image that is destroyed as soon as it's outputted
	*paramaeters that must be passed: $max_width, $max_hieght, $file
	*$max_width/$max_height are the maximum size of the picture in pizels
	*if not set, they default to 100 */
	
	if (@!$max_width)
	$max_width = 100;
	if (@!$max_height)
	$max_height = 100;
	//$root_dir = "C:/apache2triad/htdocs/tmeet/";
	$root_dir = "/home/vlad/public_html/";
	$directory = $root_dir . "pics"; //direcotry where $file is stored
	
	$size = GetImageSize($file);
	$width = $size[0];
	$height = $size[1];
	
	$x_ratio = $max_width / $width;
	$y_ratio = $max_height / $height;
	
	if ( ($width <= $max_width) && ($height <= $max_height) ) {
		$tn_width = $width;
		$tn_height = $height;
	}
	
	else if (($x_ratio * $height) < $max_height) {
		$tn_height = ceil($x_ratio * $height);
		$tn_width = $max_width;
	}
	else {
		$tn_width = ceil($y_ratio * $width);
		$tn_height = $max_height;
	}
	
	//watermark
	$watermark = imagecreatefrompng('images/tmeet_watermark.png');  
	$watermark_width = imagesx($watermark);  
	$watermark_height = imagesy($watermark);  
	$watermarkimage = imagecreatetruecolor($watermark_width, $watermark_height); 
	$dest_x = $tn_width - $watermark_width - 5;  
	$dest_y = $tn_height - $watermark_height - 5;  
	//end watermark
	
	$src = ImageCreateFromJpeg($file);
	
	$dst = ImageCreateTrueColor($tn_width,$tn_height);
	
	ImageCopyResized($dst, $src, 0, 0, 0, 0,
	$tn_width,$tn_height,$width,$height);
	
	//merge with watermark
	imagecopymerge($dst, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
	
	
	ImageJpeg ($dst, $root_dir . $filename, 90);
	
	ImageDestroy($src);
	ImageDestroy($dst);
	imagedestroy($watermark);
	
	
	//end image resizing code
	
}
:)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP-GD.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Jcart wrote:Moved to PHP-GD.
My bad. You'd think that after 700+ posts, I'd know to post in a proper section. :oops:
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I solved the problem by putting the watermarking code into another function, and calling that function after I saved the pic. :)
Post Reply