<?php
$image = new Image();
//$image->water('c_1.jpg','shou.png',9);
$image->suofang('9.jpg',200,200);
class Image{
//路径
protected $path;
//是否启用随机名字
protected $isRandName;
//要保存的图像类型
protected $type;
function __construct($path='./',$isRandName=true,$type='png'){
$this->path = $path;
$this->isRandName = $isRandName;
$this->type = $type;
}
//对外公开的水印方法
//image源图片
//water水印图片
//position 水印图片位置
//tmd 水印图片透明度
// prefix 图片前缀
public function water($image,$water,$position,$tmd=100,$prefix='water_'){
//1 判断这两张图片是否存在
if((!file_exists($image)) || (!file_exists($water))){
die("图片资源不存在");
}
//2 得到源图片的宽度和高度以及水印图片的高度和宽度
$imageInfo = self::getImageInfo($image);
$waterInfo = self::getImageInfo($water);
//3 判断水印图片能否贴上
if(!$this->checkImage($imageInfo,$waterInfo)){
exit('水印图片太大');
}
//4 打开图片
$imageRes = self::openAnyImage($image);
$waterRes = self::openAnyImage($water);
//5 根据水印图片的位置算水印图片的坐标
$pos = $this->getPosition($position,$imageInfo,$waterInfo);
//6 将水印图片贴过来
imagecopymerge($imageRes,$waterRes,$pos['x'],$pos['y'],0,0,$waterInfo['width'],$waterInfo['height'],$tmd);
//7 得到要保存图片的文件名
$newName = $this->createNewName($image,$prefix);
//8 得到要保存图片的路径,也就是文件全路径
$newPath = rtrim($this->path,'/').'/'.$newName;
//9 保存图片
$this->saveImage($imageRes,$newPath);
//10 销毁图片
imagedestroy($imageRes);
imagedestroy($waterRes);
return $newPath;
}
//对外公开的缩放方法
//$image 需要缩放的图片
// $width 缩放后的高度
// $height 缩放后的高度
public function suofang($image,$width,$height,$prefix='sf_'){
// 得到图片原来的宽度和高度
$info = self::getImageInfo($image);
//根据图片原来的宽高和最终要缩放宽高计算得到图像不变形的宽高
$size = $this->getNewSize($width,$height,$info);
//打开图片资源
$imageRes = self::openAnyImage($image);
//进行缩放
$newRes = $this->kidOfImage($imageRes,$size,$info);
//保存图片
$newName = $this->createNewName($image,$prefix);
$newPath = rtrim($this->path,'/').'/'.$newName;
$this->saveImage($newRes,$newPath);
//销毁图像资源
imagedestroy($imageRes);
imagedestroy($newRes);
}
//处理透明色函数
protected function kidOfImage($srcImg,$size,$imgInfo){
//传入新的尺寸,创建一个指定尺寸的图片
$newImg = imagecreatetruecolor($size['old_w'],$size['old_h']);
//定义透明色
$otsc = imagecolortransparent($srcImg);
if($otsc>=0){
//取得透明色
$transparentcolor = imagecolorsforindex($srcImg,$otsc);
//创建透明色
$newtranspatentcolor = imagecolorallocate(
$newImg,
$transparentcolor['red'],
$transparentcolor['green'],
$transparentcolor['blue']
);
}else{
//将黑色作为透明色,因为创建图像后在第一次分配颜色时背景默认黑色
$newtransparentcolor = imagecolorallocate($newImg,0,0,0);
}
//背景填充透明
imagefill($newImg,0,0,$newtransparentcolor);
imagecolortransparent($newImg,$newtransparentcolor);
imagecopyresampled($newImg,$srcImg,$size['x'],$size['y'],0,0,$size['new_w'],$size['new_h'],$imgInfo['width'],$imgInfo['height']);
return $newImg;
}
//计算图像最终的宽度和高度函数
protected function getNewSize($width,$height,$imgInfo){
$size['old_w'] = $width;
$size['old_h'] = $height;
$scaleWidth = $width/$imgInfo['width'];
$scaleHeight = $height/$imgInfo['height'];
$scaleFinal = min($scaleWidth,$scaleHeight);
$size['new_w'] = round($imgInfo['width']*$scaleFinal);
$size['new_h'] = round($imgInfo['height']*$scaleFinal);
if($scaleWidth<$scaleHeight){
$size['x'] = 0;
$size['y'] = round(abs($size['new_h']-$height)/2);
}else{
$size['y'] = 0;
$size['x'] = round(abs($size['new_w']-$width)/2);
}
return $size;
}
protected function checkImage($imageInfo,$waterInfo){
if(($waterInfo['width']>$imageInfo['width']) || ($waterInfo['height']>$imageInfo['height'])){
return false;
}
return true;
}
//根据位置计算水印的图片的坐标
protected function getPosition($position,$imageInfo,$waterInfo){
switch($position){
case 1:
$x = 0;
$y = 0;
break;
case 2:
$x = ($imageInfo['width']-$waterInfo['width'])/2;
$y = 0;
break;
case 3:
$x = $imageInfo['width']-$waterInfo['width'];
$y = 0;
break;
case 4:
$x = 0;
$y = ($imageInfo['height']-$waterInfo['height'])/2;
break;
case 5:
$x = ($imageInfo['width']-$waterInfo['width'])/2;
$y = ($imageInfo['height']-$waterInfo['height'])/2;
break;
case 6:
$x = $imageInfo['width']-$waterInfo['width'];
$y = ($imageInfo['height']-$waterInfo['height'])/2;
break;
case 7:
$x = 0;
$y = $imageInfo['height']-$waterInfo['height'];
break;
case 8:
$x = ($imageInfo['width']-$waterInfo['width'])/2;
$y = $imageInfo['height']-$waterInfo['height'];
break;
case 9:
$x = $imageInfo['width']-$waterInfo['width'];
$y = $imageInfo['height']-$waterInfo['height'];
break;
case 0:
$x = mt_rand(0,($imageInfo['width']-$waterInfo['width']));
$y = mt_rand(0,($imageInfo['height']-$waterInfo['height']));
break;
}
return ['x'=>$x,'y'=>$y];
}
//得到文件名
protected function createNewName($imagePath,$prefix){
if($this->isRandName){
$name = $prefix.uniqid().'.'.$this->type;
}else{
$name = $prefix.pathinfo($imagePath)['filename'].'.'.$this->type;
}
return $name;
}
//保存图片资源函数
protected function saveImage($imageRes,$newPath){
$fun = 'image'.$this->type;
//通过变量函数进行保存
$fun($imageRes,$newPath);
}
//根据图片的路径获取图片信息的 静态方法
static function getImageInfo($imagePath){
//获取图片信息
$info = getimagesize($imagePath);
var_dump($info);
$data['width'] = $info[0];
$data['height'] = $info[1];
$data['mime'] = $info['mime'];
return $data;
}
static function openAnyImage($imagePath){
//得到图片的mime类型
$mime = self::getImageInfo($imagePath)['mime'];
switch($mime){
case 'image/png':
$image = imagecreatefrompng($imagePath);
break;
case 'image/gif':
$image = imagecreatefromgif($imagePath);
break;
case 'image/jpeg':
$image = imagecreatefromjpeg($imagePath);
break;
case 'image/wbmp':
$image = imagecreatefromwbmp($imagePath);
break;
}
return $image;
}
}
php封装图片加水印和缩放类
最新推荐文章于 2021-03-21 07:35:14 发布