php圖像處理類

class Image

{

//保存目錄

protected $savePath;

//隨機名字

protected $randName;

//圖片后綴

protected $extension;

//保存文件名

protected $saveFileName;

public function __construct(

$savePath='./',

$randName=true,

$extension='png'

)

{

$this->savePath = $savePath;

$this->randName = $randName;

$this->extension = $extension;

}

public function setOption($name,$value=null)

{

if (is_array($name)) {

foreach ($name as $k => $v) {

$this->$k = $v;

}

} else {

$this->$name = $value;

}

}

public function getSaveFile()

{

return $this->saveFileName;

}

public function waterMark($dstPath,$srcPath,$pos=9,$pct=100)

{

//1、文件目錄檢測

if (!is_file($dstPath)) {

return '目標大圖不存在';

} else if (!is_file($srcPath)) {

return '水印小圖不存在';

} else if (!is_dir($this->savePath)) {

return '保存路徑不存在';

} else if (!is_writable($this->savePath)) {

return '保存路徑不可寫';

}

//2、判斷圖片尺寸

list($dstWidth,$dstHeight) = getimagesize($dstPath);

list($srcWidth,$srcHeight) = getimagesize($srcPath);

if ($srcWidth > $dstWidth || $srcHeight > $dstHeight) {

return '水印圖片尺寸過大';

}

//3、計算水印在目標圖片上的位置

if ($pos>=1 && $pos<=9) {

$offsetX = ($pos-1)%3 * ceil(($dstWidth-$srcWidth)/2);

$offsetY = floor(($pos-1)/3) * ceil(($dstHeight-$srcHeight)/2);

} else {

$offsetX = mt_rand(0,$dstWidth-$srcWidth);

$offsetY = mt_rand(0,$dstHeight-$srcHeight);

}

//4、將水印圖片合并到目標圖片上

$dstImg = $this->openImage($dstPath);

$srcImg = $this->openImage($srcPath);

imagecopymerge($dstImg, $srcImg, $offsetX, $offsetY, 0, 0, $srcWidth, $srcHeight, $pct);

//5、保存圖片

$this->saveImage($dstImg,$dstPath);

//6、釋放資源

imagedestroy($dstImg);

imagedestroy($srcImg);

//7、返回保存文件的路徑

return $this->saveFileName;

}

protected function openImage($imagePath)

{

$info = getimagesize($imagePath);

$extension = image_type_to_extension($info[2],false);

$openFunc = 'imagecreatefrom' . $extension;

return $openFunc($imagePath);

}

protected function saveImage($image,$path)

{

//路徑

$this->saveFileName = rtrim($this->savePath,'/') . '/';

$info = pathinfo($path);

//名字

if ($this->randName) {

$this->saveFileName .= uniqid();

} else {

$this->saveFileName .= $info['filename'];

}

//后綴

if (empty($this->extension)) {

$this->extension = $info['extension'];

} else {

$this->extension = ltrim($this->extension,'.');

}

//完整路徑名

$this->saveFileName .= '.' . $this->extension;

//保存圖片

if ($this->extension == 'jpg') {

$this->extension = 'jpeg';

}

$saveFunc = 'image' . $this->extension;

$saveFunc($image,$this->saveFileName);

//返回保存的文件路徑名

return $this->saveFileName;

}

public function zoomImage($imgPath,$width,$height)

{

//1、檢查文件目錄

if (!file_exists($imgPath)) {

return '圖片路徑不存在';

} else if (!is_dir($this->savePath)) {

return '保存路徑不存在';

} else if (!is_writable($this->savePath)) {

return '保存路徑不可寫';

}

//2、計算尺寸

list($srcWidth,$srcHeight) = getimagesize($imgPath);

$size = $this->getSize($width,$height,$srcWidth,$srcHeight);

//3、合并圖片

$dstImg = imagecreatetruecolor($width,$height);

$srcImg = $this->openImage($imgPath);

$this->mergeImage($dstImg,$srcImg,$size);

//4、保存圖片

$this->saveImage($dstImg,$imgPath);

//5、釋放資源

imagedestroy($dstImg);

imagedestroy($srcImg);

//6、返回保存的文件路徑名

return $this->saveFileName;

}

protected function mergeImage($dstImg,$srcImg,$size)

{

//獲取原始圖片的透明色

$lucidColor = imagecolortransparent($srcImg);

if ($lucidColor == -1) {

//如果沒有透明色,默認設置黑色為透明色

$lucidColor = imagecolorallocate($dstImg, 0, 0, 0);

}

//用透明色填充圖片

imagefill($dstImg, 0, 0, $lucidColor);

//設置透明色

imagecolortransparent($dstImg,$lucidColor);

//合并圖片

imagecopyresampled($dstImg, $srcImg, $size['offsetX'], $size['offsetY'], 0, 0, $size['newWidth'], $size['newHeight'], $size['srcWidth'], $size['srcHeight']);

}

protected function getSize($width,$height,$srcWidth,$srcHeight)

{

//保存原始尺寸

$size['srcWidth'] = $srcWidth;

$size['srcHeight'] = $srcHeight;

//計算縮放比例

$scaleWidth = $width / $srcWidth;

$scaleHeight = $height / $srcHeight;

$scaleFinal = min($scaleWidth,$scaleHeight);

//保存實際尺寸

$size['newWidth'] = $srcWidth * $scaleFinal;

$size['newHeight'] = $srcHeight * $scaleFinal;

//計算偏移尺寸

if ($scaleWidth < $scaleHeight) {

$size['offsetX'] = 0;

$size['offsetY'] = round(($height - $size['newHeight'])/2);

} else {

$size['offsetY'] = 0;

$size['offsetX'] = round(($width - $size['newWidth'])/2);

}

return $size;

}

}

著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。互聯網+時代,時刻要保持學習,攜手千鋒PHP,Dream?It?Possible。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • class Image { //保存目錄 protected $savePath; //隨機名字 protecte...
    php紅薯閱讀 422評論 0 0
  • class Upload { //保存路徑 protected $savePath = './'; //日期目錄 ...
    php紅薯閱讀 607評論 0 0
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,554評論 0 17
  • 工廠模式類似于現實生活中的工廠可以產生大量相似的商品,去做同樣的事情,實現同樣的效果;這時候需要使用工廠模式。簡單...
    舟漁行舟閱讀 7,854評論 2 17
  • 新余學院,這個圓了我大學夢的地方,2016年7月,接到新余學院錄取通知書的我顯然是興奮的,而在這股興奮勁褪去之后,...
    lt1997閱讀 280評論 1 2