Image create using gd - php

I am creating image using GD library all the functions are working fine. But the main problem where i stucked that i want to merge png image over an other image but after overlapping it cannot merge properly and looking like jpg or other instead of png. I cannot upload my image here due to low reputation so click on these links below to see the image.
The image which i want to merge is this
Png image
The image where i merge above image is:
My code is here:
<?php
$im = imagecreate(288,288);
$background_color = imagecolorallocate($im, 230, 248, 248);
$file = 'images/smiley/smile'.$_POST['smiley'].'.png';
$bg = imagecreatefrompng($file);
imagealphablending($im, true);
imagesavealpha($bg, true);
imagecopyresampled($im, $bg, 80, 80, 0, 0, 50, 50, 185, 185);
header("Content-Type: image/png");
$filename = $_SESSION['rand'].'.png';
imagepng($im,$filename);
echo '<img src="'.$filename.'" alt="" />';
?>

Your background image doesn't have an alpha channel. This makes the PHP GD library do all of it's copying operations without using an alpha channel, instead just setting each pixel to be fully opaque or transparent, which is not what you want.
The simplest solution to this is to create a new image of the same size as the background that has an alpha channel, and then copy both the background and face into that one.
$baseImage = imagecreatefrompng("../../var/tmp/background.png");
$topImage = imagecreatefrompng("../../var/tmp/face.png");
// Get image dimensions
$baseWidth = imagesx($baseImage);
$baseHeight = imagesy($baseImage);
$topWidth = imagesx($topImage);
$topHeight = imagesy($topImage);
//Create a new image
$imageOut = imagecreatetruecolor($baseWidth, $baseHeight);
//Make the new image definitely have an alpha channel
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
imagecopy($imageOut, $baseImage, 0, 0, 0, 0, $baseWidth, $baseHeight); //have to play with these
imagecopy($imageOut, $topImage, 0, 0, 0, 0, $topWidth, $topHeight); //have to play with these
//header('Content-Type: image/png');
imagePng($imageOut, "../../var/tmp/output.png");
That code produces this image:

Related

How to create background clear color of png with PHP? [duplicate]

Currently I would like to create a transparent png with the lowest quality .
The code:
<?php
function createImg ($src, $dst, $width, $height, $quality) {
$newImage = imagecreatetruecolor($width,$height);
$source = imagecreatefrompng($src); //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,$width,$height);
imagepng($newImage,$dst,$quality); //imagepng() creates a PNG file from the given image.
return $dst;
}
createImg ('test.png','test.png','1920','1080','1');
?>
However, there are some problems:
Do I need to specific a png file before creating any new file? Or can I create without any existing png file?
Warning: imagecreatefrompng(test.png): failed to open stream: No such file or directory in
C:\DSPadmin\DEV\ajax_optipng1.5\create.php on line 4
Although there are error message , it still generate a png file , however, what I found that is the file is a black color image , do I need to specific any parameter to make it transparent?
Thanks.
To 1)
imagecreatefrompng('test.png') tries to open the file test.png which then can be edited with GD functions.
To 2)
To enable saving of the alpha channel imagesavealpha($img, true); is used.
The following code creates a 200x200px sized transparent image by enabling alpha saving and filling it with transparency.
<?php
$img = imagecreatetruecolor(200, 200);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');
Take a look at:
imagecolorallocatealpha
imagefill
An example function copies transparent PNG files:
<?php
function copyTransparent($src, $output)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor($x,$y);
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
$alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alpha_channel);
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, 0, 0, 0, 0, $x, $y);
// Save transparency
imagesavealpha($im,true);
// Save PNG
imagepng($im,$output,9);
imagedestroy($im);
}
$png = 'test.png';
copyTransparent($png,"png.png");
?>
1) You can create a new png file without any existing one.
2) You get a black color image because you use imagecreatetruecolor();. It creates a highest quality image with a black background. As you need a lowest quality image use imagecreate();
<?php
$tt_image = imagecreate( 100, 50 ); /* width, height */
$background = imagecolorallocatealpha( $tt_image, 0, 0, 255, 127 ); /* In RGB colors- (Red, Green, Blue, Transparency ) */
header( "Content-type: image/png" );
imagepng( $tt_image );
imagecolordeallocate( $background );
imagedestroy( $tt_image );
?>
You can read more in this article: How to Create an Image Using PHP

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.

PHP GD scale image

I'm trying to play around with GD, and I'm trying to get it to work with large images. I want a image that's originally 640x640 to resize to 130x130 on my image that I'm creating in GD. However, with my code it just crops 130x130 of the image from the upper left corner. In other words, I don't get the whole image in 130x130. I've been trying every snippet I could find, but still no luck in getting a hold of this. This is the code I have;
header ("Content-type: image/jpeg");
$image1Url = "background.jpg";
$image2Url = "image.jpg";
$image1 = imageCreateFromjpeg($image1Url);
$image2 = imageCreateFromjpeg($image2Url);
imagecopymerge($image1, $image2, 10, 10, 0, 0, 130, 130, 100);
$line1 = "This is the first line";
$line2 = "This is the second line";
$font = "./VERDANA.TTF";
$white = imagecolorallocate($image1, 255, 255, 255);
$yellow = imagecolorallocate($image1, 252, 205, 5);
imagefttext($image1, 14, 0, 150, 110, $yellow, $font, $line1);
imagefttext($image1, 14, 0, 150, 135, $white, $font, $line2);
Imagejpeg ($image1, NULL, 100);
ImageDestroy ($image1);
ImageDestroy ($image2);
I want the image specified as $image2Url to be scaled down to 130x130 no matter what size it's originally is. It's important to me that I maintain the aspect ratio though.
I've been trying different snippets I could find, but still no luck... I've been able to resize the original image to the size I want, but not within the final image in my GD script.
If you're using PHP version >= 5.5 you should use imagescale(). If not, use the following right after loading $image2:
$image3 = imagecreatetruecolor(130,130);
list($image2w, $image2h) = getimagesize($image2Url);
imagecopyresampled($image3, $image2, 0, 0, 0, 0, 130, 130, $image2w, $image2h);
// then use $image3 instead of $image2

Create a transparent png file using PHP

Currently I would like to create a transparent png with the lowest quality .
The code:
<?php
function createImg ($src, $dst, $width, $height, $quality) {
$newImage = imagecreatetruecolor($width,$height);
$source = imagecreatefrompng($src); //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,$width,$height);
imagepng($newImage,$dst,$quality); //imagepng() creates a PNG file from the given image.
return $dst;
}
createImg ('test.png','test.png','1920','1080','1');
?>
However, there are some problems:
Do I need to specific a png file before creating any new file? Or can I create without any existing png file?
Warning: imagecreatefrompng(test.png): failed to open stream: No such file or directory in
C:\DSPadmin\DEV\ajax_optipng1.5\create.php on line 4
Although there are error message , it still generate a png file , however, what I found that is the file is a black color image , do I need to specific any parameter to make it transparent?
Thanks.
To 1)
imagecreatefrompng('test.png') tries to open the file test.png which then can be edited with GD functions.
To 2)
To enable saving of the alpha channel imagesavealpha($img, true); is used.
The following code creates a 200x200px sized transparent image by enabling alpha saving and filling it with transparency.
<?php
$img = imagecreatetruecolor(200, 200);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');
Take a look at:
imagecolorallocatealpha
imagefill
An example function copies transparent PNG files:
<?php
function copyTransparent($src, $output)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor($x,$y);
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
$alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alpha_channel);
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, 0, 0, 0, 0, $x, $y);
// Save transparency
imagesavealpha($im,true);
// Save PNG
imagepng($im,$output,9);
imagedestroy($im);
}
$png = 'test.png';
copyTransparent($png,"png.png");
?>
1) You can create a new png file without any existing one.
2) You get a black color image because you use imagecreatetruecolor();. It creates a highest quality image with a black background. As you need a lowest quality image use imagecreate();
<?php
$tt_image = imagecreate( 100, 50 ); /* width, height */
$background = imagecolorallocatealpha( $tt_image, 0, 0, 255, 127 ); /* In RGB colors- (Red, Green, Blue, Transparency ) */
header( "Content-type: image/png" );
imagepng( $tt_image );
imagecolordeallocate( $background );
imagedestroy( $tt_image );
?>
You can read more in this article: How to Create an Image Using PHP

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