PHP GD imagecolorallocatealpha only makes grey text - php

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

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

GD library arc smoothness

I've tried the above code for creating arc with transparent white background, but it is always shown in distorted manner. Please help me how to make them smooth curved one's?
$im = imagecreatetruecolor(300, 300);
$backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $backgroundColor);
$white = imagecolorallocate($im, 255,255,255);
imagecolortransparent($im, $white);
// Allocate A Color For The Text
$dark_blue = imagecolorallocate($im, 30, 65, 71);
imagesetthickness($im, 2);
//creation of thick arc
$thickness = 40;
for ($i = $thickness; $i > 0; $i--) {
imagearc($im, 150, 220, 180 - $i, 180 - $i, 180, 0, $dark_blue);
}
Increase the image size and the arc thickness, then gradually the arc smoothness keeps getting antialiased. I'm adding my code below for half ring/half donut image/half circle :
enter image description here
function scoreHalfDoughnutImage()
{
$score = 70;
$maxScore = 100;
$color = '#FC950C';
$imagePath = "doughnutContent2Image_".$score.".jpg";
// Create Image From Existing File
$im = imagecreatetruecolor(550, 340);// Create a 550x340 image
//imagealphablending($img,true);
$backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);//blending colors
imagefill($im, 0, 0, $backgroundColor);//creating white background
// Switch antialiasing on for one image
$white = imagecolorallocate($im, 255,255,255);
$black = imagecolorallocate($im, 0, 0, 0);
// for making transparent bg of white bg
imagecolortransparent($im, $white);
// Allocate A Color For The Text
$orange = imagecolorallocate($im, 252, 149, 12);
$dark_blue = imagecolorallocate($im, 30, 65, 71);
imagesetthickness($im, 4);
//creation of thick arc
$thickness = 40;
for ($i = $thickness; $i > 0; $i--) {
imagearc($im, 275, 273, (180 - $i)*3, (180 - $i)*3, 180, 0, $dark_blue);
imagearc($im, 275, 273, (180 - $i)*3, (180 - $i)*3, 180, 180+($score*1.8), $orange);
}
//imageantialias($im, true);
imagesetthickness($im, 1);
$text='NA';
// Setup the title on the center circle
if($score>=0)
$text=$score;
// Set Path to Font File
$font_path = 'lib/reports/MotivationEvaluation/font/arial.ttf';
// Print Text On Image
$textWidth = imagettfbbox(40, 0, $font_path, $text);
$x = 235;
if($textWidth[2] < 84)
{
$x = 250;
}
if($text!=='NA')
{
imagettftext($im, 45, 0, $x, 240, $dark_blue, $font_path, $text);
}
else
{
imagettftext($im, 35, 0, $x, 240, $dark_blue, $font_path, $text);
}
// Print Text On x-axis n y-axis
imagettftext($im, 32, 0, 25, 315, $dark_blue, $font_path, 0);
imagettftext($im, 32, 0, 25+450, 315, $dark_blue, $font_path, 100);
//imagealphablending($im, true); //not needed as we created the image
with alpha
imagesavealpha($im, true);
// Send Image to Browser
imagejpeg($im,$imagePath);
imagedestroy($im);
return $imagePath;
}

imagettftext not showing numbers only letters

this is my code:
header('Content-Type: image/jpeg');
$image_width = 400;
$image_height = 30;
$im = imagecreatetruecolor($image_width, $image_height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = utf8_encode("test 123 abc");
$font = 'myfont.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagejpeg($im);
imagedestroy($im);
can anyone help me find where i am wrong.

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

Making captcha image in PHP

I'm trying to use the GD graphics library to make this captcha image, and the box shows up how Id like it to, but the text isn't being displayed over the image at all. I can't figure out why it's not showing up.
<?php
session_start();
putenv('GDFONTPATH=' . realpath('.') );
$font = 'Molle';
header('Content-Type: image/png');
$im = imagecreatetruecolor(260, 40);
$white = imagecolorallocate($im, 255, 255,255);
$grey = imagecolorallocate($im, 215, 215, 215);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 3, 3, 255, 34, $grey);
$text = 'Testing';
$_SESSION["captcha"] = $text;
imagettftext( $im, 20, 0, 16, 26, $white, $font, $text );
imagettftext( $im, 20, 0, 15, 25, $black, $font, $text );
imagepng($im);
imagedestroy($im);
?>

Categories