Advertisement
imoda

2 letter icon image creation

Apr 6th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2.    
  3.     class Image {
  4.        
  5.         function __construct() {
  6.        
  7.             $this->fontSize = mt_rand(12, 48);
  8.             $this->rotationDegree = mt_rand(0, 360);
  9.             $this->font = 'http://ireader.googlecode.com/files/arial.ttf';
  10.         }
  11.        
  12.         public function set($var, $val) {
  13.            
  14.             $this->$var = $val;
  15.         }
  16.        
  17.         public function backgroundImage($src) {
  18.            
  19.             $this->source = imagecreatefrompng($src);
  20.             $this->width = imagesx($this->source);
  21.             $this->height = imagesy($this->source);
  22.         }
  23.        
  24.         public function color($hex) {
  25.            
  26.             if ($hex[0] == '#') {
  27.                
  28.                 $hex = substr($hex, 1);
  29.             }
  30.            
  31.             if (strlen($hex) == 6) {
  32.                
  33.                 list($r, $g, $b) = array($hex[0].$hex[1],
  34.                                          $hex[2].$hex[3],
  35.                                          $hex[4].$hex[5]);
  36.                                          
  37.             }
  38.             else if (strlen($color) == 3) {
  39.                
  40.                 list($r, $g, $b) = array($hex[0].$hex[0], $hex[1].$hex[1], $hex[2].$hex[2]);
  41.             }
  42.             else {
  43.                
  44.                 return false;
  45.             }
  46.            
  47.             $this->fontColor['r'] = hexdec($r);
  48.             $this->fontColor['g'] = hexdec($g);
  49.             $this->fontColor['b'] = hexdec($b);
  50.         }
  51.        
  52.         public function text($text, $maxLength = false) {
  53.            
  54.             $this->text = ucfirst(strtolower(($maxLength !== false ? (strlen($text) <= $maxLength ? $text : substr($text, 0, $maxLength)) : $text)));
  55.         }
  56.        
  57.         public function output() {
  58.            
  59.             $newImage = imagecreatetruecolor($this->width, $this->height);
  60.             imagealphablending($newImage, false);
  61.             imagesavealpha($newImage, true);
  62.             $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
  63.             imagefilledrectangle($newImage, 0, 0, $this->width, $this->height, $transparent);
  64.             imagecopyresampled($newImage, $this->source, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
  65.             imagealphablending($newImage, true);
  66.    
  67.             $xbox = imagettfbbox($this->fontSize, $this->rotationDegree, $this->font, $this->text);
  68.             $x = ($this->width / 2) - (((($xbox[2] - $xbox[0]) + ($xbox[4] - $xbox[6])) / 2) / 2);
  69.            
  70.             $ybox = imagettfbbox($this->fontSize, $this->rotationDegree, $this->font, substr($this->text, 0, 1));
  71.             $y = ($this->height / 2) - (((($ybox[3] - $ybox[5]) + ($ybox[1] - $ybox[7])) / 2) / 2) + ($ybox[3] - $ybox[5]);
  72.    
  73.             $color = imagecolorallocate($newImage, $this->fontColor['r'], $this->fontColor['g'], $this->fontColor['b']);
  74.            
  75.             imagettftext($newImage, $this->fontSize, $this->rotationDegree, $x, $y, $color, $this->font, $this->text);
  76.            
  77.             imagepng($newImage);
  78.            
  79.             imagedestroy($newImage);
  80.         }
  81.     }
  82.    
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement