Hello everyone! I was trying to follow along with this tutorial:

http://www.sitepoint.com/article/watermark-images-php/

and everything worked fine until the end. Basically I'm trying to write a script that both resizes and watermarks images for a couple of websites I'm working on, so that I don't have to go through and do it for every picture manually. My resize script worked no problem, but when I tried to add the watermarking part (from the above tutorial) it stopped displaying the images and instead puts the url for the script on the page???? When I remove the image/jpeg header it outputs the resized image just fine, but the watermarked one is a bunch of symbols.

I have attached my script, I'm very new to PHP so please try not to laugh too hard if I have made a very silly mistake. :)

Attachments
<?php

header('content-type: image/jpeg');  


move_uploaded_file($_FILES['u_image']['tmp_name'], $_FILES['u_image']['name']);

$thumb = $_FILES['u_image']['name'];

$size = imagecreatefromjpeg("$thumb"); 

		// tall image
		if(imagesy($size) > imagesx($size)){
			$height = 300;
			$width  = round(300 * imagesx($size) / imagesy($size));
		}else{
		// wide image
			$height = round(300 * imagesy($size) / imagesx($size));
			$width  = 300;
		}

		$small = ImageCreateTrueColor($width, $height);

		imagecopyresampled($small, $size,0,0,0,0,imagesx($small),imagesy($small),imagesx($size),imagesy($size)); 

		imagejpeg($small, "test.jpg", 100);

		echo "<img src=\"test.jpg\"><br>";

$wm = imagecreatefrompng('kil_wm.png');

$wm_width = imagesx($wm);
$wm_height = imagesy($wm);

$image = imagecreatefromjpeg('test.jpg');

$size = getimagesize('test.jpg');

$dest_x = $size[0] - $wm_width - 5;
$dest_y = $size[1] - $wm_height - 5;

imagecopymerge($image, $wm, $dest_x, $dest_y, 0, 0, $wm_width, $wm_height, 100);

imagejpeg($image);  
imagedestroy($image);  
imagedestroy($wm);


?>

Perhaps I should try posting my code in the actual thread :) Here is the bit that's giving me trouble:

$wm = imagecreatefrompng('kil_wm.png');

$wm_width = imagesx($wm);
$wm_height = imagesy($wm);

$image = imagecreatefromjpeg('test.jpg');

$size = getimagesize('test.jpg');

$dest_x = $size[0] - $wm_width - 5;
$dest_y = $size[1] - $wm_height - 5;

imagecopymerge($image, $wm, $dest_x, $dest_y, 0, 0, $wm_width, $wm_height, 100);

imagejpeg($image);  
imagedestroy($image);  
imagedestroy($wm);

So, what am I doing wrong? Thanks!

Okay so somebody else tried the above code and it worked for them, leading me to believe that there must be something wrong with my GD2 configuration. Any ideas what that might be?

This article has been dead for over six months. Start a new discussion instead.