Jump to content

[SOLVED] Center watermark?


slpctrl

Recommended Posts

<?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?

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.