class Image
{
//保存目錄
protected $savePath;
//隨機(jī)名字
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、文件目錄檢測(cè)
if (!is_file($dstPath)) {
return '目標(biāo)大圖不存在';
} else if (!is_file($srcPath)) {
return '水印小圖不存在';
} else if (!is_dir($this->savePath)) {
return '保存路徑不存在';
} else if (!is_writable($this->savePath)) {
return '保存路徑不可寫(xiě)';
}
//2、判斷圖片尺寸
list($dstWidth,$dstHeight) = getimagesize($dstPath);
list($srcWidth,$srcHeight) = getimagesize($srcPath);
if ($srcWidth > $dstWidth || $srcHeight > $dstHeight) {
return '水印圖片尺寸過(guò)大';
}
//3、計(jì)算水印在目標(biāo)圖片上的位置
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、將水印圖片合并到目標(biāo)圖片上
$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 '保存路徑不可寫(xiě)';
}
//2、計(jì)算尺寸
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) {
//如果沒(méi)有透明色,默認(rèn)設(shè)置黑色為透明色
$lucidColor = imagecolorallocate($dstImg, 0, 0, 0);
}
//用透明色填充圖片
imagefill($dstImg, 0, 0, $lucidColor);
//設(shè)置透明色
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;
//計(jì)算縮放比例
$scaleWidth = $width / $srcWidth;
$scaleHeight = $height / $srcHeight;
$scaleFinal = min($scaleWidth,$scaleHeight);
//保存實(shí)際尺寸
$size['newWidth'] = $srcWidth * $scaleFinal;
$size['newHeight'] = $srcHeight * $scaleFinal;
//計(jì)算偏移尺寸
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;
}
}
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。互聯(lián)網(wǎng)+時(shí)代,時(shí)刻要保持學(xué)習(xí),攜手千鋒PHP,Dream?It?Possible。