KiaraRytzar Posted April 17, 2012 Share Posted April 17, 2012 Hello. I'm currently trying to write a code to merge two separate images together, but for the life of me I can't find anything that works. Basically what I'm trying to do is add a "marking" (cat_stripes.png) to a pet image (cat.png). In the future I also want to use this for adding items to pets and things like that. Both of the images have transparent backgrounds, so that needs to be taken into account. If you haven't got a clue what I'm talking about, look up Wajas or similar virtual pet sites to get an idea of how the markings work. I just need something simple, since I don't have a site set up yet and I'm just testing some things out for a future project. I don't have any codes at the moment, since nothing has worked so far. I'm pretty new to GD too (I'm familiar with all the basic php stuff, so don't panic), so you might need to explain some of the more advanced stuff to me. Any help with this would be really appreciated, thanks. (: Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/ Share on other sites More sharing options...
creata.physics Posted April 17, 2012 Share Posted April 17, 2012 And what exactly have you tried? Have you seen http://php.net/manual/en/function.imagecopymerge.php Also, searching "merge images php" on google has many results that seem sutible for what you're trying to accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338019 Share on other sites More sharing options...
KiaraRytzar Posted April 17, 2012 Author Share Posted April 17, 2012 I already tried imagecopymerge, but I've had no luck so far. I've spent a few hours on google as well. Trust me, I wouldn't be posting here if I hadn't already tried finding a solution. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338023 Share on other sites More sharing options...
l0gic Posted April 17, 2012 Share Posted April 17, 2012 I know the code you've tried haven't worked, but you may consider posting any/it/some so we can see what you have tried (that you have tried) and what hasn't worked. From there we can help you find out why it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338024 Share on other sites More sharing options...
KiaraRytzar Posted April 17, 2012 Author Share Posted April 17, 2012 Alrighty, well this is what I've got at the moment, from the php.net manual: <?php $dest = imagecreatefrompng('Images/cat.png'); $src = imagecreatefrompng('Images/cat_stripes.png'); imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 47, 75); header('Content-Type: image/png'); imagepng($dest); imagedestroy($dest); imagedestroy($src); ?> At the moment it's just displaying the base image (cat.png) with a white square over the top of it. Could someone clarify what each of the numbers are for? I'm finding a lot of examples but nothing that really explains it. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338028 Share on other sites More sharing options...
MMDE Posted April 17, 2012 Share Posted April 17, 2012 I will try to help you, as I've written similar scripts before to automatically put together images, where I also automatically resize them, add different backgrounds, add different frames and also four numbers, all with data from a database! Also done some minor simple image editing tools with custom fonts, backgrounds, colors, stripes and text etc xD Give me the two images you want to merge, send me a pm or something if you don't want to share it here, and I will write you a short code that probably will explain it to you, as there are some functions you need to run for this to do everything you want. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338030 Share on other sites More sharing options...
creata.physics Posted April 17, 2012 Share Posted April 17, 2012 Could someone clarify what each of the numbers are for? I'm finding a lot of examples but nothing that really explains it. The link I gave you in the first post explains exactly what each paramater determines. This post may help: http://www.php.net/manual/en/function.imagecopymerge.php#88456 Personally, I find the best help in the php manuals comments. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338032 Share on other sites More sharing options...
KiaraRytzar Posted April 17, 2012 Author Share Posted April 17, 2012 I already figured out what the numbers did not long after I posted that; now I'm just having a problem with the overlay/marking image. This is the code as of right now: <?php $dest = imagecreatefrompng('Images/cat.png'); $src = imagecreatefrompng('Images/cat_stripes.png'); imagesavealpha($src, true); imagesavealpha($dest, true); imagecopymerge($dest, $src, 0, 0, 0, 0, 170, 306, 100); header('Content-Type: image/png'); imagepng($dest); imagedestroy($dest); imagedestroy($src); ?> imagesavealpha works with the base but not the overlay. The marking layer is turning out really pixel-y and it's showing colour where it should be transparent. I don't know if that makes any sense. Also is there any way to make the size 100% instead of a specific number? @MMDE Thanks. I'd rather not post the images that I'm using at the moment, but I could always upload a really quick sketch to use as an example. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338034 Share on other sites More sharing options...
MMDE Posted April 17, 2012 Share Posted April 17, 2012 I already figured out what the numbers did not long after I posted that; now I'm just having a problem with the overlay/marking image. This is the code as of right now: <?php $dest = imagecreatefrompng('Images/cat.png'); $src = imagecreatefrompng('Images/cat_stripes.png'); imagesavealpha($src, true); imagesavealpha($dest, true); imagecopymerge($dest, $src, 0, 0, 0, 0, 170, 306, 100); header('Content-Type: image/png'); imagepng($dest); imagedestroy($dest); imagedestroy($src); ?> imagesavealpha works with the base but not the overlay. The marking layer is turning out really pixel-y and it's showing colour where it should be transparent. I don't know if that makes any sense. Also is there any way to make the size 100% instead of a specific number? @MMDE Thanks. I'd rather not post the images that I'm using at the moment, but I could always upload a really quick sketch to use as an example. Yes, please, some sample so I got something to work on. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338036 Share on other sites More sharing options...
KiaraRytzar Posted April 17, 2012 Author Share Posted April 17, 2012 This has got to be the most amazing picture I've ever drawn, but alright, here it is... cat.png & cat_stripes.png The code is exactly the same as earlier, except the size of the image has changed so now it's imagecopymerge($dest, $src, 0, 0, 0, 0, 227, 188, 100); This is how the image turns out at the moment: Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338040 Share on other sites More sharing options...
MMDE Posted April 17, 2012 Share Posted April 17, 2012 This has got to be the most amazing picture I've ever drawn, but alright, here it is... cat.png & cat_stripes.png The code is exactly the same as earlier, except the size of the image has changed so now it's imagecopymerge($dest, $src, 0, 0, 0, 0, 227, 188, 100); This is how the image turns out at the moment: Yeah okay, I think I know what you need to do. You need to run this one function, but let me test so I know for sure. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338042 Share on other sites More sharing options...
MMDE Posted April 17, 2012 Share Posted April 17, 2012 <?php $dest = imagecreatefrompng('Images/cat.png'); $src = imagecreatefrompng('Images/cat_stripes.png'); imagesavealpha($src, true); imagesavealpha($dest, true); imagecopy($dest, $src, 0, 0, 0, 0, imagesx($src), imagesy($src)); header('Content-Type: image/png'); imagepng($dest); imagedestroy($dest); imagedestroy($src); ?> Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338045 Share on other sites More sharing options...
KiaraRytzar Posted April 17, 2012 Author Share Posted April 17, 2012 You are a lifesaver. ;-; It works perfectly. Thanks so much for that! Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338047 Share on other sites More sharing options...
l0gic Posted April 17, 2012 Share Posted April 17, 2012 I was watching x-men earlier, just popped back to check this out but it sounds like you've got it all under control! Good luck with your project.. And don't forget to marked this as solved. Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338050 Share on other sites More sharing options...
KiaraRytzar Posted April 17, 2012 Author Share Posted April 17, 2012 Oh pfft, I didn't even know you could do that, I thought the mods had to do it or something. /genius Quote Link to comment https://forums.phpfreaks.com/topic/261084-image-overlaymerging-help-gd/#findComment-1338061 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.