Image transparency and alpha when merging images with PHP - php

So I found some code on PHP Doc, and edited it slightly to merge two images I have. The image is then saved in a folder on the server. However there is a slight problem and I am unable to figure out why it is happening.
Firstly my code:
$glassurl = $_GET['GlassImg'];
$frameurl = $_GET['FrameImg'];
$filename = (int)date("H:i:s");
$src = imagecreatefromgif($frameurl);
$dest = imagecreatefromjpeg($glassurl);
imagecolortransparent($src, imagecolorat($src, 0, 0));
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagealphablending($src, false);
imagesavealpha($src, true);
$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);
// Output and free from memory
imagepng($dest, 'uploads/imagetest.png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src
);
Secondly some information about the images:
Both Images are exactly the same size
The 'pattern' image is just a block colour/pattern
The frame image has transparent parts within the frame (to allow pattern to show through)
The area around the frame is white to hise the excess pattern
I was hoping that when I overlayed the frame onto the pattern because of these parts that it would produce a window frame, with the glass pattern inside, and the white would hide the remaining patten.
To illustrate I have provided the images. and what happens.
Pattern:
Frame:
Result:
As you can see it doesn't result in what I expected. Can anyone please tell me where I am going wrong? I want to overlay the frame onto the pattern, keeping the transparent center and using the excess white to cover the rest of the patter. Any help is greatly appreciated.

Please note that your frame has white edges and if you sill want the windows to be wite you need to crop it and remove the imagecolortransparent added below if not you can use this
$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";
$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
imagecolortransparent($src, imagecolorat($src, 0, 0));
$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);
// Output and free from memory
header('Content-Type: image/png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
Output
You can also have
$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";
$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
$src_x = imagesx($src);
$src_y = imagesy($src);
$srcNew = imagecreatetruecolor($src_x, $src_y);
ImageColorTransparent($srcNew, imageColorAllocate($srcNew, 0, 0, 0));
imagecopy($srcNew, $src, 70, 50, 78, 60, 473, 293);
imagecopymerge($dest, $srcNew, 0, 0, 0, 0, $src_x, $src_y, 100);
header('Content-Type: image/png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
Output

Your image is not transparent as you described, try using this instead if I understood what you described correctly.
also you should find a program which does not transform transparency to white when saving (or check for options regarding this) if you really made those transparent in the first place.

Related

Remove transparent space from a png image while merging with another png image using PHP GD

I am merging two png images in my php code using php-gd. The images are getting merged, however the problem is that there is extra transparent space in the image which I want to remove.
Here is my php code.
<?php
$image1Url = "/home/sunpure-refined-sunflower-oil-v-5-ltr-1.png";
$image2Url = "/home/hypercity-every-day-sooji-rawa-v-1-kg-6.png";
$dest = imagecreatefrompng($image1Url);
$src = imagecreatefrompng($image2Url);
$offset2x = imagesx($src);
$offset3x = imagesx($dest);
$temp = imagecreatetruecolor($offset2x + $offset3x, 140);
$background = imagecolorallocate($temp, 0, 0, 0);
imagecolortransparent($temp, $background);
imagecopymerge($temp, $src, 0, 0, 0, 0, 180, 180, 100); //have to play with these numbers for it to work for you, etc.
imagecopymerge($temp, $dest, $offset2x-50, 0, 0, 0, 180, 180, 100);
header('Content-Type: image/png');
imagepng($temp, "/home/myImage.png");
imagedestroy($dest);
imagedestroy($src);
In the rightmost image, I was trying to remove the space between them , but due to transparent space, its not happening.

Put a PNG over a PNG and preserve transparency with PHP [duplicate]

So I found some code on PHP Doc, and edited it slightly to merge two images I have. The image is then saved in a folder on the server. However there is a slight problem and I am unable to figure out why it is happening.
Firstly my code:
$glassurl = $_GET['GlassImg'];
$frameurl = $_GET['FrameImg'];
$filename = (int)date("H:i:s");
$src = imagecreatefromgif($frameurl);
$dest = imagecreatefromjpeg($glassurl);
imagecolortransparent($src, imagecolorat($src, 0, 0));
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagealphablending($src, false);
imagesavealpha($src, true);
$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);
// Output and free from memory
imagepng($dest, 'uploads/imagetest.png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src
);
Secondly some information about the images:
Both Images are exactly the same size
The 'pattern' image is just a block colour/pattern
The frame image has transparent parts within the frame (to allow pattern to show through)
The area around the frame is white to hise the excess pattern
I was hoping that when I overlayed the frame onto the pattern because of these parts that it would produce a window frame, with the glass pattern inside, and the white would hide the remaining patten.
To illustrate I have provided the images. and what happens.
Pattern:
Frame:
Result:
As you can see it doesn't result in what I expected. Can anyone please tell me where I am going wrong? I want to overlay the frame onto the pattern, keeping the transparent center and using the excess white to cover the rest of the patter. Any help is greatly appreciated.
Please note that your frame has white edges and if you sill want the windows to be wite you need to crop it and remove the imagecolortransparent added below if not you can use this
$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";
$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
imagecolortransparent($src, imagecolorat($src, 0, 0));
$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);
// Output and free from memory
header('Content-Type: image/png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
Output
You can also have
$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";
$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
$src_x = imagesx($src);
$src_y = imagesy($src);
$srcNew = imagecreatetruecolor($src_x, $src_y);
ImageColorTransparent($srcNew, imageColorAllocate($srcNew, 0, 0, 0));
imagecopy($srcNew, $src, 70, 50, 78, 60, 473, 293);
imagecopymerge($dest, $srcNew, 0, 0, 0, 0, $src_x, $src_y, 100);
header('Content-Type: image/png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
Output
Your image is not transparent as you described, try using this instead if I understood what you described correctly.
also you should find a program which does not transform transparency to white when saving (or check for options regarding this) if you really made those transparent in the first place.

Image transparency not preserved when merging two images in PHP

When using examples from other posts to try and merge one PNG that has transparent parts on it with another non-transparent PNG, the foreground PNGs transparency is lost and defaults to white.
The code so far:
$width = 349;
$height = 250;
$base_image = imagecreatefrompng($_GET['bg']);
$top_image = imagecreatefrompng($_GET['fg']);
$merged_image = "merged.png";
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height);
imagepng($base_image, $merged_image);
Can anyone suggest where I may be going wrong?
Coming out like this
Should look like this
Copy from Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?
Codes should be like this:
imagesavealpha($base_image, true);
imagealphablending($base_image, false);
$image = imagecreatefrompng($_GET['bg']);
$frame = imagecreatefrompng($_GET['fg']);
imagealphablending($frame,true);
imagecopymerge($image, $frame, 0, 0, 0, 0, 0, 100, 100);
# Save the image to a file
imagepng($image, 'file-xyz.png');

Applying text to png transparency issue PHP

I am trying to write text onto a png, however when I do it puts a dark border around it, I am not sure why.
The original image:
The processed image:
Code:
// Load the image
$im = imagecreatefrompng("admin/public/images/map/order/wally.png");
// If there's an error, gtfo
if(!$im) {
die("");
}
$textColor = imagecolorallocate($im, 68, 68, 68);
$width = imagesx($im);
$height = imagesy($im);
$fontSize = 5;
$text = "AC";
// Calculate the left position of the text
$leftTextPos = ($width - imagefontwidth($fontSize)*strlen($text)) / 2;
// Write the string
imagestring($im, $fontSize, $leftTextPos, $height-28, $text, $textColor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
I've had this issue several times, let me find the answer...
Ok, found something:
imagesavealpha($im, true);
imagealphablending($im, true);
Write that before imagepng.
Yes, saving with alpha is important but loading it is important as well. Your PNG image might have transparency but it is good practice to account for that as well.
You'd need to create true color image, set alpha color and then draw your loaded image with text over it. So something like this:
// create true color image
$img = imagecreatetruecolor($width, $height);
$transparent_color = imagecolorallocatealpha($img, 255, 255, 255, 0);
imagealphablending($img, false);
imagefillrectangle($img, 0, 0, $width, $height, $transparent_color);
imagealphablending($img, true);
// draw previously loaded PNG image
imagecopy($img, $loaded_img, 0, 0, 0, 0, $width, $height);
// draw your text
// save the whole thing
imagesavealpha($img, true);
imagepng($img, $file);

Background transperancy in imagerotate()

Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.
But, to my great disappointment, it's not working at all.
It's just giving out a black background behind it.
Here's my code -
$patchImageS = 'image.png'; // the image to be patched over the final bg
$patchImage = imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage = imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');
I tried to change the parameters being passed in function to
imagerotate($patchImage, 23, 5, 0);
imagerotate($patchImage, 23, 0, 5);
Any help would be highly appreciated.
After a number of 99% finished answers, here's the solution I've found:
// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);
// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);
// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);
// Set your appropriate header
header('Content-Type: image/png');
// Render canvas to the browser
imagepng($png);
// Clean up
imagedestroy($png);
The key here is to include your imagecolorallocatealpha() in your imagerotate() call...
look for imagesavealpha() in the php-documentation - i think this is what you are looking for.
EDIT: here's an example:
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
$png = imagerotate($png, 23, 0, 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:
https://qna.habr.com/q/646622#answer_1417035
// get image sizes (X,Y)
$wx = imagesx($imageW);
$wy = imagesy($imageW);
// create a new image from the sizes on transparent canvas
$new = imagecreatetruecolor($wx, $wy);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
$rotate = imagerotate($imageW, 280, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
// get the newest image X and Y
$ix = imagesx($rotate);
$iy = imagesy($rotate);
//copy the image to the canvas
imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);

Categories