Merging Images using GD with PHP - php

i'm working on creating one PNG image from two others.
Image A and B have the same dimensions, they are both 200x400px. The final image the same.
I'm using the GD library with PHP.
So my idea was to create a PNG-24 from my original PNG-8, then use color transparency and finally copy the second image into
this new PNG-24. The problem appears in the first step anyway, when going from PNG-24 to PNG-8 with color transparency:
This is to get the original PNG-8 and it's dimensions:
$png8 = imagecreatefrompng($imageUrl);
$size = getimagesize($imageUrl);
Now i create a new PNG and fill it's background with a green color (not present in the images):
$png24 = imagecreatetruecolor($size[0], $size[1]);
$transparentIndex = imagecolorallocate($png24, 0x66, 0xff, 0x66);
imagefill($png24, 0, 0, $transparentIndex);
This is for making the green color transparent:
imagecolortransparent($png24, $transparentIndex);
Then i copy the png8 into the PNG-24:
imagecopy($png24, $png8, 0, 0, 0, 0, $size[0], $size[1]);
So here's the problem: the original PNG-8 looks good, but it has a green border surrounding the shape within the original image. It's difficult to explain really. Seems like some part of the green background is left in the remaining PNG.
What can i do?
thanks in advance
best regards,
Fernando

I had some problems with png transparency before and was able to solve them with this pattern:
// allocate original image to copy stuff to
$img = imagecreatetruecolor(200, 100);
// create second image
$bg = imagecreatefrompng('bg.png');
// copy image onto it using imagecopyresampled
imagecopyresampled($img, $bg, 0, 0, 0, 0, 200, 100, 200, 100);
imagedestroy($bg);
// create third image
// do same routine
$fg = imagecreatefrompng('fg.png');
imagecopyresampled($img, $fg, 50, 50, 0, 0, 50, 50, 50, 50);
imagedestroy($fg);
// output image
imagepng($img);
imagedestroy($img);
I think the only difference between mine and yours is imagecopy() vs. imagecopyresampled(). I seem to remember having problems with that though it was quite a while ago. You can see an example of an image I use this pattern on here: http://www.ipnow.org/images/1/bggrad/bg4/yes/TRANSIST.TTF/8B0000/custombrowserimage.jpg (I allocate a blank image, copy the background image in, copy the overlay with transparency in)

Related

php merge transparent png image to jpeg preventing the white background

I have successfully merged a png image to a jpeg background using the php GD library however I would like the png image to have a transparent background yet a white background is constantly displayed as shown below:
I have looked at a few posts to fix this issue and tried different methods of implementation all in vain, any suggestions as to what I should add or do?
Here is my code:
<?php
$background = imagecreatefromjpeg('img3.jpg');
$bird = imagecreatefrompng('img4.png');
$bird_x = imagesx($bird);
$bird_y = imagesy($bird);
imagesavealpha($bird, true);
$color = imagecolorallocatealpha($bird, 0, 0, 0, 127);
imagefill($bird, 0, 0, $color);
if (imagecopymerge($background, $bird, 0, 0, 0, 0, $bird_x, $bird_y, 100))
{
header('Content-Type: image/jpeg');
imagejpeg($background);
imagedestroy($bird);
}
else
{
header('Content-Type: text/html');
echo "Failed to Merge images!";
}
?>
Use imagecopy
imagecopy($dest_image, $src, ($offset + 250), $offset, 0, 0, imagesx($src),imagesy($src));
I guess the specified behavior is by design.
imagecopymerge merges N% of pixels of the source image with (100-N)% of pixels from the destination image. From this definition it would be logical to assume that you won't see any of the background image pixels in the area where you've copied the bird (which is the full rectangle). Hence the white pixels instead of alpha.
Try imagecopy or imagecopyresampled (example) without resizing.

"z-index" in php GD

In css, we have a property called "z-index", what is the same in PHP GD and Image Functions to control the "z-index"?
I've been searching but I can't find one, please help.
Thankyou
there nothing existing like z-index in php GD library
but there several ways to overlap image over image or text over image
$redimg = imagecreatetruecolor(100, 100);
$image = imagecreatefrompng('image.png');
// sets background to red
$red = imagecolorallocate($redimg, 255, 0, 0);
imagefill($redimg, 0, 0, $red);
// Merge the red image onto the PNG image
imagecopymerge($image, $redimg, 0, 0, 0, 0, 100, 100, 75);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($redimg);
here one example, or let me know, what exactly you trying to do, I will help you
There's more information here.

PHP GD image merge changing my image to black

I have a very frustrating situation. I am using PHP GD for the first time, and it's been a bit of a rollercoaster relationship. Basically, I am trying to merge 2 images, a square one (with a height/width of x) onto a rectangle (with a width of x and a height of y).
The square needs to be centered vertically. But this isn't the issue - I've managed to position it correctly.
Whats happening is, my rectangle is white. My square has a white background, so when the images are merged, it should just look like my asset on a white rectangluar background.
When I merge the image though, GD is for some reason changing my background white rectangle to black - so you can see the white square in the middle, with black "bars" on top and bottom. Can anyone help?
Code is:
//create copy of original image to correct size
imagecopyresized($dst_image, $src_image, 0,0,0,0,$x_width,$x_height,$orig_img_x_width,$orig_img_x_height);
imagejpeg($dst_image, "resized_copy.jpg", 100);
$img = imagecreatetruecolor(1333, 2000);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill ( $img, 0, 0, $white );
imagefilledrectangle($img,0,0,1333,2000, $white);
imagejpeg($img, "rectangle.jpg", 100);
//merge images
$dest2 = imagecreatefromjpeg("rectangle.jpg");
$src2 = imagecreatefromjpeg('resized_copy.jpg');
imagecopymerge($dest2, $src2, 0, 0, 0, -333.5, $x_width, $x_height, 100);
imagejpeg($dest2, "final_image.jpg", 100);
I've tried using imagecopy instead of imagecopymerge, but I get the same result. I'm sure there is a simple explanation, but I cant seem to find it trawling through the php manual.
I've read your question a few times but I'm not convinced I understand exactly what you are trying to achieve so I've made a few assumptions in producing the below code.
For the sake of simplicity I've created a 'square.jpg' test image file like so:
(Note that I've used small image sizes here so I can show them inline.)
// read in the square test image.
$square = imagecreatefromjpeg('square.jpg');
$square_x = imagesx($square); // 100px
$square_y = imagesy($square); // 100px
// create the rectangular image to merge with.
$rectangle = imagecreatetruecolor(100, 200);
$rectangle_x = imagesx($rectangle); // 100px
$rectangle_y = imagesy($rectangle); // 200px
// note that this isn't white, but rather a lovely shade of blue to better
// show the image on the white SO background!
$white = imagecolorallocate($rectangle, 128, 128, 255);
imagefill($rectangle, 0, 0, $white);
// merge the images.
imagecopymerge(
$rectangle,
$square,
0,
($rectangle_y / 2) - ($square_y / 2), // to vertically centre the square.
0,
0,
$square_x,
$square_y,
75 // Just to show the merge clearly; change back to 100 for your usage.
);
imagejpeg($rectangle, 'final_image.jpg', 100);
imagedestroy($rectangle);
imagedestroy($square);
This gives me the following image in final_image.jpg:

php add a picture onto another

I would like to change the color of an image with php.
if I wanted to make it appear redder applicherei an image on a higher level across an image with a transparent red and more or less high can indicate how the original photo should be red.
I can say gd php functions to create an image of a color (RGBA) and apply it to another image?
thanks :)
You can try using GD's imagecopymerge function, which copies one image to another and supports alpha transparency. Something like this should work:
<?php
$redimg = imagecreatetruecolor(100, 100);
$image = imagecreatefrompng('image.png');
// sets background to red
$red = imagecolorallocate($redimg, 255, 0, 0);
imagefill($redimg, 0, 0, $red);
// Merge the red image onto the PNG image
imagecopymerge($image, $redimg, 0, 0, 0, 0, 100, 100, 75);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($redimg);
?>
There's more information here.

Image transparency in PHP GD library

I need to create a PNG image from 4 parts of another PNG image with different levels of transparency using GD library in PHP. For example:
Result should look like this
I tried to do this thing in different ways but I coudn't achieve the desired result.
Thank you in advance ;)
Load your image with imagecreatefrompng(). Create a truecolor image with imagecreatetruecolor() and then set it fully transparent with imagecolorallocatealpha() and imagefill(). Then set alpha blending mode for both the source and destination images with imagealphablending(). After this you can use imagecopymerge() to copy the image with alpha.
Unfortunately it's not possible to enforce an alpha multiplier for imagecopymerge(), so this will get you only halfway there -- Inconvenient options include repeating imagecopymerge() calls on those parts of the image you want to be less transparent, choosing between several source images depending on the level of transparency you want to use, or going through the image pixel-by-pixel, which is inconveniently slow.
If you are not married to the image* functions, consider using ImageMagick instead. It is much more robust.
I did this:
$output = imagecreatetruecolor([width], [height]);
imagesavealpha($output , true);
$trans_colour = imagecolorallocatealpha($output , 0, 0, 0, 127);
imagefill($output , 0, 0, $trans_colour);
Now the image is transparent :)
The whole script:
$output = imagecreatetruecolor([width], [height]);
imagesavealpha($output , true);
$trans_colour = imagecolorallocatealpha($output , 0, 0, 0, 127);
imagefill($output , 0, 0, $trans_colour);
header('Content-Type: image/png');
imagepng($output);
imagedestroy($output);
Hope it helps!
This worked for me with both gif and png (of course, change every reference of png in this example to gif if using that image type).
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
$colour = imagecolorallocate($virtual_image,255,255,255);
imagefill($virtual_image , 0, 0, $colour);
imagealphablending($virtual_image,true);
imagesavealpha($virtual_image , true);
//the next line only if you're resizing to a new $width/$height, otherwise leave this line out
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
header('Content-Type: image/png');
if (imagepng($virtual_image)) imagedestroy($virtual_image);

Categories