Пример #1
0
if(!class_exists('Eden_Image')){class Eden_Image extends Eden_Class{protected $_resource=NULL;protected $_width=0;protected $_height=0;public static function i(){return self::_getMultiple(__CLASS__);}public function __construct($data,$type=NULL,$path=true,$quality=75){Eden_Image_Error::i()->argument(1,'string')->argument(2,'string','null')->argument(3,'bool')->argument(4,'int');$this->_type=$type;$this->_quality=$quality;$this->_resource=$this->_getResource($data,$path);list($this->_width,$this->_height)=$this->getDimensions();}public function __destruct(){if($this->_resource){imagedestroy($this->_resource);}}public function __toString(){ob_start();switch($this->_type){case 'gif': imagegif($this->_resource);break;case 'png': $quality=(100 - $this->_quality) / 10;if($quality > 9){$quality=9;}imagepng($this->_resource,NULL,$quality);break;case 'bmp': case 'wbmp': imagewbmp($this->_resource,NULL,$this->_quality);break;case 'jpg': case 'jpeg': case 'pjpeg': default: imagejpeg($this->_resource,NULL,$this->_quality);break;}return ob_get_clean();}public function blur(){imagefilter($this->_resource,IMG_FILTER_SELECTIVE_BLUR);return $this;}public function brightness($level){Eden_Image_Error::i()->argument(1,'numeric');imagefilter($this->_resource,IMG_FILTER_BRIGHTNESS,$level);return $this;}public function colorize($red,$blue,$green,$alpha=0){Eden_Image_Error::i()->argument(1,'numeric')->argument(2,'numeric')->argument(3,'numeric')->argument(4,'numeric');imagefilter($this->_resource,IMG_FILTER_COLORIZE,$red,$blue,$green,$alpha);return $this;}public function contrast($level){Eden_Image_Error::i()->argument(1,'numeric');imagefilter($this->_resource,IMG_FILTER_CONTRAST,$level);return $this;}public function crop($width=NULL,$height=NULL){Eden_Image_Error::i()->argument(1,'numeric','null')->argument(2,'numeric','null');$orgWidth=imagesx($this->_resource);$orgHeight=imagesy($this->_resource);if(is_null($width)){$width=$orgWidth;}if(is_null($height)){$height=$orgHeight;}if($width==$orgWidth && $height==$orgHeight){return $this;}$crop=imagecreatetruecolor($width,$height);$xPosition=0;$yPosition=0;if($width > $orgWidth || $height > $orgHeight){$newWidth=$width;$newHeight=$height;if($height > $width){$height=$this->_getHeightAspectRatio($orgWidth,$orgHeight,$width);if($newHeight > $height){$height=$newHeight;$width=$this->_getWidthAspectRatio($orgWidth,$orgHeight,$height);$rWidth=$this->_getWidthAspectRatio($newWidth,$newHeight,$orgHeight);$xPosition=($orgWidth / 2) - ($rWidth / 2);}else{$rHeight=$this->_getHeightAspectRatio($newWidth,$newHeight,$orgWidth);$yPosition=($orgHeight / 2) - ($rHeight / 2);}}else{$width=$this->_getWidthAspectRatio($orgWidth,$orgHeight,$height);if($newWidth > $width){$width=$newWidth;$height=$this->_getHeightAspectRatio($orgWidth,$orgHeight,$width);$rHeight=$this->_getHeightAspectRatio($newWidth,$newHeight,$orgWidth);$yPosition=($orgHeight / 2) - ($rHeight / 2);}else{$rWidth=$this->_getWidthAspectRatio($newWidth,$newHeight,$orgHeight);$xPosition=($orgWidth / 2) - ($rWidth / 2);}}}else{if($width < $orgWidth){$xPosition=($orgWidth / 2) - ($width / 2);$width=$orgWidth;}if($height < $orgHeight){$yPosition=($orgHeight / 2) - ($height / 2);$height=$orgHeight;}}imagecopyresampled($crop,$this->_resource,0,0,$xPosition,$yPosition,$width,$height,$orgWidth,$orgHeight);imagedestroy($this->_resource);$this->_resource=$crop;return $this;}public function edgedetect(){imagefilter($this->_resource,IMG_FILTER_EDGEDETECT);return $this;}public function emboss(){imagefilter($this->_resource,IMG_FILTER_EMBOSS);return $this;}public function gaussianBlur(){imagefilter($this->_resource,IMG_FILTER_GAUSSIAN_BLUR);return $this;}public function getDimensions(){return array(imagesx($this->_resource),imagesy($this->_resource));}public function getResource(){return $this->_resource;}public function greyscale(){imagefilter($this->_resource,IMG_FILTER_GRAYSCALE);return $this;}public function invert($vertical=false){Eden_Image_Error::i()->argument(1,'bool');$orgWidth=imagesx($this->_resource);$orgHeight=imagesy($this->_resource);$invert=imagecreatetruecolor($orgWidth,$orgHeight);if($vertical){imagecopyresampled($invert,$this->_resource,0,0,0,($orgHeight-1),$orgWidth,$orgHeight,$orgWidth,0-$orgHeight);}else{imagecopyresampled($invert,$this->_resource,0,0,($orgWidth-1),0,$orgWidth,$orgHeight,0-$orgWidth,$orgHeight);}imagedestroy($this->_resource);$this->_resource=$invert;return $this;}public function meanRemoval(){imagefilter($this->_resource,IMG_FILTER_MEAN_REMOVAL);return $this;}public function negative(){imagefilter($this->_resource,IMG_FILTER_NEGATE);return $this;}public function resize($width=NULL,$height=NULL){Eden_Image_Error::i()->argument(1,'numeric','null')->argument(2,'numeric','null');$orgWidth=imagesx($this->_resource);$orgHeight=imagesy($this->_resource);if(is_null($width)){$width=$orgWidth;}if(is_null($height)){$height=$orgHeight;}if($width==$orgWidth && $height==$orgHeight){return $this;}$newWidth=$width;$newHeight=$height;if($height < $width){$width=$this->_getWidthAspectRatio($orgWidth,$orgHeight,$height);if($newWidth < $width){$width=$newWidth;$height=$this->_getHeightAspectRatio($orgWidth,$orgHeight,$width);}}else{$height=$this->_getHeightAspectRatio($orgWidth,$orgHeight,$width);if($newHeight < $height){$height=$newHeight;$width=$this->_getWidthAspectRatio($orgWidth,$orgHeight,$height);}}return $this->scale($width,$height);}public function rotate($degree,$background=0){Eden_Image_Error::i()->argument(1,'numeric')->argument(2,'numeric');$rotate=imagerotate($this->_resource,$degree,$background);imagedestroy($this->_resource);$this->_resource=$rotate;return $this;}public function scale($width=NULL,$height=NULL){Eden_Image_Error::i()->argument(1,'numeric','null')->argument(2,'numeric','null');$orgWidth=imagesx($this->_resource);$orgHeight=imagesy($this->_resource);if(is_null($width)){$width=$orgWidth;}if(is_null($height)){$height=$orgHeight;}if($width==$orgWidth && $height==$orgHeight){return $this;}$scale=imagecreatetruecolor($width,$height);imagecopyresampled($scale,$this->_resource,0,0,0,0,$width,$height,$orgWidth,$orgHeight);imagedestroy($this->_resource);$this->_resource=$scale;return $this;}public function setTransparency(){imagealphablending( $this->_resource,false );imagesavealpha( $this->_resource,true );return $this;}public function smooth($level){Eden_Image_Error::i()->argument(1,'numeric');imagefilter($this->_resource,IMG_FILTER_SMOOTH,$level);return $this;}public function save($path,$type=NULL){if(!$type){$type=$this->_type;}switch($type){case 'gif': imagegif($this->_resource,$path);break;case 'png': $quality=(100 - $this->_quality) / 10;if($quality > 9){$quality=9;}imagepng($this->_resource,$path,$quality);break;case 'bmp': case 'wbmp': imagewbmp($this->_resource,$path,$this->_quality);break;case 'jpg': case 'jpeg': case 'pjpeg': default: imagejpeg($this->_resource,$path,$this->_quality);break;}return $this;}protected function _getHeightAspectRatio($sourceWidth,$sourceHeight,$destinationWidth){$ratio=$destinationWidth / $sourceWidth;return $sourceHeight * $ratio;}protected function _getResource($data,$path){if(!function_exists('gd_info')){Eden_Image_Error::i(Eden_Image_Error::GD_NOT_INSTALLED)->trigger();}$resource=false;if(!$path){return imagecreatefromstring($data);}switch($this->_type){case 'gd': $resource=imagecreatefromgd($data);break;case 'gif': $resource=imagecreatefromgif($data);break;case 'jpg': case 'jpeg': case 'pjpeg': $resource=imagecreatefromjpeg($data);break;case 'png': $resource=imagecreatefrompng($data);break;case 'bmp': case 'wbmp': $resource=imagecreatefromwbmp($data);break;case 'xbm': $resource=imagecreatefromxbm($data);break;case 'xpm': $resource=imagecreatefromxpm($data);break;}if(!$resource){Eden_Image_Error::i()->setMessage(Eden_Image_Error::NOT_VALID_IMAGE_FILE)->addVariable($path);}return $resource;}protected function _getWidthAspectRatio($sourceWidth,$sourceHeight,$destinationHeight){$ratio=$destinationHeight / $sourceHeight;return $sourceWidth * $ratio;}}class Eden_Image_Error extends Eden_Error{const GD_NOT_INSTALLED='PHP GD Library is not installed.';const NOT_VALID_IMAGE_FILE='%s is not a valid image file.';const NOT_STRING_MODEL='Argument %d is expecting a string or Eden_Image_Model.';}}
Пример #2
0
 protected function _getResource($data, $path)
 {
     //if the GD Library is not installed
     if (!function_exists('gd_info')) {
         //throw error
         Eden_Image_Error::i(Eden_Image_Error::GD_NOT_INSTALLED)->trigger();
     }
     # imagecreatefromgd — Create a new image from GD file or URL
     # imagecreatefromgif — Create a new image from file or URL
     # imagecreatefromjpeg — Create a new image from file or URL
     # imagecreatefrompng — Create a new image from file or URL
     # imagecreatefromstring — Create a new image from the image stream in the string
     # imagecreatefromwbmp — Create a new image from file or URL
     # imagecreatefromxbm — Create a new image from file or URL
     # imagecreatefromxpm — Create a new image from file or URL
     $resource = false;
     if (!$path) {
         return imagecreatefromstring($data);
     }
     //depending on the extension lets load
     //the file using the right GD loader
     switch ($this->_type) {
         case 'gd':
             $resource = imagecreatefromgd($data);
             break;
         case 'gif':
             $resource = imagecreatefromgif($data);
             break;
         case 'jpg':
         case 'jpeg':
         case 'pjpeg':
             $resource = imagecreatefromjpeg($data);
             break;
         case 'png':
             $resource = imagecreatefrompng($data);
             break;
         case 'bmp':
         case 'wbmp':
             $resource = imagecreatefromwbmp($data);
             break;
         case 'xbm':
             $resource = imagecreatefromxbm($data);
             break;
         case 'xpm':
             $resource = imagecreatefromxpm($data);
             break;
     }
     //if there is no resource still
     if (!$resource) {
         //throw error
         Eden_Image_Error::i()->setMessage(Eden_Image_Error::NOT_VALID_IMAGE_FILE)->addVariable($path);
     }
     return $resource;
 }
Пример #3
0
 protected function _getResource($data, $path)
 {
     if (!function_exists('gd_info')) {
         Eden_Image_Error::i(Eden_Image_Error::GD_NOT_INSTALLED)->trigger();
     }
     $resource = false;
     if (!$path) {
         return imagecreatefromstring($data);
     }
     switch ($this->_type) {
         case 'gd':
             $resource = imagecreatefromgd($data);
             break;
         case 'gif':
             $resource = imagecreatefromgif($data);
             break;
         case 'jpg':
         case 'jpeg':
         case 'pjpeg':
             $resource = imagecreatefromjpeg($data);
             break;
         case 'png':
             $resource = imagecreatefrompng($data);
             break;
         case 'bmp':
         case 'wbmp':
             $resource = imagecreatefromwbmp($data);
             break;
         case 'xbm':
             $resource = imagecreatefromxbm($data);
             break;
         case 'xpm':
             $resource = imagecreatefromxpm($data);
             break;
     }
     if (!$resource) {
         Eden_Image_Error::i()->setMessage(Eden_Image_Error::NOT_VALID_IMAGE_FILE)->addVariable($path);
     }
     return $resource;
 }