Page 1 of 1

Creating a Dynamic image in PHP Rate Topic: ***** 1 Votes

#1 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Posted 08 June 2008 - 11:59 AM

There are times when a programmer needs a Dymanic image, or one that can change depending on the data given. For instance, Sales is one of them, Stocks are another, but any way you put it, Dynamic images are the way to go for a web designer.

Following this tutorial, you will create a very simple image with dynamic data. Now, Onto the tutorial!

the first part of this tutorial is about creating the image. Now, if you've Never used the GD library, I suggest you go and read the php.net page located here, but it is not necessary to understand this tutorial.

To start off, we will be creating a simple image like the one found here. Now, if you check the url, you will see that it end in ".php". This specific image gets the data from a database I have open and adds in the relevant data.

we will start off with making the table, and inputting some Data.
CREATE TABLE `dic_image` (
`object` VARCHAR( 32 ) NOT NULL ,
`week1` SMALLINT( 3 ) NOT NULL ,
`week2` SMALLINT( 3 ) NOT NULL ,
`week3` SMALLINT( 3 ) NOT NULL ,
`week4` SMALLINT( 3 ) NOT NULL ,
`week5` SMALLINT( 3 ) NOT NULL ,
`week6` SMALLINT( 3 ) NOT NULL ,
`week7` SMALLINT( 3 ) NOT NULL
) ENGINE = MYISAM;



this table will store an object name and the amount we have over a period of 4 weeks. now that the table is set up correctly, Lets add some data to it.
INSERT INTO `dic_image` (
`object` ,
`week1` ,
`week2` ,
`week3` ,
`week4` ,
`week5` ,
`week6` ,
`week7`
)
VALUES (
'Banana', '70', '60', '50', '40', '30', '20', '10'
);


Now we have Banana in there with values.

now, we start making the image! create a new PHP file and put this into it:
<?php
header("Content-type: image/jpeg");
$localhost = "X";
$mysqlusername  = "X";
$mysqlpassword  = "X";
$db = "JX";
$con = mysql_connect($localhost, $mysqlusername, $mysqlpassword);
mysql_select_db("$db", $con);
?>


this is the simple Database connection procedures, and needs to be done. Just replace the X's with your pertinent data.

now, the next section is to gather the data from the database and store it into an array, as well as getting the total from the rows and adding them up.
<?php
$sql = mysql_query("SELECT COUNT(*) AS total,object,week1,week2,week3,week4,week5,week6,week7 FROM dic_image GROUP BY object")or die(mysql_error());
$row = mysql_fetch_array($sql);
$sum = $row[week1]+$row[week2]+$row[week3]+$row[week4]+$row[week5]+$row[week6]+$row[week7];
?>


now that the simple stuff is done, Lets create the image!

<?php
$im = imagecreate(320,255);
$white = imagecolorallocate($im,255,255,255); 
$black = imagecolorallocate($im,0,0,0);   
$red = imagecolorallocate($im,255,0,0);
imageline($im, 10, 5, 10, 230, $black);
imageline($im, 10, 230, 300, 230, $black);
?>


now, onto the even harder things.

<?php
        $x = 15;   
        $y = 230;   
        $x_width = 20;  
        $y_ht = 0;
        
        for ($i=1;$i<8;$i++){
        
          $y_ht = ($row[week.$i]/$sum)* 255;    
          
              imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
              imagestring( $im,2,$x-1,$y+10,$row[week.$i],$black);
              
          $x += ($x_width+20);  
         
        }
?>


this code uses a for loop to place the data and form the rectangles of the correct size and space.

lastly, we need to show the image!
<?php
        imagejpeg($im);  
?> 



Congratulations, you've just completed your first bar graph in PHP!

Final Code:
<?php
header("Content-type: image/jpeg");
$localhost = "X";
$mysqlusername  = "X";
$mysqlpassword  = "X";
$db = "X";
$con = mysql_connect($localhost, $mysqlusername, $mysqlpassword);
mysql_select_db("$db", $con);



$sql = mysql_query("SELECT COUNT(*) AS total,object,week1,week2,week3,week4,week5,week6,week7 FROM dic_image GROUP BY object")or die(mysql_error());
$row = mysql_fetch_array($sql);
$sum = $row[week1]+$row[week2]+$row[week3]+$row[week4]+$row[week5]+$row[week6]+$row[week7];

           
$im = imagecreate(320,255);
$white = imagecolorallocate($im,255,255,255); 
$black = imagecolorallocate($im,0,0,0);   
$red = imagecolorallocate($im,255,0,0);
imageline($im, 10, 5, 10, 230, $black);
imageline($im, 10, 230, 300, 230, $black);
    

        $x = 15;   
        $y = 230;   
        $x_width = 20;  
        $y_ht = 0;
        
        for ($i=1;$i<8;$i++){
        
          $y_ht = ($row[week.$i]/$sum)* 255;    
          
              imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
              imagestring( $im,2,$x-1,$y+10,$row[week.$i],$black);
              
          $x += ($x_width+20);  
         
        }
        
        imagejpeg($im);

?>



Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Dynamic image in PHP

#2 Muphet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Posted 19 May 2012 - 11:06 AM

Short version for this tutorial:

<?php

Header ("Content-type: image/jpeg"); 
$img_handle = imageCreateFromJPEG("image.jpg");
$color = ImageColorAllocate ($img_handle, 100, 100, 100);
$str = "This is a sample string.";
ImageString ($img_handle, 3, 10, 9,  "$str", $color); 
ImagePng ($img_handle); 
ImageDestroy ($img_handle); 

?>



You can easly change type of image to PNG/GIF etc. and add text using variables. Remember to add connection to your db if you want to fetch data from database.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1