webster08 Posted July 16, 2011 Share Posted July 16, 2011 I am trying to add two watermarks to one image, but the second watermark is not being merged. Can anyone help me correct my code or send me in the right directions; to correcting this issue? PS: I know that the header is set to plain text, but I have a reason for that. <?php header('content-type: text/plain text'); $watermark = imagecreatefrompng('watermark.png'); $watermark2 = imagecreatefromgif('watermark.gif'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $image = imagecreatetruecolor($watermark_width, $watermark_height); $image = imagecreatefromjpeg($_GET['src']); $size = getimagesize($_GET['src']); $dest_x = $size[0] - $watermark_width - 5; $dest_y = $size[1] - $watermark_height - 5; $pic = imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagecopymerge($pic, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); imagedestroy($watermark2); ?>[code=php:0] Quote Link to comment https://forums.phpfreaks.com/topic/242101-having-trouble-creating-multiple-watermark-can-anyone-help/ Share on other sites More sharing options...
Psycho Posted July 16, 2011 Share Posted July 16, 2011 Well, I see at least one problem: $pic = imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagecopymerge($pic, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagecopymerge() copies part of one image into another image - it doesn't return an image (it only returns true/false). In that second line you are trying to copy watermark2 into the return value of the first one. So, you should be doing this: imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagecopymerge($image, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); But, for that to work, watermark 2 will need to have transparency - otherwise it will be covering watermark 1. Although, I've never done that so I'm not positive if transparency is even available when doing that. Quote Link to comment https://forums.phpfreaks.com/topic/242101-having-trouble-creating-multiple-watermark-can-anyone-help/#findComment-1243328 Share on other sites More sharing options...
webster08 Posted July 16, 2011 Author Share Posted July 16, 2011 I tried your code mjdamato; but it still does not display the second watermark. The second watermark is a transparent gif; so it should not cover the main image. So here's what I got with your code: <?php header('content-type: text/plain text'); $watermark = imagecreatefrompng('watermark.png'); $watermark2 = imagecreatefromgif('watermark.gif'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); $image = imagecreatetruecolor($watermark_width, $watermark_height); $image = imagecreatefromjpeg($_GET['src']); $size = getimagesize($_GET['src']); $dest_x = $size[0] - $watermark_width - 5; $dest_y = $size[1] - $watermark_height - 5; imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagecopymerge($image, $watermark2, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); imagedestroy($watermark2); ?> That is actually what I did at first; the code you presented, but I added in the variable $pic, because the original two lines were not working. Quote Link to comment https://forums.phpfreaks.com/topic/242101-having-trouble-creating-multiple-watermark-can-anyone-help/#findComment-1243336 Share on other sites More sharing options...
Psycho Posted July 16, 2011 Share Posted July 16, 2011 Have you tried both watermarks individually to ensure they work? Quote Link to comment https://forums.phpfreaks.com/topic/242101-having-trouble-creating-multiple-watermark-can-anyone-help/#findComment-1243346 Share on other sites More sharing options...
webster08 Posted July 16, 2011 Author Share Posted July 16, 2011 Yeah... I tried them; do both do display separately. Quote Link to comment https://forums.phpfreaks.com/topic/242101-having-trouble-creating-multiple-watermark-can-anyone-help/#findComment-1243348 Share on other sites More sharing options...
webster08 Posted July 16, 2011 Author Share Posted July 16, 2011 Oh... ok... here we go... I got it.... figured it out. Here is the final code: <?php header('content-type: text/plain text'); $watermark = imagecreatefrompng('watermark.png'); $watermark2 = imagecreatefromgif('watermark.gif'); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); // here is the fix $watermark_width2 = imagesx($watermark2); $watermark_height2 = imagesy($watermark2); $image = imagecreatetruecolor($watermark_width, $watermark_height); $image = imagecreatefromjpeg($_GET['src']); $size = getimagesize($_GET['src']); $dest_x = $size[0] - $watermark_width - 5; $dest_y = $size[1] - $watermark_height - 5; imagecopymerge($image, $watermark, 1, 0, 0, 0, $watermark_width, $watermark_height, 65); imagecopymerge($image, $watermark2, 1, 0, 0, 0, $watermark_width2, $watermark_height2, 65); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); imagedestroy($watermark2); ?> Thanks for the help mjdamato! Quote Link to comment https://forums.phpfreaks.com/topic/242101-having-trouble-creating-multiple-watermark-can-anyone-help/#findComment-1243349 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.