String to image only produces a black background - php

I'm really having a problem finding out how to fix this. I cannot seem to change the background from black. How is it possible?
$string = "foo";
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = #imagecreatetruecolor ($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $string, $textcolor);
imagegif($im, 'somefile.gif', 8);
imagedestroy($im);

Use:
imagefill($im, 0, 0, $bg);

Incorporating user279470's answer:
$string = "foo";
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = #imagecreatetruecolor ($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $string, $textcolor);
imagepng($im, 'somefile.png', 8);
imagedestroy($im);

Related

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 - fit text on text-to-image

I have this code
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing... a very long text';
// Replace path by your own font path
$font = 'arial.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()
imagepng($im);
imagedestroy($im);
?>
the problem here is, when I have a very long text, the other won't show.
I've searched that it can be done by
imagettfbbox()
but I don't know how to apply it here.
Any help?
try this code:
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
// Create the image
$im = imagecreatetruecolor($img_width, $img_height);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);
// The text to draw
$text = 'Testing... a very long text bla.. ';
// Replace path by your own font path
$font = 'arial.ttf';
$fontSize = $img_width / strlen($text) * 1.8;
// Add some shadow to the text
imagettftext($im, $fontSize, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, $fontSize, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
New update code :
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
$font = 'arial.ttf';
// Create the image
$text = 'Testing... a very long text.. Testing... a very long text.. Testing... a very long text.. Testing... a very long text..';
$curTextLen = strlen($text);
$limit = 35;
$totalLine = ceil($curTextLen / $limit);
$img_height = $img_height * $totalLine;
$im = imagecreatetruecolor($img_width, $img_height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);
for($i = 1; $i <= $totalLine; $i++){
$y = $i * 27;
$textN = substr($text,($limit * ($i-1)),$limit);
imagettftext($im, 20, 0, 11, $y, $grey, $font, $textN);
// Add the text
imagettftext($im, 20, 0, 10, $y, $black, $font, $textN);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

Gd Library Image Style imagettftext

How to make Like This Image?
I just want to learn how do i get output like this using GD library,
Thanks for your help guys..
Here is your code, you could easily find it on google tho.
<?php
header ("Content-type: image/png");
$string = "your text"; // Change this text
$font = 4; // try changing this as well
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
?>
Here's an example:
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
imagepng($image); // outputting the image
For more see imagettftext().
EDIT: An example of using multiple imagettftext():
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text
$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text
imagepng($image); // outputting the image

Create transparent png with text from scratch in php

All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?
The code ive got so far follows (but it doesnt work. just outputs a blank image source)
<?php
$width = 150;
$height = 30;
$text = "My Text";
$fontsize = 5;
$im = imagecreate($width, $height);
$transcolor = imagecolortransparent($im);
imagestring($im, $fontsize, 0, 0, $text, $transcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
<?php
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Use imagestring instead of imagettftext if you don't want custom font.
Here's the solution based on your original code.
<?php
$width = 640;
$height = 480;
$text = "My Text";
$fontsize = 5;
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
I Think GD is one of the most popular for generating images, with imagettftext.
<?php
$text = 'SOME TEXT';
$font="c:/windows/Fonts/Latinwd.ttf"; //Load font file for windows
$im = ImageCreate(700, 140);
$bla = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bla); //transparent background
$black = imagecolorallocate($im, 255,255,255);
ImageTTFText ($im, 38, 0, 10, 40, $black, $font, $text);
header('Content-Type: image/png');
ImagePNG($im, 'name.png');
imagedestroy($im);
?>

PHP GD imagecolorallocatealpha only makes grey text

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

Categories