使用PHP实现生成固定大小图片缩略图功能(智能裁剪,图片不失真)
来源:互联网 发布:mysql企业版价格 编辑:程序博客网 时间:2024/04/30 08:26
<?php//图像处理类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->getFromImg($this->file, $this->type); } //缩略图(固定长高容器,图像等比例,扩容填充,裁剪)[固定了大小,不失真,不变形] 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_width = 0; $_cut_height = 0; if ($this->width < $this->height) { $new_width = ($new_height / $this->height) * $this->width; } 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,$_cut_width,$_cut_height,$new_width,$new_height,$this->width,$this->height); } //加载图片,各种类型,返回图片的资源句柄 private function getFromImg($_file, $_type) { switch ($_type) { case 1 : $img = imagecreatefromgif($_file); break; case 2 : $img = imagecreatefromjpeg($_file); break; case 3 : $img = imagecreatefrompng($_file); break; default: Tool::alertBack('警告:此图片类型本系统不支持!'); } return $img; } //图像输出 public function out() { imagepng($this->new,$this->file); imagedestroy($this->img); imagedestroy($this->new); }} ?> //使用方法:$_path = 'd:/1.jpg';$_img = new Image($_path);//$_path为图片文件的路径 $_img->thumb(150, 100); $_img->out();
0 0
- 使用PHP实现生成固定大小图片缩略图功能(智能裁剪,图片不失真)
- 使用PHP实现生成固定大小图片缩略图功能(智能裁剪,图片不失真)
- php实现等比例生成图片缩略图不失真
- 利用PHP结合GD实现图片裁剪和缩略图功能
- php裁剪上传图片生成缩略图
- 生成缩略图(不失真)
- 生成缩略图(不失真)
- node.js图片剪切 将不同大小的图片不失真的剪切为固定大小
- GTK在windwosXP+VS2010实现,图片打开、显示放大缩小功能(图片不失真)
- PHP生成图片缩略图
- PHP生成图片缩略图
- PHP图片生成缩略图
- PHP生成图片缩略图
- PHP 生成图片缩略图
- PHP生成图片缩略图
- PHP 生成图片缩略图
- PHP生成图片缩略图
- PHP生成图片缩略图
- 探索推荐引擎内部的秘密,第 1 部分: 推荐引擎初探
- Spring Security3.1实践
- The Building Blocks- Components of EA Part 2- Process, People, Network and Time
- Android音频系统之音频框架
- DNS记录类型介绍(A记录、MX记录、NS记录等)
- 使用PHP实现生成固定大小图片缩略图功能(智能裁剪,图片不失真)
- hibernate中的查询语句
- 获取html参数
- Android应用开发多语言文件夹
- MySQL更改字符集
- ios开发中改变nib文件中的控件大小
- 活动图
- UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)
- 从工程师到项目经理