Jump to content

Image overlay/merging help (GD)?


KiaraRytzar

Recommended Posts

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. (:

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :P 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. :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :D 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.

Link to comment
Share on other sites

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. :D 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.

Link to comment
Share on other sites

This has got to be the most amazing picture I've ever drawn, but alright, here it is...

 

cat.pngcat_stripes.png

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:

cat_image_test.png

Link to comment
Share on other sites

This has got to be the most amazing picture I've ever drawn, but alright, here it is...

 

cat.pngcat_stripes.png

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:

cat_image_test.png

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.

Link to comment
Share on other sites

<?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);
?>

Link to comment
Share on other sites

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.

 

:)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.