Adding alpha channels to PHP GD Images - php

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:

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

Imagettftext not producing correct results if I use the tamil lanuage ttf?

I am trying to write Tamil text on an image. I tried with different font files like latha.ttf and
Uni Ila.Sundaram-01.ttf files. The problem is that the vowels are getting misplaced. Please see the image for reference. Can anyone please suggest to me how to overcome this issue?
I use the code below:
<?php
header('Content-type: image/png; charset=UTF-8');
$text ='சுற்றிப்பார்க்க ';
$font = './fonts/UniIlla.ttf';
$im = imagecreatetruecolor(600, 300);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 599, 299, $bg_color);
imagettftext($im, 20, 0, 10, 50, $font_color, $font, $font);
imagettftext($im, 20, 0, 10, 120, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
?>

php how add transparent background color for php image?

background is comming in white i want to remove white and put transparent.please help
this is an image: http://funbusy.com/fbtest/user_image.php
?php
// Create the image
$im = imagecreatetruecolor(400, 25);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 22, 125, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = ucwords('tanuja sree');
// Replace path by your own font path
$font = 'OpenSans-Italic.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
ob_start();
imagepng($im);
$image_data = ob_get_clean();
$image_code = '<img id="image_code" src="data:image/png;base64,'. base64_encode($image_data) . '">';
imagedestroy($im);
?>
Try adding this piece of code after imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagecolortransparent($im, $white);
imagesavealpha($im,true);
imagealphablending($im, true);

How to show image that was edited with PHP

I have an image which can show polylines. I can show the image perfectly but when i apply into website which not using header("Content-type: image/png"); i got problem.
Can i show the image that has been edited with PHP in website not just image.
here's my code
<?php
$img = imagecreatefromjpeg('1967_NL_MAJENEresize.jpg'); //you can change image name
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
imagesetthickness($img, 4);
imagefill($img, 0, 0, $white);
imageline($img, 100, 80, 210, 380, $white); //x1, y1, x2, y2
imageline($img, 200, 80, 210, 380, $white);
imageline($img, 200, 80, 310, 580, $white);
imagefilledrectangle ($img, (100-5), (80-5), 100+10, 80+10, $red);
imagefilledrectangle ($img, (210-5), (380-5), 210+10, 380+10, $red);
imagefilledrectangle ($img, (200-5), (80-5), 200+10, 80+10, $red);
imagefilledrectangle ($img, (310-5), (580-5), 310+10, 580+10, $red);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
You shouldn't include the image generating code in existing content, instead have a dedicated script to create the image using the correct header.
Link to the image from your content by using <img> tag
generate-image.php:
<?php
$imagefile = preg_replace('/[^A-Za-z0-9_\-]/', '_', $_GET['filename']);
$img = imagecreatefromjpeg($imgfile); //you can change image name
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
imagesetthickness($img, 4);
imagefill($img, 0, 0, $white);
imageline($img, 100, 80, 210, 380, $white); //x1, y1, x2, y2
imageline($img, 200, 80, 210, 380, $white);
imageline($img, 200, 80, 310, 580, $white);
imagefilledrectangle ($img, (100-5), (80-5), 100+10, 80+10, $red);
imagefilledrectangle ($img, (210-5), (380-5), 210+10, 380+10, $red);
imagefilledrectangle ($img, (200-5), (80-5), 200+10, 80+10, $red);
imagefilledrectangle ($img, (310-5), (580-5), 310+10, 580+10, $red);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
index.php:
Blah Blah
<img src='generate-image.php?filename=<?= $db->row['thumbnail']; ?>'>;
blah blah

PHP Transparent Image Layering Problems

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.

Categories