截止目前已经改造了4个类:
图像处理类:
1,图片加水印处理( 支持任意位置与九宫格位置 )
2,图片缩放处理( 等比缩放,不变形裁剪 )
<?php class Image{
//文件路径
protected $path;
//是否允许随机文件名称
protected $enableRandName;
//文件类型
protected $type;
//错误号
protected $errorNo;
//错误信息
protected $errorInfo; public function __construct( $_path = './', $_enableRandName = true ){
$this->path = $_path;
$this->enableRandName = $_enableRandName;
} public function __get( $key ) {
if( $key == 'errorNo' ) {
return $this->errorNo;
}else if( $key == 'errorInfo' ) {
return $this->getErrorInfo();
}
}
protected function getErrorInfo(){
$info = '';
switch( $this->errorNo ){
case 1000:
$info = "水印或者图片源文件不存在";
break;
case 1001:
$info = '水印图片的尺寸(宽或者高)大于图片本身';
break;
}
return $info;
} //获取图片信息
public static function getImageInfo( $imgPath ){
$data = getimagesize( $imgPath );
$info['width'] = $data[0];
$info['height'] = $data[1];
$info['mime'] = $data['mime'];
return $info;
} /*
* @srcImg 原图片
* @waterImg 水印图片
* @pos 水印位置
* @alpha 透明度
* @prefix 水印处理之后的图片前缀
* */
public function waterMark( $srcImg, $waterImg, $pos, $alpha, $prefix = 'ghostwu_' ){
//检查图片和水印是否存在
if( !file_exists( $srcImg )
|| !file_exists( $waterImg ) ) {
$this->errorNo = 1000;
return false;
} //获取图片和水印信息
$srcImageInfo = self::getImageInfo( $srcImg );
$waterImageInfo = self::getImageInfo( $waterImg ); $this->type = explode( '/', $srcImageInfo['mime'] )[1]; //检查水印和图片的尺寸, 水印是否能加在图片上
if( !$this->check( $srcImageInfo, $waterImageInfo ) ) {
$this->errorNo = 1001;
return false;
} //打开图片文件和水印文件资源
$imgRes = self::openImage( $srcImg );
$waterRes = self::openImage( $waterImg ); //计算水印图片位置
$waterPos = $this->calcPosition( $pos, $srcImageInfo, $waterImageInfo ); //合成水印图片
imagecopymerge ( $imgRes, $waterRes, $waterPos['x'], $waterPos['y'], 0, 0, $waterImageInfo['width'], $waterImageInfo['height'], $alpha ); //得到文件的保存名字
$saveName = $this->getFileName( $srcImg, $prefix ); //拼凑完整的文件名称
$fullPath = rtrim( $this->path, '/' ) . '/' . $saveName; //保存添加水印后的文件
$this->saveImage( $imgRes, $fullPath ); imagedestroy( $imgRes );
imagedestroy( $waterRes ); return $fullPath;
} protected function saveImage( $imgRes, $fullPath ){
$func = 'image' . $this->type;
$func( $imgRes, $fullPath );
} protected function getFileName( $imgPath, $prefix ){
if ( $this->enableRandName ) {
$saveName = $prefix . uniqid() . '.' . $this->type;
}else {
$saveName = $prefix . pathinfo( $imgPath )['filename'] . '.' . $this->type;
}
return $saveName;
} protected function calcPosition( $pos, $imageInfo, $waterImageInfo ){
$x = 0; $y = 0;
switch( $pos ) {
case 1: //左上角
$x = 0; $y = 0;
break;
case 2: //上中
$x = ( $imageInfo['width'] - $waterImageInfo['width'] ) / 2;
$y = 0;
break;
case 3: //右上角
$x = $imageInfo['width'] - $waterImageInfo['width'];
$y = 0;
break;
case 4: //左边中间
$x = 0;
$y = ( $imageInfo['height'] - $waterImageInfo['height'] ) / 2;
break;
case 5: //正中间
$x = ( $imageInfo['width'] - $waterImageInfo['width'] ) / 2;
$y = ( $imageInfo['height'] - $waterImageInfo['height'] ) / 2;
break;
case 6: //右边中间
$x = $imageInfo['width'] - $waterImageInfo['width'];
$y = ( $imageInfo['height'] - $waterImageInfo['height'] ) / 2;
break;
case 7: //左下角
$x = 0;
$y = $imageInfo['height'] - $waterImageInfo['height'];
break;
case 8: //中间下面
$x = ( $imageInfo['width'] - $waterImageInfo['width'] ) / 2;
$y = $imageInfo['height'] - $waterInfo['height'];
break;
case 9: //右下角
$x = $imageInfo['width'] - $waterImageInfo['width'];
$y = $imageInfo['height'] - $waterImageInfo['height'];
break;
case 0: //随机位置
$x = mt_rand( 0, $imageInfo['width'] - $waterImageInfo['width'] );
$y = mt_rand( 0, $imageInfo['height'] - $waterImageInfo['height'] );
break;
}
return [ 'x' => $x, 'y' => $y ];
} public static function openImage( $imagePath ){
$mime = self::getImageInfo( $imagePath )['mime'];
switch( $mime ){
case 'image/gif':
$im = imagecreatefromgif( $imagePath );
break;
case 'image/jpeg':
$im = imagecreatefromjpeg( $imagePath );
break;
case 'image/png':
$im = imagecreatefrompng( $imagePath );
break;
case 'image/wbmp':
$im = imagecreatefromwbmp( $imagePath );
break;
}
return $im;
} public function check( $imgInfo, $waterInfo ){
if( $imgInfo['width'] < $waterInfo['width']
|| $imgInfo['height'] < $waterInfo['height'] ){
return false;
}
return true;
} /*
* @srcImg 原图片路径
* @dstImg 目标图片路径
* @newWidth 缩放后新的图片宽度
* @newHeight 缩放后新的图片高度
* */
public function thumb( $srcImg, $newWidth, $newHeight, $prefix="thumb_" ){
$srcIm = self::openImage( $srcImg );
$srcInfo = self::getImageInfo( $srcImg );
$srcW = $srcInfo['width'];
$srcH = $srcInfo['height'];
$this->type = explode( '/', $srcInfo['mime'] )[1];
$ratio_w = $newWidth / $srcW;
$ratio_h = $newHeight / $srcH;
$ratio = 1.0; //缩放后的图片 宽度/高度 都比原图大或者小
if( ( $ratio_w < 1 && $ratio_h < 1 ) || ( $ratio_w > 1 && $ratio_h > 1 ) ) {
if( $ratio_w < $ratio_h ) {
$ratio = $ratio_h;
}else {
$ratio = $ratio_w;
}
$tmp_w = (int)( $newWidth / $ratio );
$tmp_h = (int)( $newHeight / $ratio );
$tmp_im = imagecreatetruecolor( $tmp_w, $tmp_h );
//生成一个以原图片最大边长,比例为ratio的中间图像
imagecopy( $tmp_im, $srcIm, 0, 0, 0, 0, $tmp_w, $tmp_h );
//定义一个目标尺寸的图像
$newIm = imagecreatetruecolor( $newWidth, $newHeight );
imagecopyresampled( $newIm, $tmp_im, 0, 0, 0, 0, $newWidth, $newHeight, $tmp_w, $tmp_h );
}else {
//缩放后的图片 宽度/高度 一个比原图大, 另一个比原图小
$ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w; //取比例大的那个值 $tmp_w=(int)($srcW * $ratio);
$tmp_h=(int)($srcH * $ratio);
$tmp_im = imagecreatetruecolor($tmp_w, $tmp_h); imagecopyresampled($tmp_im, $srcIm, 0, 0, 0, 0, $tmp_w, $tmp_h, $srcW, $srcH ); $newIm = imagecreatetruecolor($newWidth, $newHeight);
imagecopy($newIm, $tmp_im, 0, 0, 0, 0, $newWidth, $newHeight);
}
//处理后的文件的保存名字
$saveName = $this->getFileName( $newIm, $prefix ); //拼凑完整的文件名称
$fullPath = rtrim( $this->path, '/' ) . '/' . $saveName; $this->saveImage( $newIm, $fullPath ); imagedestroy( $newIm ); return $fullPath;
}
} $img = new Image();
//$img->waterMark( '1.jpg', 'logo.png', 5, 30 );
echo $img->thumb( '1.jpg', 330, 495 ); ?>