Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* © Copyright 2011 - tomas.bara@whoami.cz */
- /*
- Please.. check my class and if u know better way.. tell me..
- */
- class image {
- /* ***** */
- private $image = false;
- private $imageType = false;
- public $imageTypes = false;
- /* ***** */
- function __construct ( ) {
- $this->imageTypes = array(
- IMAGETYPE_JPEG => array(
- 'extension' => 'jpg',
- 'contentType' => 'image/jpg',
- 'functionLoad' => 'imagecreatefromjpeg',
- 'functionOutput' => 'imagejpeg',
- ),
- IMAGETYPE_GIF => array(
- 'extension' => 'gif',
- 'contentType' => 'image/gif',
- 'functionLoad' => 'imagecreatefromgif',
- 'functionOutput' => 'imagegif',
- ),
- IMAGETYPE_PNG => array(
- 'extension' => 'png',
- 'contentType' => 'image/png',
- 'functionLoad' => 'imagecreatefrompng',
- 'functionOutput' => 'imagepng',
- ),
- );
- }
- /* ***** */
- function isValidImageType ( $imageType = false ) {
- if ( isset($this->imageTypes[($imageType)]) && is_array($this->imageTypes[($imageType)]) ) {
- if ( isset($this->imageTypes[($imageType)]['extension'], $this->imageTypes[($imageType)]['contentType'], $this->imageTypes[($imageType)]['functionLoad'], $this->imageTypes[($imageType)]['functionOutput']) ) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- function functionLoad ( $imageType = IMAGETYPE_JPEG, $parms = array() ) {
- if ( $this->isValidImageType($imageType) && is_array($parms) ) {
- return call_user_func_array($this->imageTypes[($imageType)]['functionLoad'], $parms);
- } else {
- return false;
- }
- }
- function functionOutput ( $imageType = IMAGETYPE_JPEG, $parms = array() ) {
- if ( $this->isValidImageType($imageType) && is_array($parms) ) {
- return call_user_func_array($this->imageTypes[($imageType)]['functionOutput'], $parms);
- } else {
- return false;
- }
- }
- /* ***** */
- function load ( $file = false ) {
- if ( isset($file) && !empty($file) && is_string($file) ) {
- $imageInfo = getimagesize($file);
- if ( $this->isValidImageType($imageInfo[2]) ) {
- $image = $this->functionLoad($imageInfo[2], array( $file ));
- if ( $image !== false ) {
- $this->imageType = $imageInfo[2];
- $this->image = $image;
- return $this;
- } else {
- return false;
- }
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- function loadString ( $string = false, $imageType = IMAGETYPE_JPEG ) {
- if ( isset($string) && !empty($string) && is_string($string) ) {
- if ( $this->isValidImageType($imageType) ) {
- $image = imagecreatefromstring($string);
- if ( $image !== false ) {
- $this->imageType = $imageType;
- $this->image = $image;
- return $this;
- } else {
- return false;
- }
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- function loadUri ( $uri = false ) {
- if ( isset($uri) && !empty($uri) && is_string($uri) ) {
- $uriParts = explode(',', $uri);
- $contentType = explode(':', $uriParts[0]);
- $contentType = explode(';', $contentType[1]);
- $contentType = $contentType[0];
- $imageSrc = base64_decode($uriParts[1]);
- foreach ( $this->imageTypes as $key => $val ) {
- if ( $val['contentType'] == $contentType ) {
- $imageType = $key;
- break;
- }
- }
- if ( isset($imageType) && $this->isValidImageType($imageType) ) {
- return $this->loadString($imageSrc, $imageType);
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- /* ***** */
- function save ( $file = false, $imageType = IMAGETYPE_JPEG ) {
- if ( isset($file) && !empty($file) && is_string($file) ) {
- if ( $this->isValidImageType($imageType) ) {
- if ( $this->functionOutput($imageType, array( $this->image, $file )) ) {
- return $this;
- } else {
- return false;
- }
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- /* ***** */
- function output ( $imageType = IMAGETYPE_JPEG, $header = false ) {
- if ( $this->isValidImageType($imageType) ) {
- if ( $header ) {
- header("Content-type: ".$this->imageTypes[($imageType)]['contentType']);
- }
- if ( $this->functionOutput($imageType, array( $this->image )) ) {
- return $this;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- function outputString ( $imageType = IMAGETYPE_JPEG ) {
- if ( $this->isValidImageType($imageType) ) {
- ob_start();
- if ( $this->output($imageType) !== false ) {
- $valid = true;
- } else {
- $valid = false;
- }
- $output = ob_get_contents();
- ob_end_clean();
- if ( $valid ) {
- return $output;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- function outputUri ( $imageType = IMAGETYPE_JPEG ) {
- if ( $this->isValidImageType($imageType) ) {
- return 'data:'.$this->imageTypes[($imageType)]['contentType'].';base64,'.base64_encode($this->outputString($imageType));
- } else {
- return false;
- }
- }
- /* ***** */
- function getWidth ( ) {
- return imagesx($this->image);
- }
- function getHeight ( ) {
- return imagesy($this->image);
- }
- function getImageType ( ) {
- return $this->imageType;
- }
- /* ***** */
- function resize ( $width, $height ) {
- if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
- $newImage = imagecreatetruecolor($width, $height);
- imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
- $this->image = $newImage;
- return $this;
- } else {
- return false;
- }
- }
- function resizeToWidth ( $width ) {
- if ( isset($width) && !empty($width) && is_numeric($width) ) {
- $ratio = $width / $this->getWidth();
- $height = $this->getheight() * $ratio;
- $this->resize($width, $height);
- return $this;
- } else {
- return false;
- }
- }
- function resizeToHeight ( $height ) {
- if ( isset($height) && !empty($height) && is_numeric($height) ) {
- $ratio = $height / $this->getHeight();
- $width = $this->getWidth() * $ratio;
- $this->resize($width, $height);
- return $this;
- } else {
- return false;
- }
- }
- function resizeToMin ( $width, $height ) {
- if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
- $or = $this->getWidth() / $this->getHeight();
- $r = $width / $height;
- if ( $r > $or ) {
- $this->resizeToWidth($width);
- } else if ( $r < $or ) {
- $this->resizeToHeight($height);
- } else {
- $this->resize($width, $height);
- }
- return $this;
- } else {
- return false;
- }
- }
- function resizeToMax ( $width, $height ) {
- if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
- $or = $this->getWidth() / $this->getHeight();
- $r = $width / $height;
- if ( $r > $or ) {
- $this->resizeToHeight($height);
- } else if ( $r < $or ) {
- $this->resizeToWidth($width);
- } else {
- $this->resize($width, $height);
- }
- return $this;
- } else {
- return false;
- }
- }
- function centerCrop ( $width, $height ) {
- if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
- $newImage = imagecreatetruecolor($width, $height);
- imagecopy ( $newImage, $this->image, 0, 0, (($this->getWidth()-$width)/2), (($this->getHeight()-$height)/2), $this->getWidth(), $this->getHeight());
- $this->image = $newImage;
- return $this;
- } else {
- return false;
- }
- }
- function scale ( $scale ) {
- if ( isset($scale) && !empty($scale) && is_numeric($scale) ) {
- $width = $this->getWidth() * $scale/100;
- $height = $this->getheight() * $scale/100;
- $this->resize($width, $height);
- return $this;
- } else {
- return false;
- }
- }
- function justifyResizeCenterCrop ( $width, $height ) {
- if ( ( isset($width) && !empty($width) && is_numeric($width) ) && ( isset($height) && !empty($height) && is_numeric($height) ) ) {
- $this->resizeToMin($width, $height)->centerCrop($width, $height);
- return $this;
- } else {
- return false;
- }
- }
- /* ***** */
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement