It sounds a bit mad, but you need to set imageAlphaBlending($watermark_orig, true); rather than false, and remove the imageSaveAlpha($watermark_orig, true); line altogether.
Code: Select all
<?php
$file = "wm.jpg"; // Original image to have watermark applied to it.
$wm = "watermark.png"; // Watermark image
$image = imagecreatefromjpeg($file);
imagealphablending($image,true); // Tell PHP to use alpha blending when the destination image is $image
$wm_image = imagecreatefrompng($wm);
imagecopyresampled($image,$wm_image,0,0,0,0,imagesx($image),imagesy($image),imagesx($wm_image),imagesy($wm_image));
header("Content-type: image/jpeg");
imagejpeg($image);
?>
Obviously that code applies the watermark directly to the image instead of going via an image created with imagecreatetruecolour(). If you really need to do that I have an imagecreatetruecolortransparent() function that should do the job ..
Code: Select all
function imagecreatetruecolortransparent($x,$y) {
$i = imagecreatetruecolor($x,$y);
$b = imagecreatefromstring(base64_decode(blankpng()));
imagealphablending($i,false);
imagesavealpha($i,true);
imagecopyresized($i,$b,0,0,0,0,$x,$y,imagesx($b),imagesy($b));
return $i;
}
function blankpng() {
$c = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";
$c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg";
$c .= "dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN";
$c .= "egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ";
$c .= "oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA";
$c .= "DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";
return $c;
}
$image = imagecreatetruecolortransparent(200,200);
header("Content-type: image/png");
imagepng($image);