Created
December 3, 2014 16:01
-
-
Save rongself/52625fde39ff0c05e167 to your computer and use it in GitHub Desktop.
image resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CzImage{ | |
protected $resource; | |
protected $newResource; | |
protected $name; | |
protected $path; | |
protected $width; | |
protected $height; | |
protected $ext; | |
protected $mime; | |
private $_saveFunctionName; | |
/** | |
* @param $path | |
* @throws CzImageArgumentException | |
* @throws CzImageTypeException | |
*/ | |
public function __construct($path) | |
{ | |
$this->path = $path; | |
$imageInfo = getimagesize($this->path); | |
if($imageInfo===false) throw new CzImageArgumentException('load image failure,path: '.$this->path); | |
switch($imageInfo[2]){ | |
case IMAGETYPE_GIF: | |
$this->resource = @imagecreatefromgif($this->path); | |
$this->ext = '.gif'; | |
$this->_saveFunctionName = 'imagegif'; | |
break; | |
case IMAGETYPE_JPEG: | |
$this->resource = @imagecreatefromjpeg($this->path); | |
$this->ext = '.jpg'; | |
$this->_saveFunctionName = 'imagejpeg'; | |
break; | |
case IMAGETYPE_PNG: | |
$this->resource = @imagecreatefrompng($this->path); | |
$this->ext = '.png'; | |
$this->_saveFunctionName = 'imagepng'; | |
break; | |
default: | |
throw new CzImageTypeException('Not support this image type:'.$this->path); | |
break; | |
} | |
if($this->resource===false) throw new CzImageArgumentException('create image failure,path: '.$this->path); | |
$this->width = $imageInfo[0]; | |
$this->height = $imageInfo[1]; | |
$this->mime = $imageInfo['mime']; | |
$this->name = substr($this->path,strrpos($this->path,'/')+1); | |
} | |
/** | |
* @param int|null $newWidth | |
* @param int|null $newHeight | |
*/ | |
public function resize($newWidth=null,$newHeight=null) | |
{ | |
$newWidth = intval($newWidth); | |
$newWidth = intval($newWidth); | |
if($newWidth<=0&&$newHeight>0){ | |
$newWidth = intval($newHeight*$this->width/$this->height); | |
}elseif($newWidth>0&&$newHeight<=0){ | |
$newHeight = intval($newWidth*$this->height/$this->width); | |
}else{ | |
$newWidth = $this->width; | |
$newHeight = $this->height; | |
} | |
$this->newResource = imagecreatetruecolor($newWidth, $newHeight); | |
imagecopyresampled($this->newResource,$this->resource,0,0,0,0,$newWidth,$newHeight,$this->width,$this->height); | |
$this->width = $newWidth; | |
$this->height = $newHeight; | |
} | |
/** | |
* @param $path | |
* @param int $jpgQuality | |
* @return bool | |
*/ | |
public function save($path,$jpgQuality=75) | |
{ | |
if(isset($this->newResource)){ | |
$resource = $this->newResource; | |
}else{ | |
$resource = $this->resource; | |
} | |
$dir = substr($path,0,strrpos($path,'/')+1); | |
$params = array($resource,$path); | |
if($this->_saveFunctionName == 'imagejpeg')$params[2] = intval($jpgQuality); | |
if(!file_exists($dir)){ | |
if(mkdir($dir)){ | |
return call_user_func_array($this->_saveFunctionName,$params) !== false; | |
} | |
}else{ | |
return call_user_func_array($this->_saveFunctionName,$params) !== false; | |
} | |
return false; | |
} | |
/** | |
* @param int $jpgQuality | |
* @return bool | |
*/ | |
public function output($jpgQuality=75){ | |
if(isset($this->newResource)){ | |
$resource = $this->newResource; | |
}else{ | |
$resource = $this->resource; | |
} | |
$params = array($resource,null); | |
if($this->_saveFunctionName == 'imagejpeg')$params[2] = $jpgQuality; | |
return call_user_func_array($this->_saveFunctionName,$params) !== false; | |
} | |
public function __destruct() | |
{ | |
imagedestroy($this->resource); | |
imagedestroy($this->newResource); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @return int | |
*/ | |
public function getWidth() | |
{ | |
return $this->width; | |
} | |
/** | |
* @return int | |
*/ | |
public function getHeight() | |
{ | |
return $this->height; | |
} | |
/** | |
* @return string | |
*/ | |
public function getMime() | |
{ | |
return $this->mime; | |
} | |
} | |
class CzImageTypeException extends Exception{} | |
class CzImageArgumentException extends Exception{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment