How to export data table into an image

Hi guys

lately I was trying to export data from mysql into an image. I have got it to work with the following code although with some limitations

here the table is categorytable and the column name is patient_id

add this code into your table controller

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


echo '<ol>';


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

foreach($names as $name)


$string= $name->patient_name;

  $font_size = 14;



  foreach ($ts as $k=>$string) { //compute width



  // Create image width dependant on width of the string

  $width  = imagefontwidth($font_size)*$width;

  // Set height to that of the font

  $height = imagefontheight($font_size)*count($ts);



  // Create the image pallette

  $img = imagecreatetruecolor($width,$height);

  // Dark red background

  $bg = imagecolorallocate($img, 0x00, 0x00, 0x00);

  imagefilledrectangle($img, 0, 0,$width ,$height , $bg);

  // White font color

  $color = imagecolorallocate($img, 255, 255, 255);

  foreach ($ts as $k=>$string) {

    // Length of the string

    $len = strlen($string);

     // Loop through the string


          // Draw character

    imagestring($img, $font_size, 0, 0, $line, $color);




  // Remove image




add this little line to your views where you want the button

I added it to my create.php file in the table folder

array(‘label’=>‘Take An Image’,‘url’=>array(‘index’)),

Don’t forget to change the rules in the controller file


The above code works fine for single line texts but does not identify line breaks.

I am still working on it and any help will be appreciated.

Nice work …keep it up…:)

Thanks Jayant. You should check out my other topic as well. Its a better method.

Your welcome…:)

Can you post a link of your other topic.

Here is the link to the other topic