Search code examples
phpwatermarkantialiasing

Resize watermark relative to parent image and keep antialiasing


I've seen other answers but none address this issue, I have this code

$stamp = imagecreatefrompng('w.png');
$im = imagecreatefromjpeg('image.jpg');
$stw = imagesx($im)/4;
$marge_bottom = 10;  
$marge_right = 10;

$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopyresized($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));


header("Content-Type: image/jpeg");
imagejpeg($im,NULL,100);

This works but the watermark (stamp) is jagged after being resizedenter image description here

Watermark Should look like this:

enter image description here

Edit: here is the solution for anyone with this problem, replace the imagecopyresized() function with imagecopyresampled()

imagecopyresampled($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));

Solution

  • Try imagecopyresampled instead of imagecopyresized:

    imagecopyresampled($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));
    

    From the manual:

    imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.