学习笔记-php图像加水印组合图像简单完美剪裁-2016.4.7

<?php
/**
* Created by PhpStorm.
* User: 兰小宇
* Date: 2016/3/30
* Time: 23:08
*/
//图像处理类
class Image{
private $file; //图像地址
private $width; //获取图像的宽度
private $height; //获取图像的高度
private $type; //获取图像的类型
private $img; //原来图像的资源句柄
private $new; //新的资源句柄
//构造方法
public function __construct($file){
$this->file = $_SERVER['DOCUMENT_ROOT'].$file;
list($this->width,$this->height,$this->type) = getimagesize($this->file);
$this->img = $this->getType($this->file,$this->type);
}
/*
* 水印处理 ,给图片加水印,并且设定水印的位置,同时等比例处理图片
* @param $new_width 新的宽度
* @param $new_height 新的高度
*/
public function ckeImg($new_width = 0 ,$new_height = 0){//初始化
//获取水印的宽度和高度以及类型
list($water_width,$water_height,$water_type) = getimagesize(MARK);//这里的MARK是设定的水印图片的资源句柄
//获取水印的资源
$water = $this->getType(MARK,$water_type);
//判断新的宽度和高度是否存在
if(empty($new_width) && empty($new_height)){
$new_width = $this->width;
$new_height = $this->height;
}
//如果传递过来的值不是数字而是字母或者其他,我们也需要进行处理
if(!is_numeric($new_width) || !is_numeric($new_height)){
$new_width = $this->width;
$new_height = $this->height;
}

//等比例处理图片
if ($this->width < $this->height) {
//让长度和新高度等比例
$new_width = ($new_height / $this->height) * $this->width;
}else{
//让新高度和新长度等比例
$new_height = ($new_width / $this->width) * $this->height;
}
//设定水印图片的位置
$water_w = $new_width-$water_width - 5;
$water_h = $new_height-$water_height - 5;


/**
*创建一个背景图
* 使用函数imagecreatetruecolor();两个参数高度和宽度
*
*/
$this->new = imagecreatetruecolor($new_width,$new_height);
//创建剪裁后的图像
imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
//给图片加上水印,但是这里要加上判断,只有图片的宽度和高度都比水印的宽度和高度大的时候,我们才给图片加水印,
if($new_width > $water_width && $new_height > $water_height){
imagecopy($this->new,$water,$water_w,$water_h,0,0,$water_width,$water_height);
}
//销毁水印资源
imagedestroy($water);
//输出的时候,在外部我们把输出的图像的资源句柄销毁了,用类内的out()方法
}

/****************************************/
/*
* 图像剪裁三:固定长高,等比列,对图像裁剪,扩容,修剪
*
*/
public function thumb($new_width = 0,$new_height = 0){//为避免不没有传值,所以我们初始化了新宽度和和高度
//另外这里需要一个判断
if(empty($new_width) && empty($new_height)){
$new_width = $this->width;
$new_height = $this->height;
}
//如果传递过来的值不是数字而是字母或者其他,我们也需要进行处理
if(!is_numeric($new_width) || !is_numeric($new_height)){
$new_width = $this->width;
$new_height = $this->height;
}
//固定生成图像的宽和高
$n_w = $new_width;
$n_h = $new_height;
//初始化裁剪点
$cut_w = 0;
$cut_h = 0;
//判断原始图像的宽和高
if ($this->width < $this->height) { //如果原始图像的宽比他的高度小
//让长度和新高度等比例
$new_width = ($new_height / $this->height) * $this->width; //新的宽度等于新的高度除以原来的高度再乘以原来的宽度
//公式解释:在等比例的裁剪中,我们首先要找到等比例的因子,就是按照什么样的比例来进行缩放的
//如果款比高小,那我们就用新的高度,除以老的高度,得到一个等比例的百分数,然后乘以元来的宽度等于新的宽度
//例如:原来的是500*1000 ,设定的宽和高为150 * 50
//那么新的宽度等于 (50/1000)*500
}else{
//让新高度和新长度等比例
$new_height = ($new_width / $this->width) * $this->height;
}

//这里我们需要通过另外一个小方法,寻找合适的裁剪点,如下:
if ($new_width < $n_w) {
//如果新高度小于新容器高度
$r = $n_w / $new_width;
//按长度求出等比例因子
$new_width *= $r;
//扩展填充后的长度
$new_height *= $r;
//扩展填充后的高度
$cut_height = ($new_height - $n_h) / 2;//这里一定要用等比例后新的高度度减去容器的高度除以二得到剪裁的点
//求出裁剪点的高度
}
if ($new_height < $n_h) {
//如果新高度小于容器高度
$r = $n_h / $new_height;
//按高度求出等比例因子
$new_width *= $r;
// //扩展填充后的长度
$new_height *= $r;
// //扩展填充后的高度
$cut_width = ($new_width - $n_w) / 2;//这里一定要用等比例后新的宽度减去容器的宽度除以二得到剪裁的点
//求出裁剪点的长度
}

$this->new = imagecreatetruecolor($n_w,$n_h);
//创建剪裁后的图像
imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
}


//判断图像类型,然后加载图像资源
private function getType($file,$type){
$img = '';
switch($type){
case 1:
$img = imagecreatefromgif($file);
break;
case 2:
$img = imagecreatefromjpeg($file);
break;
case 3:
$img = imagecreatefrompng($file);
break;
default:
Tool::alertBack('请上传图片类型为gif,jpg,png的文件!');
}
return $img;
}
//图像输出
public function out(){
imagepng($this->new,$this->file);//输出
imagedestroy($this->img);//销毁资源
imagedestroy($this->new);//销毁
}
}
posted @ 2016-04-07 02:51  兰小宇  阅读(211)  评论(0编辑  收藏  举报