php 图片等比例裁切,学习笔记-php图像等比例剪裁-2016.4.7

因为小区附近宽带升级,所以笔记暂时没有更新,但是学习一直继续!今天继续,

/**

* 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);//销毁

}

}

原文:http://www.cnblogs.com/lanxiaoyu/p/5361948.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值