Advertisement
whoami_cz

Image class

Dec 20th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.52 KB | None | 0 0
  1. <?php
  2. /* © Copyright 2011 - tomas.bara@whoami.cz */
  3. /*
  4.     Please.. check my class and if u know better way.. tell me..
  5. */
  6.  
  7.     class image {
  8.    
  9.         /* ***** */
  10.        
  11.         private $image = false;
  12.         private $imageType = false;
  13.         public $imageTypes = false;
  14.        
  15.         /* ***** */
  16.        
  17.         function __construct (  ) {
  18.        
  19.             $this->imageTypes = array(
  20.                 IMAGETYPE_JPEG => array(
  21.                     'extension' => 'jpg',
  22.                     'contentType' => 'image/jpg',
  23.                     'functionLoad' => 'imagecreatefromjpeg',
  24.                     'functionOutput' => 'imagejpeg',
  25.                 ),
  26.                 IMAGETYPE_GIF => array(
  27.                     'extension' => 'gif',
  28.                     'contentType' => 'image/gif',
  29.                     'functionLoad' => 'imagecreatefromgif',
  30.                     'functionOutput' => 'imagegif',
  31.                 ),
  32.                 IMAGETYPE_PNG => array(
  33.                     'extension' => 'png',
  34.                     'contentType' => 'image/png',
  35.                     'functionLoad' => 'imagecreatefrompng',
  36.                     'functionOutput' => 'imagepng',
  37.                 ),
  38.             );
  39.        
  40.         }
  41.        
  42.         /* ***** */
  43.        
  44.         function isValidImageType ( $imageType = false ) {
  45.             if ( isset($this->imageTypes[($imageType)]) && is_array($this->imageTypes[($imageType)]) ) {
  46.                 if ( isset($this->imageTypes[($imageType)]['extension'], $this->imageTypes[($imageType)]['contentType'], $this->imageTypes[($imageType)]['functionLoad'], $this->imageTypes[($imageType)]['functionOutput']) ) {
  47.                     return true;
  48.                 } else {
  49.                     return false;
  50.                 }
  51.             } else {
  52.                 return false;
  53.             }
  54.         }
  55.        
  56.         function functionLoad ( $imageType = IMAGETYPE_JPEG, $parms = array() ) {
  57.             if ( $this->isValidImageType($imageType) && is_array($parms) ) {
  58.                 return call_user_func_array($this->imageTypes[($imageType)]['functionLoad'], $parms);
  59.             } else {
  60.                 return false;
  61.             }
  62.         }
  63.        
  64.         function functionOutput ( $imageType = IMAGETYPE_JPEG, $parms = array() ) {
  65.             if ( $this->isValidImageType($imageType) && is_array($parms) ) {
  66.                 return call_user_func_array($this->imageTypes[($imageType)]['functionOutput'], $parms);
  67.             } else {
  68.                 return false;
  69.             }
  70.         }
  71.        
  72.         /* ***** */
  73.        
  74.         function load ( $file = false ) {
  75.             if ( isset($file) && !empty($file) && is_string($file) ) {
  76.                 $imageInfo = getimagesize($file);
  77.                 if ( $this->isValidImageType($imageInfo[2]) ) {
  78.                     $image = $this->functionLoad($imageInfo[2], array( $file ));
  79.                     if ( $image !== false ) {
  80.                         $this->imageType = $imageInfo[2];
  81.                         $this->image = $image;
  82.                         return $this;
  83.                     } else {
  84.                         return false;
  85.                     }
  86.                 } else {
  87.                     return false;
  88.                 }
  89.             } else {
  90.                 return false;
  91.             }
  92.         }
  93.        
  94.         function loadString ( $string = false, $imageType = IMAGETYPE_JPEG ) {
  95.             if ( isset($string) && !empty($string) && is_string($string) ) {
  96.                 if ( $this->isValidImageType($imageType) ) {
  97.                     $image = imagecreatefromstring($string);
  98.                     if ( $image !== false ) {
  99.                         $this->imageType = $imageType;
  100.                         $this->image = $image;
  101.                         return $this;
  102.                     } else {
  103.                         return false;
  104.                     }
  105.                 } else {
  106.                     return false;
  107.                 }
  108.             } else {
  109.                 return false;
  110.             }
  111.         }
  112.        
  113.         function loadUri ( $uri = false ) {
  114.             if ( isset($uri) && !empty($uri) && is_string($uri) ) {
  115.                 $uriParts = explode(',', $uri);
  116.                 $contentType = explode(':', $uriParts[0]);
  117.                 $contentType = explode(';', $contentType[1]);
  118.                 $contentType = $contentType[0];
  119.                 $imageSrc = base64_decode($uriParts[1]);
  120.                 foreach ( $this->imageTypes as $key => $val ) {
  121.                     if ( $val['contentType'] == $contentType ) {
  122.                         $imageType = $key;
  123.                         break;
  124.                     }
  125.                 }
  126.                 if ( isset($imageType) && $this->isValidImageType($imageType) ) {
  127.                     return $this->loadString($imageSrc, $imageType);
  128.                 } else {
  129.                     return false;
  130.                 }
  131.             } else {
  132.                 return false;
  133.             }
  134.         }
  135.        
  136.         /* ***** */
  137.        
  138.         function save ( $file = false, $imageType = IMAGETYPE_JPEG ) {
  139.             if ( isset($file) && !empty($file) && is_string($file) ) {
  140.                 if ( $this->isValidImageType($imageType) ) {
  141.                     if ( $this->functionOutput($imageType, array( $this->image, $file )) ) {
  142.                         return $this;
  143.                     } else {
  144.                         return false;
  145.                     }
  146.                 } else {
  147.                     return false;
  148.                 }
  149.             } else {
  150.                 return false;
  151.             }
  152.         }
  153.        
  154.         /* ***** */
  155.        
  156.         function output ( $imageType = IMAGETYPE_JPEG, $header = false ) {
  157.             if ( $this->isValidImageType($imageType) ) {
  158.                 if ( $header ) {
  159.                     header("Content-type: ".$this->imageTypes[($imageType)]['contentType']);
  160.                 }
  161.                 if ( $this->functionOutput($imageType, array( $this->image )) ) {
  162.                     return $this;
  163.                 } else {
  164.                     return false;
  165.                 }
  166.             } else {
  167.                 return false;
  168.             }
  169.         }
  170.        
  171.         function outputString ( $imageType = IMAGETYPE_JPEG ) {
  172.             if ( $this->isValidImageType($imageType) ) {
  173.                 ob_start();
  174.                     if ( $this->output($imageType) !== false ) {
  175.                         $valid = true;
  176.                     } else {
  177.                         $valid = false;
  178.                     }
  179.                     $output = ob_get_contents();
  180.                 ob_end_clean();
  181.                 if ( $valid ) {
  182.                     return $output;
  183.                 } else {
  184.                     return false;
  185.                 }
  186.             } else {
  187.                 return false;
  188.             }
  189.         }
  190.        
  191.         function outputUri ( $imageType = IMAGETYPE_JPEG ) {
  192.             if ( $this->isValidImageType($imageType) ) {
  193.                 return 'data:'.$this->imageTypes[($imageType)]['contentType'].';base64,'.base64_encode($this->outputString($imageType));
  194.             } else {
  195.                 return false;
  196.             }
  197.         }
  198.        
  199.         /* ***** */
  200.        
  201.         function getWidth (  ) {
  202.             return imagesx($this->image);
  203.         }
  204.        
  205.         function getHeight (  ) {
  206.             return imagesy($this->image);
  207.         }
  208.        
  209.         function getImageType (  ) {
  210.             return $this->imageType;
  211.         }
  212.        
  213.         /* ***** */
  214.        
  215.         function resize ( $width, $height ) {
  216.             if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
  217.                 $newImage = imagecreatetruecolor($width, $height);
  218.                 imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  219.                 $this->image = $newImage;
  220.                 return $this;
  221.             } else {
  222.                 return false;
  223.             }
  224.         }
  225.        
  226.         function resizeToWidth ( $width ) {
  227.             if ( isset($width) && !empty($width) && is_numeric($width) ) {
  228.                 $ratio = $width / $this->getWidth();
  229.                 $height = $this->getheight() * $ratio;
  230.                 $this->resize($width, $height);
  231.                 return $this;
  232.             } else {
  233.                 return false;
  234.             }
  235.         }
  236.        
  237.         function resizeToHeight ( $height ) {
  238.             if ( isset($height) && !empty($height) && is_numeric($height) ) {
  239.                 $ratio = $height / $this->getHeight();
  240.                 $width = $this->getWidth() * $ratio;
  241.                 $this->resize($width, $height);
  242.                 return $this;
  243.             } else {
  244.                 return false;
  245.             }
  246.         }
  247.        
  248.         function resizeToMin ( $width, $height ) {
  249.             if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
  250.                 $or = $this->getWidth() / $this->getHeight();
  251.                 $r = $width / $height;
  252.                 if ( $r > $or ) {
  253.                     $this->resizeToWidth($width);
  254.                 } else if ( $r < $or ) {
  255.                     $this->resizeToHeight($height);
  256.                 } else {
  257.                     $this->resize($width, $height);
  258.                 }
  259.                 return $this;
  260.             } else {
  261.                 return false;
  262.             }
  263.         }
  264.        
  265.         function resizeToMax ( $width, $height ) {
  266.             if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
  267.                 $or = $this->getWidth() / $this->getHeight();
  268.                 $r = $width / $height;
  269.                 if ( $r > $or ) {
  270.                     $this->resizeToHeight($height);
  271.                 } else if ( $r < $or ) {
  272.                     $this->resizeToWidth($width);
  273.                 } else {
  274.                     $this->resize($width, $height);
  275.                 }
  276.                 return $this;
  277.             } else {
  278.                 return false;
  279.             }
  280.         }
  281.        
  282.         function centerCrop ( $width, $height ) {
  283.             if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
  284.                 $newImage = imagecreatetruecolor($width, $height);
  285.                 imagecopy ( $newImage, $this->image, 0, 0, (($this->getWidth()-$width)/2), (($this->getHeight()-$height)/2), $this->getWidth(), $this->getHeight());
  286.                 $this->image = $newImage;
  287.                 return $this;
  288.             } else {
  289.                 return false;
  290.             }
  291.         }
  292.        
  293.         function scale ( $scale ) {
  294.             if ( isset($scale) && !empty($scale) && is_numeric($scale) ) {
  295.                 $width = $this->getWidth() * $scale/100;
  296.                 $height = $this->getheight() * $scale/100;
  297.                 $this->resize($width, $height);
  298.                 return $this;
  299.             } else {
  300.                 return false;
  301.             }
  302.         }
  303.        
  304.         function justifyResizeCenterCrop ( $width, $height ) {
  305.             if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
  306.                 $this->resizeToMin($width, $height)->centerCrop($width, $height);
  307.                 return $this;
  308.             } else {
  309.                 return false;
  310.             }
  311.         }
  312.        
  313.         /* ***** */
  314.    
  315.     }
  316.  
  317. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement