I’m having trouble executing createimagefrompng()
from a hook. I’ve used the fairly straightforward PHP way to add a watermark to an image. If I hardcode the image URLs in this function and for example execute it from a page or whatever it seems to work flawlessly, but whenever I execute the exact function from a hook (to add watermarks to images just uploaded to a post), WordPress seems to ‘crash’ (as in, the loading spinner keeps spinning and nothing actually happens).
The PHP code I use for the watermark – which works perfectly when called from a template or whatever:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Whenever I put this in a function, and comment out code part by part it seems to already crash on the first $stamp = ...
line. Can’t really figure out why this happens.
tl;dr scenario sketch
I want to add a watermark to every image uploaded in an ACF field in a specific posttype but the watermark PHP code provided in the PHP.net docs crashes on the first line of code.
Thanks guys!