Problems with special characters in php imagestring - php

everybody.
my code is
<?php
header("Content-type: image/jpeg");
$imgPath = 'logo.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 000, 000, 255);
$string = "A&B UK LIMITED";
$fontSize = 5;
$x = 19;
$y = 197;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image,"test.jpg", 85);
?>
in code $string variable has a special character and that why my code doesn't work
is there any body know how to fix this problem,
but If i cahange the value of $string variable it does work. Example $string="test Company";

Do it like below
header("Content-type: image/jpeg");
$imgPath = 'http://upload.wikimedia.org/wikipedia/en/3/37/VII-200-bakside.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 0,0, 250);
$string = "A&B UK LIMITED";
$fontSize = 5;
$x = 50;
$y = 50;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image,'test.jpeg',75);
imagejpeg($image);
output:

Related

Display image from imagettftext

I have code to generate an image:
<?php
ini_set('display_errors', 1);
$members = json_decode(file_get_contents('https://discord.com/api/guilds/828207694284193802/widget.json'), true)['members'];
$membersCount = 0;
foreach ($members as $member) {
if ($member['status'] == 'online') {
$membersCount++;
}
}
$img = imagecreatetruecolor(380, 80);
$white = imagecolorallocate($img, 255,255,255);
putenv('GDFONTPATH=' . realpath('.'));
$font = "arial.ttf";
$txt = "ONLINE: " . $membersCount;
$image_width = imagesx($img);
$image_height = imagesy($img);
$text_box = imagettfbbox(30,0, $font,$txt);
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[7]-$text_box[1];
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
imagettftext($img, 30, 0, $x, $y, $white, $font, $txt);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>
And I want to display a result on my forum:
<img src="http://s52518.web101.svpj.pl/banners/counter_dc.php" alt="" />
But the result is:
Or an empty box:
My PHP code is working well:
I want add the image in HTML to my sidebar/box (I don't know how it's named). It's a Discord online users counter.

PHP GD center multiple lines (dynamically text)

Im drawing a dynamically text on a image centersized with GD library.
Whats the best way to align all lines center?
<?php
function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {
for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
$bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}
$image = "12.png";
$font = "./impact.ttf";
$font_size = "50";
$image_2 = imagecreatefrompng($image);
$black = imagecolorallocate($image_2, 255,255,255);
$black2 = imagecolorallocate($image_2, 0,0,0);
$image_width = imagesx($image_2);
$image_height = imagesy($image_2);
$margin = 35;
$text = "This is the Text. It is dynamically long. This text will be split using the function under this text.";
//explode text by words
$text_a = explode(' ', $text);
$text_new = '';
foreach($text_a as $word){
//Create a new text, add the word, and calculate the parameters of the text
$box = imagettfbbox($font_size, 0, $font, $text_new.' '.$word);
//if the line fits to the specified width, then add the word with a space, if not then add word with new line
if($box[2] > $image_width - $margin*2){
$text_new .= "\n".$word;
} else {
$text_new .= " ".$word;
}
}
$text_box = imagettfbbox($font_size,0,$font,$text_new);
$text_width = $text_box[2]-$text_box[0]; // lower right corner - lower left corner
$text_height = $text_box[3]-$text_box[1];
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
$font_color = imagecolorallocate($image_2, 255, 255, 255);
$font_color2 = imagecolorallocate($image_2, 0, 0, 0);
$stroke_color = imagecolorallocate($image_2, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$grey = imagecolorallocate($im, 175, 175, 175);
imagettfstroketext($image_2, $font_size ,0,$x,$y,$font_color, $stroke_color, $font, $text_new, 0);
header ("Content-type: image/png");
imagejpeg($image_2, "../img/output.png");
imagedestroy($image_2);
?>
This is what it looks right now:
This is what it should look like:
Using three times "imagettfstroketext" with adding "$y+50" would do the trick but the text is dynamic.
Any suggestions?
best regards
I would create a temporary image on one line, storing the accumulated length in an array. Then determine exactly which words fit on a line, and the offset to center each line. Then create the image.
Alternatively you can create separate images for each line, then combine them with the right offset.

Write the output of a (shoutcast status) script to an image

I'm trying to write the following output of a (shoutcast status) script to an image using GD but it's not working. What am I doing wrong?
$string = "/home/test.txt";
Using the above just displays the path to the file not it's content.
output:
psytrance.value 37
breaks.value 8
dubstep.value 6
reggae.value 130
oldskool.value 5
ambient.value 81
test.value <- this should be ignored!
complete.value 267
php:
<?php
header ("Content-type: image/png");
$string = "/home/test.txt";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/home/banner2.png");
$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);
?>
The shoutcast Status is saved in test.txt? Then you have to write the content of the file into your PNG.
$content = file_get_contents ($string);
[...]
$lines = explode("\n", $content);
foreach ($lines as $line) {
if (strstr("test.value", $line) !== false) continue;
imagestring ($im, $font, $x, $y, $string, $textColor);
$y += 20;
}

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

imagecreatefrompng makes imported image block-ish?

I'm trying to use the following PHP in order to apply randomly generated text files to an image. (I'm just using a random image right now.)
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
$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);
ImageDestroy($im);
?>
However, when I run that code the imported image just becomes very blockish.
Here is the image I am testing with:
http://i.stack.imgur.com/LhNkv.png
And here is how it actually appears:
http://i.stack.imgur.com/AAcHZ.png
My research shows that "imagecreate" generates a "palette" image - and I thought that might have something to do with my error, but I have seen plenty of examples where the base image is by no means distorted.
Thanks in advance for your insights.
(Ugh. It wouldn't let me post images but let me upload them just fine?)
Update
Changing the code to:
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
//$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);
ImageDestroy($im);
?>
Produces the same block-like effects as above, except now no text is being written to the image, either (obviously?).
Could be an alpha blending problem. Try adding these before saving the image:
imagealphablending($im, true);
imagesavealpha($im, true);

Categories