Convert Text to Image in PHP

Convert text to image?

I believe libGD is one of the most popular alternatives for generating images (and it has bindings for most languages used in web development).

See the documentation on PHP.net. I guess you are especially interested in imagettftext.

Converting Text to Image using php script

Your code is working (you forgot a closing span and the function does not return anything, but I imagine that's due to debugging).

What is happening is probably that the text you are loading contains whitespace or extra line feeds, and is then rendered outside your PNG.

Try with a larger PNG or try trimming the text with trim().

function getData()
{
...
return trim($items->nodeValue);
}

Test code (working)

...or at least returning an image with something on it I can't read :-)

<?php

function getData(){
$url = "http://www.sandesh.com/article.aspx?newsid=119068";
//$url = "http://www.sandesh.com/article.aspx?newsid=115627";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$DOM = new DOMDocument;
$output = mb_convert_encoding($output, 'HTML-ENTITIES', "UTF-8");
@$DOM->loadHTML($output);
$items = $DOM -> getElementById('lblNews');

return trim($items -> nodeValue);
}

// Set the content-type
//mb_internal_encoding("UTF-8");
// require_once('seven.php');
// Create the image
$im = imagecreatetruecolor(400, 400);

// 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, ImageSX($im), ImageSY($im), $white);

// The text to draw
$text = getData();//'કેમ છો ?';
//echo $text;
// Replace path by your own font path
$font = 'Surya.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()
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

?>

How to convert input text to image?

Yeah for sure.

 <?php foreach(range(1,5)as $rating):?>            
<a href="rate.php?article= <?php echo $article->id; ?> &rating= <?php echo $rating ; ?> ">
<?php echo '<img src="images/rating_'.$rating.'.png">'; ?> </a>
<?php endforeach;?>

Now simply create the folder images and insert rating_1.png, rating_2.png and so on.

BTW: Maybe it is easier and better for you when you use it in this way (only to show):

 <?php
$bla = $article->id;
foreach(range(1,5)as $rating){
echo('
<a href="rate.php?article='.$bla.'&rating='.$rating.'">
<img src="images/rating_'.$rating.'.png">
</a>
');
}
?>

How to convert rich text into an image using PHP?

You can configure PHP and ImageMagick to use Pango which is a type of rich text. I am no expert in Pango, and a lot more is certainly possible - see Pango Gallery for examples.

$img = new Imagick();
$img->setBackgroundColor(new ImagickPixel('yellow'));
$img->setPointSize(72);

$HTML = "<span fgcolor='red' bgcolor='#0000ff'>red</span>\n";
$HTML.= "<b><u>Bold and underlined</u></b>\n";
$HTML.= "<i><s>Italic and struckthrough</s></i>";

$img->newPseudoImage(800,400,"pango:$HTML");
$img->writeImage("result.png");

Sample Image


Keywords: ImageMagick, IMagick, PHP, HTML, markup, RTF, Rich Text Format, formatted text



Related Topics



Leave a reply



Submit