Text to Image Conversion in PHP?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


20 points

Hello,

I am working on a PHP project and I want to generate png images from the text that is generated dynamically from the code. The text has 2 fonts in it. I am trying to use Imagemagick for this. I had earlier used GD for this. I could not find a solution to this problem..

Can someone help?



16 points

Can't you just simply use php functions imagepng, imagettftext and imagecreate? imagettftext takes a font as the input as well.

Anonymous's picture
Created by Anonymous
5 points

You can use the following code:


<?php

header
("Content-type: image/png");
$string "some text here";
$im     imagecreatefrompng("images/button1.png");
$orange imagecolorallocate($im22021060);
$px     = (imagesx($im) - 7.5 strlen($string)) / 2;
imagestring($im3$px9$string$orange);
imagepng($im);
imagedestroy($im);

?>

Anonymous's picture
Created by Anonymous
14 points

Or somthing like this can be helpful too:


<?php
header
("Content-type:image/jpeg");
$array=array("this is text1","this is text2""this is text3");
        function 
imagettftext_cr(&$im$size$angle$x$y$color$fontfile$text)
        {
            
// retrieve boundingbox
            
$bbox imagettfbbox($size$angle$fontfile$text);
            
// calculate deviation
            
$dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;         // deviation left-right
            
$dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;        // deviation top-bottom
            // new pivotpoint
            
$px $x-$dx;
            
$py $y-$dy;
            return 
imagettftext($im$size$angle$px$y$color$fontfile$text);
        }
$image imagecreate(500,90);
$black imagecolorallocate($image,0,0,0);
$grey_shade imagecolorallocate($image,40,40,40);
$white imagecolorallocate($image,255,255,255);


$text $array[rand(0,sizeof($array)-1)];

$otherFont 'army1.ttf';
$font 'army.ttf';

if(
$_GET['name'] == ""){ $name "Sam152";}else{$name$_GET['name'];}
$name substr($name025)    


//BG text for Name
while($i<10){
imagettftext_cr($image,rand(2,40),rand(0,50),rand(10,500),rand(0,200),$grey_shade,$font,$name);
$i++;
}
//BG text for saying
while($i<10){
imagettftext_cr($image,rand(0,40),rand(90,180),rand(100,500),rand(200,500),$grey_shade,$otherFont,$text);
$i++;
}
// Main Text
imagettftext_cr($image,35,0,250,46,$white,$font,$name);
imagettftext_cr($image,10,0,250,76,$white,$otherFont,$text);
imagejpeg($image);

?>

Anonymous's picture
Created by Anonymous
0 points

Consult this for functions: http://au.php.net/manual/en/book.image.php

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.