slpctrl Posted May 3, 2008 Share Posted May 3, 2008 <?php header("Content-type: image/jpeg"); function resizeimg($image) { $image = $_GET['image']; $w = 320; $h = 240; $x = @getimagesize($image); $sw = $x[0]; $sh = $x[1]; $new = @ImageCreateFromJPEG($image) or $new = @ImageCreateFromPNG($image) or $new = @ImageCreateFromGIF($image) or $new = false; if(!$new) { readfile($new); } else { $thumb = @ImageCreateTrueColor($w, $h); @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh); return $thumb; } return false; } $image = resizeimg($_GET['image']); $watermark = imagecreatefrompng('watermark.png'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $dest_x = imagesx($image) - $watermark_width - 5; $dest_y = imagesy($image) - $watermark_height - 5; imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> The lines: $dest_x = imagesx($image) - $watermark_width - 5; $dest_y = imagesy($image) - $watermark_height - 5; Is what I got to put the watermark in the corner, but when I try to calculate half the size of the thumbnail I create in the resize function (340x240) I can't seem to put the watermark in the center . Any suggestions? Quote Link to comment Share on other sites More sharing options...
slpctrl Posted May 3, 2008 Author Share Posted May 3, 2008 Meh, I got it after a lot of trial and error. Here's the final working code, it resizes an image to 320x240 and places a watermark in the center. You just gotta put watermark.png in the same directory to get it workin: <?php header("Content-type: image/jpeg"); function resizeimg($image) { $image = $_GET['image']; $w = 320; $h = 240; $x = @getimagesize($image); $sw = $x[0]; $sh = $x[1]; $new = @ImageCreateFromJPEG($image) or $new = @ImageCreateFromPNG($image) or $new = @ImageCreateFromGIF($image) or $new = false; if(!$new) { readfile($new); } else { $thumb = @ImageCreateTrueColor($w, $h); @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh); return $thumb; } return false; } $image = resizeimg($_GET['image']); $watermark = imagecreatefrompng('watermark.png'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $dest_x = (imagesx($image) - $watermark_width)/2; $dest_y = (imagesy($image) - $watermark_height)/2; imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 30); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> Alternatively, if you wish to specify the width and height of the resize you can set them to GET variables: <?php header("Content-type: image/jpeg"); function resizeimg($image) { $image = $_GET['image']; $w = $_GET['w']; $h = $_GET['h']; $x = @getimagesize($image); $sw = $x[0]; $sh = $x[1]; $new = @ImageCreateFromJPEG($image) or $new = @ImageCreateFromPNG($image) or $new = @ImageCreateFromGIF($image) or $new = false; if(!$new) { readfile($new); } else { $thumb = @ImageCreateTrueColor($w, $h); @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh); return $thumb; } return false; } $image = resizeimg($_GET['image']); $watermark = imagecreatefrompng('watermark.png'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $dest_x = (imagesx($image) - $watermark_width)/2; $dest_y = (imagesy($image) - $watermark_height)/2; imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 30); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.