Jump to content

Text to Image Conversion


HILU RAJU

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

<?php
//error_reporting(E_ALL);
include('connect.php');
$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";
$font="DidactGothic-Regular";
$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 ));
//die;
imagettftext( $png, $size, 0, $textx, $texty, $tx, $fontfile, $textval );
header("content-type: image/jpeg");
imagejpeg($png);
imagedestroy($png);
exit;

?>

check.php

Edited by HILU RAJU
Link to comment
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');
	error_reporting(E_ALL);

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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...