关于水印操作
请问老师这样做是否符合要求,还有水印该怎样跟随缩略图一样等比例缩小呢?
<?php
namespace work\demo1;
use mysql_xdevapi\Exception;
require_once 'GDBasic.php';
class image extends GDBasic
{
protected $_width;
protected $_height;
protected $_im;
protected $_type;
protected $_mime;
protected $_real_path;
public function __construct($file)
{
//检查GD库
self::check();
$imageInfo = $this->createImageByFile($file);
$this->_width = $imageInfo['width'];
$this->_height = $imageInfo['height'];
$this->_type = $imageInfo['type'];
$this->_im = $imageInfo['im'];
$this->_mime = $imageInfo['mime'];
$this->_real_path = $imageInfo['real_path'];
}
/**
* 根据文件创建图像资源,返回图像的信息数组,并存入属性中
* @param $file
* @return array
* @throws \Exception
*/
public function createImageByFile($file)
{
//检测文件是否存在
if (!file_exists($file)) {
throw new \Exception('file is not exits');
}
// 获取原图信息
$imageInfo = getimagesize($file);
// 原图的真实路径
$realPath = realpath($file);
if (!$imageInfo) {
throw new \Exception('file is not image file');
}
//根据原图类型创建对应类型的图像资源
switch ($imageInfo[2]) {
case IMAGETYPE_GIF:
$im = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$im = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$im = imagecreatefrompng($file);
break;
default:
throw new \Exception('image file must be png,jpeg,gif');
}
//返回原图信息
return array(
'width' => $imageInfo[0],
'height' => $imageInfo[1],
'type' => $imageInfo[2],
'mime' => $imageInfo['mime'],
'im' => $im,
'real_path' => $realPath,
);
}
/**
* 缩略图
* @param int $width
* @param int $height
* @return $this
* @throws \Exception
*/
public function resize(int $width, int $height)
{
if (!is_numeric($width) || !is_numeric($height)) {
throw new \Exception('image width or height must be number');
}
// 获得原图宽高
$srcW = $this->_width;
$srcH = $this->_height;
// 如果输入宽高<=0,则使用原图像宽高
if ($width <= 0 || $height <= 0) {
$desW = $srcW;//缩略图高度
$desH = $srcH;//缩略图宽度
} else {
$srcP = $srcW / $srcH; //原图宽高比
$desP = $width / $height; //输入的宽高比
//如果输入的宽度大于原图像宽度
if ($width > $srcW) {
//如果输入的高度大于原图像高度
if ($height > $srcH) {
$desW = $srcW;
$desH = $srcH;
} else {
$desH = $height;
$desW = round($desH * $srcP);
}
} else {
//如果输入的宽高比大于原图宽高比,说明宽度更大,尽量取较大的一边作为基本
if ($desP > $srcP) {
$desW = $width;
$desH = round($desW / $srcP);
} else {
$desH = $height;
$desW = round($desH * $srcP);
}
}
}
//如果PHP版本低于5.5.0
if (version_compare(PHP_VERSION, '5.5.0', '<')) {
$desIm = imagecreatetruecolor($desW, $desH); //根据缩略图宽高创建画布
if (imagecopyresampled($desIm, $this->_im, 0, 0, 0, 0, $desH, $desW, $srcW, $srcH)) {
imagedestroy($this->_im);
$this->_im = $desIm;
$this->_width = imagesx($this->_im);
$this->_height = imagesx($this->_im);
};
} else {
$desIm = imagescale($this->_im, $desW, $desH);
$this->_im = $desIm;
$this->_width = imagesx($this->_im);
$this->_height = imagesy($this->_im);
}
return $this;
}
/**
* 根据百分比生成缩略图
* @param int $percent 1-100
* @return image
* @throws \Exception
*/
public function resizeByPercent(int $percent)
{
//如果输入的比例<=0,则抛出异常
if (intval($percent) <= 0) {
throw new Exception('percent must be gt 0');
}
//如果比例大于100,则赋为100,否贼保持原比例
$percent = intval($percent) > 100 ? 100 : intval($percent);
//获得百分比
$percent = $percent / 100;
//按照百分比计算出缩略图宽高
$desW = $this->_width * $percent;
$desH = $this->_height * $percent;
//调用resize 生成缩略图
return $this->resize($desW, $desH);
}
/**
* 缩略图旋转
* @param $degree
* @return $this
*/
public function rotate($degree)
{
$degree = 360 - intval($degree);
$back = imagecolorallocatealpha($this->_im, 0, 0, 0, 127); //创建旋转填充色
$im = imagerotate($this->_im, $degree, $back, 1); //旋转图像 //是否对透明度敏感
imagesavealpha($im, true);
imagedestroy($this->_im);
$this->_im = $im;
$this->_width = imagesx($this->_im);
$this->_height = imagesy($this->_im);
return $this;
}
/**生成水印
* @param string $water 水印图片
* @param int $pct 透明度
* @return $this
* @throws \Exception
*/
public function waterMask($water, $pct = 60)
{
//根据水印图像文件生成图像资源
$waterInfo = $this->createImageByFile($water);
imagecopymerge($this->_im,$waterInfo['im'],5,5,0,0,$waterInfo['width'],$waterInfo['height'],$pct);
return $this;
}
/**
* 缩略图输出
* @return bool
*/
public function show()
{
header("Content-Type:{$this->_mime}");
if ($this->_type == 1) {
imagegif($this->_im);
return true;
}
if ($this->_type == 2) {
imagejpeg($this->_im, null, 80);
return true;
}
if ($this->_type == 3) {
imagepng($this->_im);
return true;
}
return true;
}
/**保存缩略图
* @param $file 文件路径
* @param null $quality JPEG图像质量
* @return bool true|false
* @throws \Exception 异常类
*/
public function save($file, $quality = null)
{
$ext = pathinfo($file, PATHINFO_EXTENSION); // 取得扩展名
$ext = strtolower($ext); // 小写处理
if (!$ext || !in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) { // 如果扩展名不存在或不是要求的扩展名,抛出异常
throw new \Exception('image save file must be jpg,png,gif');
}
if ($ext === 'gif') {
imagegif($this->_im, $file);
return true;
}
if ($ext === 'jpeg' || $ext === 'jpg') {
if ($quality > 0) {
if ($quality < 1) {
$quality = 1;
}
if ($quality > 100) {
$quality = 100;
}
imagejpeg($this->_im, $file, $quality);
} else {
imagejpeg($this->_im, $file);
}
return true;
}
if ($ext === ' png') {
imagepng($this->_im, $file);
return true;
}
}
}<?php
require_once 'image.php';
$image = new \work\demo1\image('./image/1.jpg');
$image->resizeByPercent(50)->waterMask('./image/aaa.png')->show();5
收起
正在回答
1回答
您好,水印效果可以完成。水印图片可以先进行缩放,将缩放后的图片保存,再作为水印使用。祝学习愉快!
PHP常用技术与ThinkPHP5框架开发
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星