学习笔记-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);
}


public function thumb($new_height,$new_width){
if ($this->width < $this->height) {
//让长度和新高度等比例
$new_width = ($new_height / $this->height) * $this->width;
}else{
//让新高度和新长度等比例
$new_height = ($new_width / $this->width) * $this->height;
}
$this->new = imagecreatetruecolor($new_width,$new_height);
//创建剪裁后的图像
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:44  兰小宇  阅读(293)  评论(0编辑  收藏  举报