How to export data table into an image method 2

I observed that my first post wasn’t that tidy and the code was a little complicated and so I got a new code. It makes conversion of text into an image very easy. This one is simpler and you can print line spaces as well as let you choose the font for your text.

In your table controller

I have used my table Categorytable and my column name at which the text is stored is patient_name




public function actionimage()

{


$names = Categorytable::model()->findAll(array(

));

echo '<ol>';


$NameCounter=1;


$font1 ='/home/woi/arial.ttf';

//path to the font you want to use 


foreach($names as $name)

{


$string= $name->patient_name;

//gets the data from table


$font=strlen($string);

     $width  = ImageFontWidth($font)*strlen($string);

     $height = ImageFontHeight($font)*strlen($string);

     $im = @imagecreatetruecolor ($width,$height);


     $background_color = imagecolorallocate ($im, 255, 255, 255);

  

   $text_color = imagecolorallocate ($im, 188, 188, 188);


   imagettftext ( $im ,$font , 1 , 1 ,  70 , $text_color,$font1, $string );  


imagejpeg($im,'/home/woi/'.$NameCounter.'i.jpeg');


$NameCounter++;


imagedestroy($im);


}




echo '</ol>';

}




Add the following to your index.php in your view folder of your table




  array('label'=>'Take An Image','url'=>array('image')),



And change your controller access rules by adding




array('allow',

'actions' =>array('admin','image'),

'users' =>array('admin'),

),



Now if you don’t have line breaks in your text I would recommend you replace the imagettftext command by the following line




imagestring ($im, $font, 0, 0,  $string, $text_color);



and change the font size by replacing $font with the size you want

Let me know if you encounter any problem and leave a thank you if you like it ;D