php图像处理类
博客专区 > wb123456 的博客 > 博客详情
php图像处理类
wb123456 发表于9个月前
php图像处理类
  • 发表于 9个月前
  • 阅读 0
  • 收藏 1
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

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。

 

 

 

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 77
码字总数 62434
×
wb123456
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: