为什么没有水印呢
<?php
namespace GD\lib;
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->_im=$imageInfo['im'];
$this->_type=$imageInfo['type'];
$this->_real_path=$imageInfo['real_path'];
$this->_mime=$imageInfo['mime'];
}
/**
* 根据文件创建图像
* @param $file
* @return array
* @throws \Exception
*/
public function createImageByFile($file)
{
//检查文件是否存在
if(!file_exists($file))
{
throw new \Exception("file is not exists");
}
//获取文件信息
$imageInfo=getimagesize($file);
$realPath=realpath($file);//获取文件的全部路径
if(!$imageInfo)
{
throw new \Exception("file is not image file ");
}
switch ($imageInfo[2]) {
case 1:
$im=imagecreatefromgif($file);
break;
case 2:
$im=imagecreatefromjpeg($file);
break;
case 3:
$im=imagecreatefrompng($file);
break;
default:
throw new \Exception(" image file must be gif jpeg png");
break;
}
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($width,$height)
{
if(!is_numeric($width)||!is_numeric($height))
{
throw new \Exception("image width or height must be number");
}
//根据传参的宽高获取最终的宽高
//$desW=0;//生成缩略图的宽
//$desH=0;//生成缩略图的高
$srcW=$this->_width;//原图的宽
$srcH=$this->_height;//原图的高
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
if(version_compare(PHP_VERSION,'5.5.0','<'))
{
$desIm=imagecreatetruecolor($desW,$desH);
if(imagecopyresampled($desIm,$this->_im,0,0,0,0,$desW,$desH,$srcW,$srcH))
{
imagedestroy($this->_im);//释放原图
$this->_im=$desIm;
$this->_width=imagesx($this->_im);
$this->_height=imagesy($this->_im);
}
}else
{
//PHP版本大于5.5
if($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($percent)
{
if(intval($percent)<=0)
{
throw new \Exception("percent must be gt 0");
}
$percent=intval($percent)>100?100:intval($percent);
$percent=$percent/100;
$desW=$this->_width*$percent;
$desH=$this->_height*$percent;
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);
$this->resize($this->_width,$this->_height);
return $this;
}
/**
* 生成水印
* @param file $water 水印图片
* @param int $pct 透明度
* @return $this
*/
public function waterInfo($water,$pct=60)
{
//根据水印图像文件生成图像资源
$waterInfo=$this->createImageByFile($water);
imagecopymerge($this->_im, $waterInfo['im'], 0, 0, 0, 0, $waterInfo['width'], $waterInfo['height'], $pct);
//销毁$this->_im
$this->_im=$waterInfo['im'];
$this->_width=$this->_im;
$this->_height=$this->_im;
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;
}*/
switch ($this->_type) {
case 1:
imagegif($this->_im);
break;
case 2:
imagejpeg($this->_im,null,80);
break;
case 3:
imagepng($this->_im);
break;
}
}
/**
* 保存图像文件
* @param $file
* @param null $quality
* @return bool
* @throws \Exception
*/
public function save($file,$quality=null)
{
//获取保存目的文件的扩展名
$ext=pathinfo($file,PATHINFO_EXTENSION);
$ext=strtolower($ext);
if (!$ext || !in_array($ext,array('jpeg','jpg','png','gif')))
{
throw new \Exception("image save file must be jpeg,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;
}
}
}require_once 'Image.php';
$Image=new \GD\lib\Image('../image/b.png');
$Image->resizeByPercent(50)->waterInfo('../image/1.jpg')->show();显示结果是只有水印的照片,而且大小也不能调整
5
收起
正在回答 回答被采纳积分+1
1回答
PHP常用技术与ThinkPHP5框架开发
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星