Combining transparent pngs - php

I have looked here on SO for solutions for my problem, but I can't find a solution that seems to work for me. Maybe I'm doing something wrong.
I am merging a number of transparent PNGs together, into one display for the customer. There are pretty much three layers (obviously the background is transparent, then fairly rectangular PNGs, but somewhat out of square, and then smaller ones on top of that).
The top layer of smaller images goes on the rectangles nicely. But the rectangles look as though they were filled with black first, then the image applied - you can see this because the image is slightly out of square, so the edges usually have this black line around it. The thing that I don't understand is why this layer has it and not the top - both images are instantiated with the same code.
Here is my code (I found the imagecopymerge_alpha function somewhere, and it seems to do pretty well, but obviously, not entirely).
// Merging:
$this->imagecopymerge_alpha($this->image, $resizedImage, $startX, $startY, 0, 0, imagesx($resizedImage), imagesy($resizedImage), 100);
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
// Creating the background/container image:
private function createTransparent($width, $height)
{
$image = imagecreatetruecolor($width, $height);
imagealphablending($image, false);
imagesavealpha($image, true);
$transparentColor = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $transparentColor);
return $image;
}
So, my question is this: do you see anything in here that would make a transparent PNG not maintain it's transparency and merge correctly with a base transparent PNG?

Related

Merge an image with background image gives black background in php

I have a light blue image set as $dest.
I have the logo set as logo1
I want to insert the logo into the light blue image. So I tried the following:
<?php
$dest = imagecreatefrompng('template.png');
$logo1 = 'tst.png';
$logo1created = imagecreatefrompng($logo1);
imagealphablending($logo1created,true);
imagesavealpha($logo1created, true);
$logo1width = imagesx($logo1created);
$logo1height = imagesy($logo1created);
$bg_color = imagecolorat($logo1created,1,1); //get color of top-left pixel
imagecolortransparent($logo1created, $bg_color);
imagecopymerge($dest, $logo1created, 70, 132, 0, 0, $logo1width, $logo1height, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($logo1created);
?>
I get the following as the output.
The logo is not transparent (it has a white background) and I have resized the image to 124*30
I want to set transparent background to the logo when I do that there is a black background as per the screenshot above. I am not sure why? Also, the image is blurred. Can someone help me fix this?
Thanks!
Please try the following: (you may adjust the x and y by changing the values 60,70 to other values)
<?php
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
$base = imagecreatefrompng('template.png');
$logo1 = 'tst.png';
$logo = imagecreatefrompng($logo1);
imagecopymerge_alpha($base, $logo, 60, 70, 0, 0, 124, 30, 100);
header('Content-Type: image/png');
imagepng($base);
?>

Image with transparanent background becoming white when added with imagecopymerge

I'm using imagecopymerge to place an image on top of another image, however, when I do so, the transparent part of the image becomes white, which isn't what I am after. So the images I have are the below as the background:
And the image I am adding on top of it, with transparent background, is this:
This, however, (along with the rest of my code) results in the area around the circle, and within the boundaries being white:
The code I am using at the moment to inject the value is:
$magical = imagecreatefrompng('Magical.png');
imagecopymerge($img, $magical, 366, 135, 0, 0, 32, 32, 100);
Research has said I should try using things like imagealphablending or imagecopy instead of imagecopymerge, but that didn't work. I've also found references to trying imagecolortransparent and imagecolorallocate but that didn't work either. So, for example, neither of these attempts worked:
$magical = imagecreatefrompng('Magical.png');
$white = imagecolorallocate($output, 255, 255, 255);
imagecolortransparent($magical, $white);
imagecopymerge($img, $magical, 366, 135, 0, 0, 32, 32, 100);
// or this attempt
$magical = imagecreatefrompng('Magical.png');
imagealphablending($img, true);
imagealphablending($magical, true);
imagecopymerge($img, $magical, 366, 135, 0, 0, 32, 32, 100);
What am I missing here? How do I ensure that when the image is added to the other its transparency is retained?
I pulled your images into my local machine and used the imagecopymerge_alpha function listed on the PHP Documentation - https://www.php.net/manual/en/function.imagecopymerge.php#92787
<?php
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
$img = imagecreatefrompng('baseImage.png');
$magical = imagecreatefrompng('overlay.png');
imagecopymerge_alpha($img, $magical, 366, 135, 0, 0, 32, 32, 100);
imagepng($img, "./test.png");

Is it possible to use a image by cropping to fixed size

I have two different images, one is in my local machine and another is in web. I want to merge these two image, I have everything but my problem is these two two image is not same width and height. My local image is a mask and the online image need to fit with local. Local image dimension 400x400.
Problems: If the online image dimension is lower/greater then the local one then the image just take the actual size, not fit into the mask image.
Now what should i do to make these image fit to each other?
My output Image
What i try is [Collected from web]-
//define the width and height of our images
define("WIDTH", 400);
define("HEIGHT", 400);
$dest_image = imagecreatetruecolor(WIDTH, HEIGHT);
//make sure the transparency information is saved
imagesavealpha($dest_image, true);
//create a fully transparent background (127 means fully transparent)
$trans_background = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);
//fill the image with a transparent background
imagefill($dest_image, 0, 0, $trans_background);
//take create image resources out of the 3 pngs we want to merge into destination image
$a = imagecreatefrompng('https://rickwgrundy.files.wordpress.com/2014/01/pt509_stick_figure_podium_speaking-348x370.png');
$b = imagecreatefrompng('400x400.png');
//copy each png file on top of the destination (result) png
imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT);
imagecopy($dest_image, $b, 0, 0, 0, 0, WIDTH, HEIGHT);
//send the appropriate headers and save the image in the given link name
header('Content-Type: image/png');
imagepng($dest_image, "result.png", 9);
//destroy all the image resources to free up memory
imagedestroy($a);
imagedestroy($b);
imagedestroy($dest_image);
Instead of
imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT); you need to copy AND resize the image using imagecopyresized http://php.net/manual/en/function.imagecopyresized.php
So you need this to replace imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT); with:
$image_current_width = imagesx($a);
$image_current_height = imagesy($a);
imagecopyresized($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT, $image_current_width , $image_current_height );
This will resize your image while it copies it across.
yes Is it possible but your web image not 400x400 . your web image 348X370 so. if your local image 348X370 it is 100% work properly . i try my local image base on your web image then working .
my image and your web image
and my code here
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x,$src_y, $src_w, $src_h, $pct)
{
$cut = imagecreatetruecolor($src_w, $src_h);
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
$image1 = imagecreatefrompng('https://rickwgrundy.files.wordpress.com/2014/01/pt509_stick_figure_podium_speaking-348x370.png'); //348 x 370
$image2 = imagecreatefrompng('b.png'); //348 x 370
imagealphablending($image1, false);
imagesavealpha($image1, true);
imagecopymerge_alpha($image1, $image2, 10, 9, 0, 0, 348, 370, 100);
header('Content-Type: image/png');
imagepng($image1);

Adding some opacity on an image with imagecopymerge in PHP

Here my problem:
I want to change the opacity of an image, by copying it on another transparent image.
My code:
$opacity = 50;
$transparentImage = imagecreatetruecolor($width, $height);
imagesavealpha($transparentImage, true);
$transColour = imagecolorallocatealpha($transparentImage , 0, 0, 0, 127);
imagefill($transparentImage , 0, 0, $transColour);
imagecopymerge($transparentImage, $image, 0, 0, 0, 0, $width, $height, $opacity);
$image = $transparentImage;
header('Content-type: image/png');
imagepng($image);
By doing this, when I use imagecopymerge, $transparentImage loses its transparency... So $image is merged on a black image... and not on a transparent image !
However, when I show $transparentImage before calling imagecopymerge, the image is transparent in my navigator !
Is there a solution to change opacity of my image, without adding it on a colored background ?
It seems that imagecopymerge does not support the alpha (transparency) channel on images. Fortunately you can use a workaround with imagecopy to do it correctly. Here's a function to do this, taken from the comments on php.net:
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}

How to avoid black background when rotating an image 45 degree using PHP?

Hi I have to flip a thumpnail image before merge it with another jpeg file. but when I rotate 45 degree using php. It shows a black background. how can I avoid that. any body can help me.
Well, if you are generating a jpg, using PHP GD you set the color of the background as the third option of the function imagerotate. In this example I'm gonna assume that you are rotating a jpg image $filename by an arbitrary $angle degrees, and you want a white background, i.e. color code 16777215:
$rotatedImage = imagerotate(imagecreatefromjpeg($filename), ((360-$angle)%360), 16777215);
black is color code 0, which is default, and the rest of the color gamma is in between the two, so you just need to decide which background color you would like
EDIT:
for transparent backgrounds, if you are generating a png you would do:
$destimg = imagecreatefromjpeg($filename);
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, ((360-$angle)%360), $transColor);
Hope that helps
<?
$image = "130.jpg";
$degrees = 25;
for($i=0;$i<count($data);$i++){
$ext = "";
$extarr = "";
$extarr = explode(".", $data[$i]['name']);
$ext = array_pop($extarr);
if($ext == "png"){
$rotate = imagecreatefrompng("images/".$data[$i]['name']);
$transColor = imagecolorallocatealpha($rotate, 255, 255, 255, 270);
$watermark1[$i] = imagerotate($rotate, ((360-$degrees)%360), $transColor);
}
}
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opct){
$w = imagesx($src_im);
$h = imagesy($src_im);
$cut = imagecreatetruecolor($src_w, $src_h);
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, (100 - $opct));
}
for($i=0; $i<count($watermark1); $i++){
if($i == 0) imagecopymerge_alpha($image, $watermark1[$i], $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
else imagecopymerge_alpha($image, $watermark1[$i], ($i*$dest_x)*3, ($i*$dest_y)*15, 0, 0, $watermark_width, $watermark_height, $opacity);
imagedestroy($watermark1[$i]);
}
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
Also, do your watermark images have alpha channel or are they fully opaque?

Categories