PHP Transparent Image Layering Problems - php

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.

Related

Text is disappearing when I try to add an image in PHP + GD library

I'm trying to create a PNG with some text and a scaled picture in it. Here is the code for just the text, it works fine:
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);
// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");
// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "assets/fonts/arial.ttf", "DOWN DOWN DOWN");
header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>
With the code above you get the following picture, which is correct:
Now I'm trying to have a small picture in it, So I'm loading the picture from a JPEG file (adidas.jpg). Here's the code
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);
// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");
// image
$src = imagecreatefromjpeg("adidas.jpg");
$pic = imagecreatetruecolor(500, 500);
imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
$white = imagecolorallocate($pic, 255, 255, 255);
imagefill($label,0,0,$white);
imagedestroy($pic);
// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "arial.ttf", "DOWN DOWN DOWN");
header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>
And this is what I get:
To my surprise the "down" text disappeared. Why is that? The text added before the picture is fine, the text added after it turns to black for some reason
Your code is bit messy, "DOWN.." text will appear if you remove second:
$color = imagecolorallocate($label, 255, 255, 255);
You dont fill the original image, you try it later but with wrong color ($white is from $pic, not $label).
I cleaned it up:
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
$black = imagecolorallocate($label, 0, 0, 0);
$white = imagecolorallocate($label, 255, 255, 255);
imagefill($label, 0, 0, $black);
imagettftext($label, 50, 0, 0, 150, $white, "arial.ttf", "UP UP UP");
$src = imagecreatefromjpeg("adidas.jpg");
$pic = imagecreatetruecolor(500, 500);
imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
$white2 = imagecolorallocate($pic, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $white, "arial.ttf", "DOWN DOWN DOWN");
ob_end_clean();
header('Content-type: image/png');
imagepng($label);
imagedestroy($src);
imagedestroy($pic);
imagedestroy($label);
die();
?>

PHP imagecreatefromgif animated + color

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 :/

Adding alpha channels to PHP GD Images

I want to create an png image with transparency using the GD functions of PHP.
Specifically, I'm have text with different opacity levels (for anti-aliasing).
Using the following code I am able to create an alpha in the main part of the background:
//Create image
$image = imagecreatetruecolor($width, $height);
//Set background to opaque
imagecolortransparent($image, imagecolorallocate($image, 0, 0, 0));
Though it does properly create an alpha, in area the image has opacity levels that are not 0% or 100%, it makes black.
How can I correctly create the opacity levels of these areas in the image?
Specifically, I'm have text with different opacity levels (for
anti-aliasing)
Using a different opacity level for text doesn't anti-alias it. And there's no reason to do this since GD outputs anti-aliased text anyway.
Example 1: This creates an image with an opaque black background with white text at various opacity levels
// create image resource.
$img = imagecreatetruecolor(250, 200);
// create image colours.
$black = imagecolorallocate($img, 0, 0, 0);
$white_0 = imagecolorallocatealpha($img, 255, 255, 255, 0);
$white_25 = imagecolorallocatealpha($img, 255, 255, 255, 32);
$white_50 = imagecolorallocatealpha($img, 255, 255, 255, 64);
$white_75 = imagecolorallocatealpha($img, 255, 255, 255, 96);
$white_100 = imagecolorallocatealpha($img, 255, 255, 255, 127);
// set background to opaque black.
imagefill($img, 0, 0, $black);
// output text strings.
imagettftext($img, 20, 0, 10, 30, $white_0, 'arial.ttf', '0% transparent');
imagettftext($img, 20, 0, 10, 60, $white_25, 'arial.ttf', '25% transparent');
imagettftext($img, 20, 0, 10, 90, $white_50, 'arial.ttf', '50% transparent');
imagettftext($img, 20, 0, 10, 120, $white_75, 'arial.ttf', '75% transparent');
imagettftext($img, 20, 0, 10, 150, $white_100, 'arial.ttf', '100% transparent'); // not visible!
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
Result 1:
Example 2: If what you want is a transparent background:
// create image resource.
$img = imagecreatetruecolor(250, 200);
// save alpha channel information (for transparent background).
imagealphablending($img, false);
imagesavealpha($img, true);
// create image colours.
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
$black_0 = imagecolorallocatealpha($img, 0, 0, 0, 0);
$black_25 = imagecolorallocatealpha($img, 0, 0, 0, 32);
$black_50 = imagecolorallocatealpha($img, 0, 0, 0, 64);
$black_75 = imagecolorallocatealpha($img, 0, 0, 0, 96);
// set background to transparent.
imagefill($img, 0, 0, $transparent);
// output text strings.
imagettftext($img, 20, 0, 10, 30, $black_0, 'arial.ttf', '0% transparent');
imagettftext($img, 20, 0, 10, 60, $black_25, 'arial.ttf', '25% transparent');
imagettftext($img, 20, 0, 10, 90, $black_50, 'arial.ttf', '50% transparent');
imagettftext($img, 20, 0, 10, 120, $black_75, 'arial.ttf', '75% transparent');
imagettftext($img, 20, 0, 10, 150, $transparent, 'arial.ttf', '100% transparent'); // not visible!
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
Result 2:

cant change font type in dynamic image

cant seem to get the font to work , so far i've seen that the imagestring doesn't support the font being changed so im trying to work with imagettftext like so
imagettftext($finalImage, 20, 0, 11, 21, $textcolorBlue, $font2, $text);
but instead of displaying any text , it just shows the image with all the imagestring's written , also this is my first time working with dynamic images so , im not quite sure what im doing wrong (if any thing) also Neverwinter.ttf is in the same file directory as the image script any help would be appriciated
<?php
$image = imagecreatefrompng("icons.png");
imagesavealpha($image, true);
imagealphablending($image, true);
$size = getimagesize("icons.png");
$finalImage = imagecreatetruecolor(320,125);
$font = imageloadfont('/runescape/phpfile/arial.ttf');
$font2 = imageloadfont('Neverwinter.ttf');
$font = 4;
$textcolorBlack = imagecolorallocate($finalImage, 0, 0, 0);
$textcolorWhite = imagecolorallocate($finalImage, 255, 255, 255);
$textcolorGreen = imagecolorallocate($finalImage, 0, 255, 0);
$textcolorBlue = imagecolorallocate($finalImage, 225,225, 255);
imagecopy($finalImage, $image, 0, 0, 0, 0, 320, 125);
//imagestring($finalImage, $font, 0, 0, 'random', $textcolorBlue);
$text = 'testing...';
imagettftext($finalImage, 20, 0, 11, 21, $textcolorBlue, $font2, $text);
// Content type
header('Content-type: image/png');
imagepng($finalImage);
?>
just change the line
$font2 = imageloadfont('Neverwinter.ttf');
to
$font2 = 'path/Neverwinter.ttf';

PHP imagecopymerge source image's background change transparent to black

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);

Categories