PHP GD watermark script produces blank watermark -
i'm trying add watermark images right after uploading them website, seems watermark keeps coming out black object no details. believe script working little bit because if wasn't, wouldn't see kind of watermark or script fail.
this script far:
$watermark = imagecreatefrompng('preview-watermark.png'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $image = imagecreatetruecolor($watermark_width, $watermark_height); $image = imagecreatefromjpeg($portfolio_preview_dir.'/'.$file); $size = getimagesize($portfolio_preview_dir.'/'.$file); $dest_x = $size[0] - $watermark_width - 5; $dest_y = $size[1] - $watermark_height - 5; imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100); imagejpeg($image, $portfolio_preview_dir.'/'.$file); imagedestroy($image); imagedestroy($watermark);
this it's producing. shape of watermark correct, watermark 325x37 pixels:
i have tried playing watermark image itself. first attempt save photoshop watermark (with transparent bg) using 'save web' , selected 'png-24'. didn't work, saved normal png (without 'save web') , still fails.
i'm not sure whether it's script or image! can please share knowledge me , fix issue?
$watermark = imagecreatefrompng('preview-watermark.png'); imagealphablending($watermark , false); imagesavealpha($watermark , true); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $image = imagecreatetruecolor($watermark_width, $watermark_height); $image = imagecreatefromjpeg($portfolio_preview_dir.'/'.$file); $size = getimagesize($portfolio_preview_dir.'/'.$file); $dest_x = $size[0] - $watermark_width - 5; $dest_y = $size[1] - $watermark_height - 5; imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); imagejpeg($image, $portfolio_preview_dir.'/'.$file); imagedestroy($image); imagedestroy($watermark);
there things:
- imagecopymerge not allow transparency png-24
(source: https://drupal.org/node/80369)
- imagesavealpha + imagealphablending allow save transparency
(source: http://php.net/manual/en/function.imagesavealpha.php)
i hope fixes problem.
also output jpeg, why? stay png , image support transparent watermarks, right not!
Comments
Post a Comment