Advertisement
lorro

PHP - Draw an image in memory

Jun 11th, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2.   // PHP - Draw an image in memory
  3.   // reference: http://www.php.net/manual/en/ref.image.php
  4.   $im = imagecreate($width, $height);
  5.  
  6.   // make some colours
  7.   $white = imagecolorallocate($im, 255, 255, 255);  
  8.   $black = imagecolorallocate($im, 0, 0, 0);  
  9.  
  10.   // set a background
  11.   imagefill($im, 0, 0, $white);
  12.  
  13.   $text = 'Something';
  14.   $font_number = 2; // try 2 to start with
  15.   imagestring($im, $font_number, $x, $y, $text, $black);
  16.  
  17.   // we need somewhere to put the image
  18.   $path = '/public_html/temp/'; // a server path to a temporary directory
  19.   // make a unique number to ensure the image is not cached
  20.   $unique = time();
  21.   $url = $path.$unique.'gif';
  22.   // save the image to the server    
  23.   $result = imagegif($im, $url);
  24.  
  25.   // get the http address for the image
  26.   $url = get_site_url().'/temp/'.$unique.'.gif';
  27.   echo '<img src="'.$url.'" />';
  28.  
  29.   // release the memory
  30.   imagedestroy($im);
  31.  
  32.   // NB the temp directory will fill up over time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement