Jump to content

Text to Image Conversion

Recommended Posts

Text to image conversion in php

A string from db have to be converted into image but it is not working.

If the string is not taken from the db and defined in the file itself text is getting converted into image properly

Can anyone help to sort it out  

Attaching the code used for the same

 Thanks in advance

$res   = mysql_query("select * from imgtext")or die(mysql_error());
$arr   = mysql_fetch_array($res);
$data  = $arr['data'];
$textval =  str_replace('\r\n',"<br>",$data);
$textcolor = '666666';

//$string =  preg_replace( "/\r|\n/", "", $textval);

//echo $string;die;
// $font="fonts";
$size = 9;
$padding= 1;
$bgcolor= "ffffff";

$transparent = 0;
$antialias = 0;

$fontfile = $font;

$box= imageftbbox( $size, 0, $fontfile, $textval, array());
$boxwidth= $box[4];
$boxheight= abs($box[3]) + abs($box[5]);
$width= $boxwidth + ($padding*2) + 1;
$height= $boxheight + ($padding) + 0;
$textx= $padding;
$texty= ($boxheight - abs($box[3])) + $padding;

// create the image
$png= imagecreate($width, $height);
$color = str_replace("#","",$bgcolor);
$red = hexdec(substr($bgcolor,0,2));
$green = hexdec(substr($bgcolor,2,2));
$blue = hexdec(substr($bgcolor,4,2));
$bg = imagecolorallocate($png, $red, $green, $blue);
$color = str_replace("#","",$textcolor);
$red = hexdec(substr($textcolor,0,2));
$green = hexdec(substr($textcolor,2,2));
$blue = hexdec(substr($textcolor,4,2));
$tx = imagecolorallocate($png, $red, $green, $blue);
//print_r(imagettftext( $png, $size, 0, $textx, $texty, $tx, $fontfile, $textval ));
imagettftext( $png, $size, 0, $textx, $texty, $tx, $fontfile, $textval );
header("content-type: image/jpeg");



Edited by HILU RAJU
Link to post
Share on other sites

If you're writing a script that doesn't produce text output, you should use an error log to check for error messages.  You can change that at runtime like this:

ini_set('display_errors', 0);
	ini_set('log_errors', 1);
	ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');

That error.log file should then contain any error messages, as long as PHP has permission to write to it.  Start there and see what you get in the log.  You should also change that mysql extension to use PDO or mysqli, that isn't going to work in PHP 7.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...