I tried to duplicate a PNG image, which has drop shadow (i.e. alpha channel) and transparent background. However, the resulting image paints the shadow and transparent background with black. I tried with imagecopy and imagecopymerge; neither yielded to valid results, which aren't the same with the original image.
Preview of the images.
$src = imagecreatefrompng('img_box3-bg.png');
/* Using imagecopy. */
$dest = imagecreatetruecolor(116, 100);
imagecopy($dest, $src, 0, 0, 0, 0, 116, 100);
imagepng($dest, 'img_box3-bg.imagecopy.png');
imagedestroy($dest);
/* Using imagecopymerge. */
$dest2 = imagecreatetruecolor(116, 100);
imagecopymerge($dest2, $src, 0, 0, 0, 0, 116, 100, 100);
imagepng($dest2, 'img_box3-bg.imagecopymerge.png');
imagedestroy($dest2);
imagedestroy($src);
Help? Thanks beforehand.
Something like this:
$src = imagecreatefrompng('img_box3-bg.png');
/* Using imagecopy. */
$dest = imagecreatetruecolor(116, 100);
// this is new
imagesavealpha($dest, true);
$transparent = imagecolorallocatealpha($dest, 0, 0, 0, 127);
imagefill($dest, 0, 0, $transparent);
imagecopy($dest, $src, 0, 0, 0, 0, 116, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
Related
I want to add a color layer over a image in php using gd.
This is the image:
I want to overlay this with this color: #ABD0D2
I made a quick image how it should look at the end.
Keep in mind that the image should still be transparent
So far I have this code:
$img = imagecreatefrompng('image.png');
imagesavealpha($img, true);
imagefill($img, 0, 0, imagecolorallocatealpha($img, 0, 0, 0, 127));
// make overlay with new color???
imagepng($img, 'new.png');
imagedestroy($img);
You can create a new image, filled with your target colour, and then merge the two:
$img = imagecreatefrompng('image.png');
$w = imagesx($img);
$h = imagesy($img);
imagesavealpha($img, true);
$img2 = imagecreatetruecolor($w, $h);
imagefill($img2, 0, 0, imagecolorallocatealpha($img, 0xAB, 0xD0, 0xD2, 64));
imagecopy($img, $img2, 0, 0, 0, 0, $w, $h);
imagepng($img, 'new.png');
imagedestroy($img);
imagedestroy($img2);
Result:
It's not completely clear to me how you want to maintain transparency (as your expected result image isn't transparent) so in the code above I've set the 'mask' colour at 50% opacity.
That worked for me:
$width = 400;
$height = 400;
$image = imagecreatefrompng('img.png');
$blueOverlay = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
imagealphablending($image, false);
imagesavealpha($blueOverlay, true);
$blue = imagecolorallocatealpha($blueOverlay, 0, 0, 255, ceil(0.22 * 127));
imagefill($blueOverlay, 0, 0, $blue);
imagecopymerge($blueOverlay, $image, 0, 0, 0, 0, $width, $height, 70);
imagepng($blueOverlay, 'imgWithOverlay.png');
imagedestroy($image);
imagedestroy($blueOverlay);
I need to combine two images in PHP. Master picture is in PNG, secondary in JPG.
First picture:
(source: grafikstudio-m.com)
Secondary picture:
(source: grafikstudio-m.com)
Test:
http://happywin.konektor.grafikstudio-m.com/imageMerge/
<?php
$dest = imagecreatefrompng('muster.png');
$src = imagecreatefromjpeg("test.jpg");
imagecolortransparent($src);
imagesavealpha($dest, false);
imagealphablending($src, true);
imagesavealpha($src, true);
imagecopymerge($dest, $src, 0, 0, 0, -100, 2000, 1300, 50);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
I need the pictures to be connected and with a white background
Create a canvas image with same size as muster.png and fill it with white color.
$muster = imagecreatefrompng('muster.png');
$canvas = imagecreatetruecolor(imagesx($muster), imagesy($muster));
$white = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $white);
Open test.jpg and use the top-left pixel color as the transparent color.
$test = imagecreatefromjpeg('test.jpg');
imagecolortransparent($test, imagecolorallocate($test, 255, 255, 255));
Copy the 2 images to the canvas, the order is optional, notice that copy muster.png (truecolor) with imagecopy but copy test.jpg (pallete) with imagecopymerge.
imagecopy($canvas, $muster, 0, 0, 0, 0, imagesx($muster), imagesy($muster));
imagecopymerge($canvas, $test, 0, 100, 0, 0, imagesx($test), imagesy($test), 100);
header('Content-Type: image/png');
imagepng($canvas);
// ...destroy
I am trying to merge my two images in php. One image is being uploaded frim my system and the other is the one I am creating with transparent background.
Here is my code. My code is just showing a non-image icon.
I don't understand where I am wrong.
<?php
//Set the Content Type
header("Content-type: image/png");
#dispaly the image
$file=$_GET['file'];
// echo file_get_contents($file);
$im = imagecreatetruecolor(250, 200);
$black = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);
imagecolortransparent($im, $black);
//text to draw
$text="hello world";
//font path
$font = '/usr/share/fonts/truetype/droid/DroidSans.ttf';
// Add the text
imagettftext($im, 15, 0, 50, 50, $blue, $font, $text);
$dest=imagecreatefrompng($file);
$src=imagecreatefrompng($im);
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 250, 200);
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
Use $im not $src - as Sayed pointed out, imagecreatefrompng takes filename (string) as argument - not GD resource. Why set $src if $im already contains GD resource ready for use?
There is important part with imagettftext. I was able to reproduce the effect of empty icon if GD cannot find a font in given path. Check your location, permissions and letter case. Also, if you decide to just copy the .ttf file right into script location, refer to imagettftext() documentation as there is important caveat with ".ttf" extension.
Also, to create fully transparent image use:
(by George Edison in PHP doc for imagefill
$im = imagecreatetruecolor(317, 196);
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);
Also, from PHP doc for imagecopymerge() by Sina Salek: imagecopymerge_alpha function to provide for true transperency in imagecopymerge()
So, my solution:
<?php
//Set the Content Type
header("Content-type: image/png");
#dispaly the image
$file='test.png';
$im = imagecreatetruecolor(317, 196);
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);
$blue = imagecolorallocatealpha($im, 0, 0, 255, 0);
//text to draw
$text="hello world";
putenv('GDFONTPATH=' . realpath('.'));
$font = 'lucida';
imagettftext($im, 20, 0, 10, 50, $blue, $font, $text);
$dest=imagecreatefrompng($file);
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge_alpha($dest, $im, 10, 10, 0, 0, 200, 180, 100);
imagepng($dest);
imagedestroy($dest);
imagedestroy($im);
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);
}
?>
You should use imagecopymerge() function for this.
Find out link
http://php.net/manual/en/function.imagecopymerge.php
I have an image stacking process that I am trying to accomplish with PHP GD. What I have is the following:
3 Images:
Masked clipart
Clipart texture
Final background
My masked clipart has black in place of where transparency will be after the texture is applied to the clipart and is transparent for layering over a texture.
The following code sorta works for this:
$im = imagecreatetruecolor($width,$height);
imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);
imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
Output is an image with transparent background and the clipart with the texture applied.
However, when I open that file in photoshop, the transparent areas are black which also brings me to the rest of this function:
Now that I have this image, I need to layer it on top of the "Final background" image which will make all the transparency of the previous output now be this "Final background" texture. My thoughts were something like:
$im = imagecreatetruecolor($width,$height);
imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);
imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
$img = imagecreatetruecolor($width,$height);
imagecopy($img, $background, 0, 0, 0, 0, $width, $height);
imagecopy($img, $im, 0, 0, 0, 0, $width, $height);
imagedestroy($im);
return $img;
The problem with this is that is outputs the image with a black background instead of my final texture layer. I believe this code for the final layering may actually work fine and that the black background from the final output and in photoshop is from missing some alpha line in the first part. I have tried to play around with:
imagealphablending( $im, false );
imagesavealpha( $im, true );
Mixing and matching the true / false and alternating only using one of them didn't seem to matter.
If anyone could shed some light on my mistakes here, it would be greatly appreciated.
UPDATE
The images: http://imgur.com/a/7SN1S
The code:
// Layer clipart over texture and convert black to transparent (works)
$im = imagecreatetruecolor($width,$height);
imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);
imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
// Layer above image with transparency over background (non-working)
$img = imagecreatetruecolor($width,$height);
imagecopy($img, $background, 0, 0, 0, 0, $width, $height);
imagecopy($img, $im, 0, 0, 0, 0, $width, $height);
header('Content-Type: image/png');
//imagepng($im); // Correctly outputs first step
imagejpeg($img); // Incorrectly outputs final result
imagedestroy($im);
imagedestroy($img);
As noted on the imagecolortransparent() manual page:
Transparency is copied only with imagecopymerge() and true color images, not with imagecopy() or pallete images.
So, changing the following two lines (16 & 17 in index.php)...
imagecopy($img, $background, 0, 0, 0, 0, $width, $height);
imagecopy($img, $im, 0, 0, 0, 0, $width, $height);
...to this...
imagecopymerge($img, $background, 0, 0, 0, 0, $width, $height, 100);
imagecopymerge($img, $im, 0, 0, 0, 0, $width, $height, 100);
...gives (very close to) the desired result:
I have this script, which makes an image and posts it on another image:
<?php
$img=imagecreatetruecolor(150,20);
imagealphablending($img,false);
$col=imagecolorallocatealpha($img,255,255,255,127);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
imagefilledrectangle($img,0,0,180,20,$col);
imagealphablending($img,true);
$font='../ttf/0001.ttf';
$color = imagecolorallocate($img, 0, 0, 0);
imagettftext($img,11,0,5,14,$color,$font,'Text goes here');
imagealphablending($img,false);
imagesavealpha($img,true);
imagejpeg($img, '../custom_images/test.jpg');
// Create image instances
$dest = imagecreatefromjpeg('../custom_images/121536.jpg');
$src = imagecreatefromjpeg('../custom_images/test.jpg');
$width = imagesx($src);
$height = imagesy($src);
imageantialias($src, true);
$color = imagecolorallocatealpha($src, 0, 0, 0, 127);
$rotated = imagerotate($src, 0, $color);
imagesavealpha($rotated, true);
$trans_colour = imagecolorallocatealpha($rotated, 0, 0, 0, 127);
imagefill($rotated, 0, 0, $trans_colour);
imagepng($rotated, 'shahid.png');
$new_img = imagecreatefrompng('shahid.png');
$width = imagesx($new_img);
$height = imagesy($new_img);
// imagecopymerge($dest, $new_img, 50, 50, 0, 0, $width+60, $height+60, 100);
imagecopymerge_alpha($dest, $new_img, 0, 20, 0, 0, $width, $height, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
2 things:
Background is not transparent
I want the Width and Height to be automatic, so if the text is short, the image is it to.
What do I fault?