I need to merge two pictures,
my code is like this:
$image = imagecreatefromjpeg("images/big.jpg");
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");
imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image1);
the frist picture is :
second is :
final result not transparent
Please help!!!Thanks!
new version of code:
$image = imagecreatefromjpeg("images/big.jpg");
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");
$background = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $background);
imagealphablending($image1, false);
imagesavealpha($image1, true);
imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image1);
and the result is :
Ok..i found a solution now..
$image = imagecreatefromjpeg("images/show01.jpg"); //
$image1 = imagecreatefrompng("small_image/1.png");//
$image3 = imagecreatefromgif("images/carBg.gif");//
$size = getimagesize("small_image/1.png");
$overlay = imagecreatetruecolor(80, 80);
$white = imagecolorallocate($overlay, 229, 229, 229);
imagefilledrectangle($overlay, 0, 0, 80, 80, $white);
imagecolortransparent($overlay,$white);
imagecopy($overlay, $image1, (80-$size[0])/2, (80-$size[1])/2, 0, 0, $size[0],$size[1]);
imagecopymerge($image3, $overlay, 0, 0, 0, 0, 80, 80, 100);
imagecopymerge($image, $image3, 280, 30, 0, 0, 80, 80, 100);
header('Content-Type: image/png');
// and finally, output the result
imagepng($image);
imagedestroy($image);
Related
So I'm trying to take two large images (but later I'll be combinging 6 images in total), resize them to the x, y width, height I have taken from photoshop, and combine them into one 460 x 230 sized image.
This is the code I'm using
<?php
$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png');
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagealphablending($src, false);
imagesavealpha($src, true);
//imagescale($dest, 396, 161.92);
$some = imagecreate(460, 230);
$dest2 = resize($dest, 396, 162);
$src2 = resize($src, 79.19, 79.19);
//imagecopyresized($dest, $dest, 0, 0, 0, 0, 396, 161.92, 1098, 449);
imagecopyresized($src, $src, 10, 10, 0, 0, 79.19, 79.19, 256, 256);
//$img2 = imagecopymerge($dest, $src, 0, 0, 0, 0, 256, 256, 100); //have to play with these numbers for it to work for you, etc.
imagecopymerge($dest2, $src2, 0, 0, 0, 0, 460, 230, 50);
header('Content-Type: image/png');
imagepng($dest, 'merged2.png');
imagepng($dest2);
//file_put_contents('merged.png', $contents);
imagedestroy($dest);
imagedestroy($src);
imagedestroy($some);
imagedestroy($dest2);
imagedestroy($src2);
imagedestroy($img2);
//imagedestroy($then);
function resize($img, $width, $height, $stretch = false)
{
$temp = imagecreatetruecolor($width, $height);
imagealphablending($temp, true);
imagesavealpha($temp, true);
$bg = imagecolorallocatealpha($temp, 0, 0, 0, 0); // Background color
imagefill($temp, 0, 0, $bg);
if ($stretch)
{
imagecopyresampled($temp, img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
}
else
{
if (imagesx($img) <= $width && imagesy($img) <= $height)
{
$fwidth = imagesx($img);
$fheight = imagesy($img);
}
else
{
$wscale = $width / imagesx($img);
$hscale = $height / imagesy($img);
$scale = min($wscale, $hscale);
$fwidth = $scale * imagesx($img);
$fheight = $scale * imagesy($img);
}
imagecopyresampled($temp,
$img,
($width - $fwidth) / 2, ($height - $fheight) / 2,
0, 0,
$fwidth, $fheight,
imagesx($img), imagesy($img)
);
}
return $temp;
}
The issue is that the image rendered is very faded
because of this line:
imagecopymerge($dest2, $src2, 0, 0, 0, 0, 460, 230, 50);
If I change the 50, which is the PCT value to 100, it shows one image with a black background (masking the other image), but if I change it to 0, it shows only the other image with a black background (masking the other image)
If the value is either 0 or 100, the image shown is at its full color though. How do I merge these 2 images together while preserving their transparency and vibrancy of color?
Instead of imagecopymerge use imagecopy. You also always need to correctly specify the dimensions of the source image when copying:
$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png');
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png');
$dest2 = resize($dest, 396, 162);
imagedestroy($dest);
$src2 = resize($src, 79, 79); // should be int not float.
imagedestroy($src);
// the last 2 params must match the width/height of the $src2 image.
imagecopy($dest2, $src2, 0, 0, 0, 0, 79, 79);
imagedestroy($src2);
header('Content-Type: image/png');
imagepng($dest2);
imagedestroy($dest2);
You don't need to change the alpha settings on $dest or $src because they aren't being rendered - you render the new image resource created in, and returned by, your resize function. Because of this you do need to slightly change the function:
function resize($img, $width, $height, $stretch = false)
{
$temp = imagecreatetruecolor($width, $height);
imagealphablending($temp, false); // changed to false.
imagesavealpha($temp, true);
...
Edit:
You might be better off simply using the imagescale function instead of using your own resize function:
$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png');
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png');
$dest2 = imagescale($dest, 396);
imagealphablending($dest2, false);
imagesavealpha($dest2, true);
$src2 = imagescale($src, 79);
imagecopy($dest2, $src2, 0, 0, 0, 0, 79, 79);
header('Content-Type: image/png');
imagepng($dest2);
imagedestroy($dest);
imagedestroy($src);
imagedestroy($dest2);
imagedestroy($src2);
I'm trying to create a Image with PHP.
With PNG it works pretty well (The Font Colors works!).
But if I'm using GIF, the Fonts dont get Colors (Always white).
And the GIF Image I use for the Background is not Animated!
This is how it works in PNG (Font Color):
<?php
$zeile1 = #file_get_contents('zeile1.txt');
$zeile1_title = #file_get_contents('zeile1_title.txt');
$image = imagecreatefrompng("bg_text.png");
imagesavealpha($image, true);
imagealphablending($image, true);
$finalImage = imagecreatetruecolor(391,300);
$font = '../banner1/arial.ttf';
$color = imagecolorallocate($finalImage, 0, 0, 0);
$font_title = '../banner1/arialbd.ttf';
$color_title = imagecolorallocate($finalImage, 255, 0, 126);
imagettftext($image, 15, 0, 2, 130, $color, $font, $zeile1);
imagettftext($image, 25, 0, 2, 130, $color_title, $font_title, $zeile1_title);
header('Content-type: image/png');
imagepng($image);
?>
But now I want a animated gif as background:
<?php
$zeile1 = #file_get_contents('zeile1.txt');
$zeile1_title = #file_get_contents('zeile1_title.txt');
$image = imagecreatefromgif("bg_text.gif");
imagesavealpha($image, true);
imagealphablending($image, true);
$finalImage = imagecreatetruecolor(391,300);
$font = '../banner1/arial.ttf';
$color = imagecolorallocate($finalImage, 0, 0, 0);
$font_title = '../banner1/arialbd.ttf';
$color_title = imagecolorallocate($finalImage, 255, 0, 126);
imagettftext($image, 15, 0, 2, 130, $color, $font, $zeile1);
imagettftext($image, 25, 0, 2, 130, $color_title, $font_title, $zeile1_title);
header('Content-type: image/gif');
imagegif($image);
?>
Now the Text is only white and the Title text, is not Showing + The image is not animated.
I hope you understand what I mean, because my english is bad :/
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?
Is there any reason why imagecolorallocatealpha() would only be making the text grey?
<?php
header('Content-Type: image/png');
function checkImg($imgname) {
$im = #imagecreatefrompng($imgname);
if(!$im) {
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
$hr = 48;
$tOne = "VALID FOR";
$tTwo = $hr." HOURS";
$img = checkImg('img.png');
$font = 'helr67w.ttf';
$size = 9;
$red = imagecolorallocatealpha($img, 255, 0, 0, 75);
imagettftext($img, $size, 0, 225, 132, $red, $font, $tOne);
imagettftext($img, $size, 0, 225, 144, $red, $font, $tTwo);
imagepng($img);
imagedestroy($img);
?>
In your code you don't set the image to support an alpha channel. I can imagine that is causing the issue:
function checkImg($imgname) {
$im = #imagecreatefrompng($imgname);
if(!$im) {
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
// Turn off alpha blending and set alpha flag
imagealphablending($im, true);
imagesavealpha($im, true);
return $im;
}
See imagesavealpha PHP Manual and imagealphablending PHP Manual
I just can't tell why this:
<?php
$image = imagecreatefromjpeg($_GET['u']);
imagealphablending($image, true);
imagesavealpha($image,true);
$overlay = imagecreatefrompng("overlay.png");
imagealphablending($overlay, true);
imagesavealpha($overlay,true);
$finalImage = imagecreate(85,85);
imagealphablending($finalImage, true);
imagesavealpha($finalImage,true);
$trans = imagecolorallocate($finalImage,255,0,0);
imagecolortransparent($finalImage,$trans);
imagefill($finalImage, 0, 0, $trans);
imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
imageDestroy($image);
imageDestroy($overlay);
// Content type
header('Content-type: image/png');
imagepng($finalImage);
imagedestroy($finalImage);
?>
Produces this:
alt text http://alanjack.co.uk/travel/0rotatedImage.php%20(1).png
When doing imagecopy one or the other produces healthy results:
imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
//imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
//ImageDestroy($image);
//ImageDestroy($overlay);
giving:
alt text http://alanjack.co.uk/travel/1rotatedImage.php%20(1).png
and
//imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
//ImageDestroy($image);
//ImageDestroy($overlay);
giving:
alt text http://alanjack.co.uk/travel/2rotatedImage.php%20(1).png
Could it be some kind of palette inconsistency or something - something to do with one being a PNG and another a JPEG?
Grrrrrrrr ... Alan angry ... ALAN WANT SMASH!!!
Try this code instead:
<?php
$image = imagecreatefromjpeg($_GET['u']);
imagesavealpha($image, true);
imagealphablending($image, true);
$overlay = imagecreatefrompng("overlay.png");
imagesavealpha($overlay, true);
imagealphablending($overlay, true);
$finalImage = imagecreatetruecolor(85,85);
imagefill($finalImage, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($finalImage, true);
imagealphablending($finalImage, true);
/*
$trans = imagecolorallocatealpha($finalImage, 255, 0, 0, 127);
imagecolortransparent($finalImage, $trans);
imagefill($finalImage, 0, 0, $trans);
*/
imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
//imageDestroy($image);
//imageDestroy($overlay);
// Content type
header('Content-type: image/png');
imagepng($finalImage);
//imagedestroy($finalImage);
?>
Does it solve your problem?
The grey rectangle went away when I changed imagecreate() to imagecreatetruecolor(), so I think it was a palette issue after all!
Thanks anyway guys.