0

I need to add watermark on uploaded image and also resize it to make thumbnails and icons from that watermarked image. Below is my function code which is adding watermark but I can't find out how to resize image to given height and width:

function watermark_image_new($target, $wtrmrk_file, $newcopy, $extension, $w = 0, $h = 0) {
$watermark = imagecreatefrompng($wtrmrk_file);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
//resize code
if ($w != 0) {
    list($width, $height) = getimagesize($target);
    if ($extension == 'jpeg' || $extension == 'jpg') {
        $img = imagecreatefromjpeg($target);
    } else if ($extension == 'png') {
        $img = imagecreatefrompng($target);
    }
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $img, 0, 0, 0, 0, $w, $h, $width, $height);
}
$img_w = imagesx($img);
$img_h = imagesy($img);
$wtrmrk_w = imagesx($watermark);
$wtrmrk_h = imagesy($watermark);
$dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
$dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
if ($extension == 'jpeg' || $extension == 'jpg') {
    imagejpeg($img, $newcopy, 100);
} else if ($extension == 'png') {
    imagepng($img, $newcopy);
}
imagedestroy($img);
imagedestroy($watermark);
}
Umair Malik
  • 1,403
  • 3
  • 30
  • 59
  • I googled "resize image in PHP" and found [Imagick::resizeImage](http://php.net/manual/en/imagick.resizeimage.php) – gregn3 Oct 01 '17 at 11:24
  • @gregn3 can you please explain how it works? it has some different parameters, what was their use? – Umair Malik Oct 01 '17 at 11:31
  • You're right, it's different from imagecopyresampled(). What is the exact problem with imagecopyresampled()? (I found the [manual page](http://php.net/manual/en/function.imagecopyresampled.php) for it too) – gregn3 Oct 01 '17 at 11:39
  • @gregn3 My issue is my code is not resizing image, it create image as it is with watermark on it. – Umair Malik Oct 01 '17 at 11:41
  • Are you sure you are supplying the correct parameters to imagecopyresampled() ? (There might be a bug in your code... I don't know) – gregn3 Oct 01 '17 at 11:49
  • 1
    @gregn3 oh yes! I corrected it, i missed $width, $height to copy here, but in real code they were present and still no change in image size. – Umair Malik Oct 01 '17 at 11:54
  • 1
    I would try to test imagecopyresampled() separately, until it works. Good luck! – gregn3 Oct 01 '17 at 11:57
  • 1
    Thanks! According to my findings, imagecopyresampled is returning true, i think issue occurs on code which implement watermark.. it is adding watermark on original image and saving it not the one we resized. – Umair Malik Oct 01 '17 at 12:09
  • 1
    Yes I see you were using **$img** in imagecopy() instead of **$dst**. I'm glad it's resolved now. :) – gregn3 Oct 01 '17 at 12:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155696/discussion-between-umair-malik-and-gregn3). – Umair Malik Oct 01 '17 at 12:48

1 Answers1

0

A simple example to use Imagick for solve your case. To render Watermark and resize it and output the changed image

<?php
$image = new Imagick();
$image->readImage("/path/to/image.jpg");
$watermark = new Imagick();
$watermark->readImage("/path/to/watermark.png");
// how big are the images?
$iWidth = $image->getImageWidth();
$iHeight = $image->getImageHeight();
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
if ($iHeight < $wHeight || $iWidth < $wWidth) {
 // resize the watermark
   $watermark->scaleImage($iWidth, $iHeight);
// get new size
$wWidth = $watermark->getImageWidth();
   $wHeight = $watermark->getImageHeight();
}
// calculate the position
$x = ($iWidth - $wWidth) / 2;
$y = ($iHeight - $wHeight) / 2;
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
episch
  • 388
  • 2
  • 19
  • While this does add the watermark to the image it does not resize the original image to a smaller dimension, which is what the OP requested. – Dawson Irvine Apr 13 '20 at 00:55
  • in order to fulfill the last case one would only have to execute $image->scaleImage again after $image->compositeImage – episch Jan 17 '21 at 00:31