php 缩略图生成类,php生成图片缩略图类
/**
* @abstract 生成图片的缩略图,可以指定任意尺寸,生成的图片为png格式
* @example
* $file = 'test.png';
* $th =new Thumbnail();
* $th->GenerateThumbnail($file, 400, 500);
*
*/
class Thumbnail{
/**
* @var string $from 源图片
*/
private $from;
/**
* @var string $name 缩略图的文件名
*/
private $name = '';
/**
* @var 原图宽
*/
private $rWidth;
/**
* @var 原图高
*/
private $rHeight;
/**
* @var 缩略图宽
*/
private $tWidth;
/**
* @var 缩略图高
*/
private $tHeight;
/**
* @var 实际缩放到的宽度
*/
private $width;
/**
* @var 实际缩放到的高度
*/
private $height;
public function __construct(){
try{
if(!function_exists('gd_info')){
throw new Exception('Must GD extension is enabled');
}
}
catch(Exception $e){
$msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
echo $msg;
exit;
}
}
/**
* @var $from 原图像
* @var $width 生成的缩略图的宽
* @var $height 生成缩略图的高
* @var $name 生成的缩略图的文件名,不带后缀
* @return string 生成的缩略图
*/
public function GenerateThumbnail($from, $width, $height, $name=''){
try{
if(!file_exists($from)){
throw new Exception('File does not exist');
}
if($width <= 0){
throw new Exception('The width is invalid');
}
if($height <= 0){
throw new Exception('The height is invalid');
}
$this->from = $from;
$this->tWidth = $width;
$this->tHeight = $height;
if(!empty($name)){
$this->name = $name;
}
else{
$this->name = date('Ymd') . mt_rand(0, 9999);
}
$this->createThumbnail();
}
catch(Exception $e){
$msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
echo $msg;
exit;
}
}
public function getThumbnail(){
return $this->name;
}
/**
* 生成缩略图文件
*/
private function createThumbnail(){
try{
//读取原始图像信息
$sourceInfo = getimagesize($this->from);
$this->rWidth = $sourceInfo[0];
$this->rHeight = $sourceInfo[1];
//创建缩略图图像资源句柄
$new_pic = imagecreatetruecolor($this->tWidth, $this->tHeight);
//原图绘制到缩略图的x、y坐标
$x = 0;
$y = 0;
//创建原始图像资源句柄
$source_pic = '';
switch ($sourceInfo[2]){
case 1: $source_pic = imagecreatefromgif($this->from); //gif
break;
case 2: $source_pic = imagecreatefromjpeg($this->from); //jpg
break;
case 3: $source_pic = imagecreatefrompng($this->from); //png
break;
default: throw new Exception('Does not support this type of image');
}
//计算缩放后图像实际大小
//原图宽高均比缩略图大
if($this->rWidth > $this->tWidth && $this->rHeight > $this->tHeight){
$midw = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例
$midh = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例
//那个缩小的比例大以那个为准
if($midw > $midh){
$this->width = $this->tWidth;
$this->height = $this->rHeight - floor($this->rHeight * $midw);
$y = ($this->tHeight - $this->height) / 2;
}
else{
$this->width = $this->rWidth - floor($this->rWidth * $midh);
$this->height = $this->tHeight;
$x = ($this->tWidth - $this->width) / 2;
}
}
//原图宽高均比缩略图小
elseif($this->rWidth < $this->tWidth && $this->rHeight < $this->tHeight){
$midw = ($this->tWidth - $this->rWidth) / $this->rWidth; //宽放大的比例
$midh = ($this->tHeight - $this->rHeight) / $this->rHeight; //高放大的比例
//那个放大的比例小以那个为准
if($midw < $midh){
$this->width = $this->tWidth;
$this->height = $this->rHeight + floor($this->rHeight * $midw);
$y = ($this->tHeight - $this->height) / 2;
}
else{
$this->width = $this->rWidth + floor($this->rWidth * $midh);
$this->height = $this->tHeight;
$x = ($this->tWidth - $this->width) / 2;
}
}
//原图宽小于缩略图宽,原图高大于缩略图高
elseif($this->rWidth < $this->tWidth && $this->rHeight > $this->tHeight){
$mid = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例
$this->width = $this->rWidth - floor($this->rWidth * $mid);
$this->height = $this->rHeight - floor($this->rHeight * $mid);
$x = ($this->tWidth - $this->width) / 2;
$y = ($this->tHeight - $this->height) / 2;
}
//原图宽大于缩略图宽,原图高小于缩略图高
elseif($this->rWidth > $this->tWidth && $this->rHeight < $this->tHeight){
$mid = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例
$this->width = $this->rWidth - floor($this->rWidth * $mid);
$this->height = $this->rHeight - floor($this->rHeight * $mid);
$x = ($this->tWidth - $this->width) / 2;
$y = ($this->tHeight - $this->height) / 2;
}
else{
throw new Exception('Resize error');
}
//给缩略图添加白色背景
$bg = imagecolorallocate($new_pic, 255, 255, 255);
imagefill($new_pic, 0, 0, $bg);
//缩小原始图片到新建图片
imagecopyresampled($new_pic, $source_pic, $x, $y, 0, 0, $this->width, $this->height, $this->rWidth, $this->rHeight);
//输出缩略图到文件
imagepng($new_pic, $this->name.'.png');
imagedestroy($new_pic);
imagedestroy($source_pic);
}
catch(Exception $e){
$msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
echo $msg;
exit;
}
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
php 缩略图生成类,php生成图片缩略图类相关推荐
- php 生成缩略图保存,PHP批量生成图片缩略图的方法
//用PHP批量生成图片缩略图 function mkdirs($dirname,$mode=0777) //创建目录(目录, [模式]) { if(!is_dir($dirname)) { mkdi ...
- php图片缩略图的方法,php生成图片缩略图的方法
本文实例讲述了php生成图片缩略图的方法.分享给大家供大家参考.具体如下: 这里需要用到GD2 library function make_thumb($src,$dest,$desired_widt ...
- php图片生成缩略图_PHP生成图片缩略图类示例
本文实例讲述了PHP生成图片缩略图类.分享给大家供大家参考,具体如下: class App_image_helper { protected $imgFileName; protected $imgW ...
- php 上传类 缩略图,php图片文件上传类 (附自动生成缩略图) | 学步园
/** 作 者:冻结回忆 (linghunts@163.com); 功 能:文件上传类 支持文件夹自动分组保存(2008-01/09); 时 间:2007-10-17; 创建类:参数(文件域,文件原名 ...
- php图片生成缩略图_PHP实现生成图片缩略图函数
本文主要和大家介绍了PHP基于GD库实现的生成图片缩略图函数,涉及php针对图片属性相关操作技巧,需要的朋友可以参考下,希望能帮助到大家. /** * 生成缩略图函数(支持图片格式:gif.jpeg. ...
- 生成图片_GitHub Star 3.2K Java 图片缩略图生成库
大家好,我是你们的章鱼猫. 最近有一个需求是需要给网站的图片生成一个高质量的缩略图,方便在有些场景中展示.而在 Java 中,如果要对图片进行处理,需要了解和使用 Image I/O API.Java ...
- 缩放图片工具类,创建缩略图、伸缩图片比例
支持将Image的宽度.高度缩放到指定width.height,并保存在指定目录 通过目标对象的大小和标准(指定)大小计算出图片缩小的比例 可以设置图片缩放质量,并且可以根据指定的宽高缩放图片 pac ...
- java 生成缩略图_Java实现等比例缩略图
1.简介:Web应用为上传图片生成缩略图是常见的基本功能,通过缩略图生成提高了信息浏览时的性能,在保证用户使用体验的同时减少了数据传输量. 2.实现图片等比例缩略图生成,方式及相关工具介绍: (1)T ...
- (转)PHP生成图片缩略图
注:此功能依赖GD2图形库 最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图 试用了一下后,发现有这样几个问题: 1.png图片生成的缩略图是jpg格式的 2.png图片 ...
最新文章
- 电子病历、HL7交流QQ群:14739311欢迎一起交流
- mysql容器创建命令_centos7下docker创建基本的mysql容器
- SpringBoot 应用程序启动过程探秘
- 经典面试题(17):以下代码将输出的结果是什么?
- 嵌套 思维导图_看我怎么用思维导图,来轻松学习JavaScript,值得收藏
- seaweedfs-client适配高版本的seaweedfs服务
- 怎么样对阿里云ECS主机进行绑定域名
- Stata制作限制立方样条(RCS)(2)
- 百度推广系列之优化之笔
- RTSP协议视频安防综合管理平台EasyNVR与海康萤石云平台运行机制差异对比说明
- Acer TravelMateP249主板上最容易被人忽略的問題
- SQL server完整性约束的操作
- ARM Compiler相关资料汇总
- 劲乐园合歌(幽灵圣典+飞吧喜鹊+唯一+v3+幽灵圣典2)1铃声 劲乐...
- 这个水卡算法规律有会的吗?
- 计算机7层网络以及每层协议
- 行路难,行路难,多歧路,今安在?
- c语言中减号算一个字符吗,C语言中指针的加减运算
- 淘宝与日本雅虎网购平台上线;Novell公司推出SUSE Meego系统(每日关注20100602)...
- 在做嵌入式开发的时候难免手抖,IAR软件窗口恢复操作