-2

I want to convert a php string to an image. I use this code

header("Content-type: image/png");
$string = '.the_title().';

$font  = 5;
$width  = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);

$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);

imagestring ($image,$font,0,0,$string,$black);

imagepng ($image);
imagedestroy($image)    

but its shows the_title as text instead of executing the string

2
  • Possible duplicate of php string to image imagecreatefromstring fault Commented Oct 20, 2015 at 11:02
  • its not duplicated read the complete post its about a base64 string Commented Oct 20, 2015 at 11:13

2 Answers 2

5

Use imagecreatefromstring

$string = '.the_title().';

$data = base64_decode($string);

$im = imagecreatefromstring($data);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
Sign up to request clarification or add additional context in comments.

4 Comments

doesnt display anything
@ninju you just copy / paste the php sample without testing…
i didn't test it.it's just for reference
refrence for what? this isnt working with a php string..its works with a base64 encoded string but that was not my question
1

Use imagestring like:

<?php

$string = the_title();

$im = imagecreate(150, 20); // image size 150x20px
imagecolorallocate($im, 255, 255, 255); // background white
$text_color = imagecolorallocate($im, 0, 0, 0); // text color black

imagestring($im, 3, 5, 5, $string, $text_color); // append string to image

header('Content-type: image/png'); // filetype
imagepng($im, 'image.png'); // save as image.png
imagedestroy($im); // free up memory

7 Comments

Search if gd is present in your phpinfo(); See stackoverflow.com/questions/26161655/…
gd is enabled your code just isnt working. im able to use "my code" above with a normal text string (returns image)
I try it with the string "test" and it's works. So with the string 'the_title()'; it's works.
maybe there is a typo anywhere. i just tried the "imagestring code" from php.net and replace the text string with $string. this works but just returns the_title() instead of executing the php string
the image creation was also working with my code thats not my problem. my problem is that the_title() should return the title of posts. thats not happening the image which get created just has the text the_title() in it instead of the post name
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.