php图像处理类
{
//保存目录
protected $savePath;
//随机名字
protected $randName;
//图片后缀
protected $extension;
//保存文件名
protected $saveFileName;
public function __construc
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 && $posopenImage($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; } } (编辑:源码门户网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |