PHP生成缩略图的一个方法类(转)
//使用如下类就可以生成图片缩略图
class resizeimage
{
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//是否裁图
var $cut;
//源图象
var $srcimg;
//目标图象地址
var $dstimg;
//临时创建的图象
var $im; function resizeimage($img, $wid, $hei,$c,$dstpath)
{
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
//图片的类型
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img($dstpath);
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy ($this->im);
}
function newimg()
{
//改变后的图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//实际图象的比例
$ratio = ($this->width)/($this->height);
if(($this->cut)=="1")
//裁图
{
if($ratio>=$resize_ratio)
//高度优先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
//宽度优先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
ImageJpeg ($newimg,$this->dstimg);
}
}
else
//不裁图
{
if($ratio>=$resize_ratio)
{
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
{
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
}
}
//初始化图象
function initi_img()
{
if($this->type=="jpg")
{
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif")
{
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png")
{
$this->im = imagecreatefrompng($this->srcimg);
}
}
//图象目标地址
function dst_img($dstpath)
{
$full_length = strlen($this->srcimg); $type_length = strlen($this->type);
$name_length = $full_length-$type_length; $name = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $dstpath; //echo $this->dstimg;
}
} //就只用下面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高,第4个参数表示是否截图
$resizeimage = new resizeimage($save_path.$file_name, "185", "130", "0",$save_path."c_".$file_name);
此代码来源于网络,经本人证实确实有效
PHP生成缩略图的一个方法类(转)的更多相关文章
- ASP组件AspJpeg(加水印)生成缩略图等使用方法
		ASP组件AspJpeg(加水印)生成缩略图等使用方法 作者: 字体:[增加 减小] 类型:转载 时间:2012-12-17我要评论 ASPJPEG是一款功能相当强大的图象处理组件,用它可以轻松地做出 ... 
- Thinkphp自定义生成缩略图尺寸的方法
		Thinkphp自定义生成缩略图尺寸的方法,本实例中生成两张不同尺寸的图片:第一张是大图350*350,第二张 50*50的缩略图 Image类是Thinkphp系统自带的,可以研究下,这个缩略图类很 ... 
- .net又一个生成缩略图的方法,不变形
		生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 by 何问起 /// ... 
- .net又一个生成缩略图的方法,不变形,非常好用
		生成缩略图是一个十分常用功能,找到了一个方法,重写部分代码,实用又好用,.net又一个生成缩略图的方法,不变形 /// <summary> /// 为图片生成缩略图 by 何问起 /// ... 
- jQuery $.fn.extend()方法类插件
		一.为JQuery原型扩展新的属性和方法,然后在JQuery的实例对象上调用 在 jQuery 中,我们可以使用$.fn.extend()方法来定义一个方法类插件.方法类插件就是首先你使用 jQuer ... 
- python 单例模式,一个类只能生成唯一的一个实例,重写__new__方法详解
		单例:一个类只能生成唯一的一个实例 每个类只要被实例化了,他的私有属性 '_instance'就会被赋值,这样理解对吗 对 #方法1,实现__new__方法 #并在将一个类的实例绑定到类变量_inst ... 
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
		eclipse 中main()函数中的String[] args如何使用? 右击你的项目,选择run as中选择 run configuration,选择arguments总的program argu ... 
- Eclipse快速生成一个JavaBean类的方法
		原文: https://jingyan.baidu.com/article/948f5924156866d80ff5f921.html Eclipse快速生成一个JavaBean类的方法 听语音 | ... 
- C#生成缩略图的方法
		1.需要添加应用System.Drawing.dll 2.命名空间 using System.IO; using System.Drawing; using System.Drawing.Imagin ... 
随机推荐
- 实习培训——Servlet(7)
			实习培训——Servlet(7) 1 Servlet 异常处理 当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类 ... 
- 实习培训——Servlet(6)
			实习培训——Servlet(6) 1 Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的 ... 
- HDU1010:Tempter of the Bone(dfs+剪枝)
			http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ... 
- hdu1505City Game(扫描线)
			http://acm.hdu.edu.cn/showproblem.php?pid=1505 题意:R为被占位置,F为空位,求出最大子空矩阵大小*3. 思路:1.悬线法,记录每个位置的悬线能到达的左边 ... 
- Hive的安装与配置
			1.因为我使用MySQL做为Hive的元数据库,所以先安装MySQL. 参考:http://www.cnblogs.com/hunttown/p/5452205.html 登录命令:mysql -h主 ... 
- sklearn总览
- react-native 0.57 run-ios 失败解决办法
			React Native 日常报错 'config.h' file not found 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_ ... 
- PTA 团体程序设计天梯赛 L3-020 至多删三个字符
			$f[i][j]$表示到第$i$个字符,已经删去了$j$个字符的方案数. 显然的转移: $f[i][j] = f[i - 1][j] + f[i - 1][j - 1]$ 但是这样会有重复,我们考虑什 ... 
- Repeater 控件使用总结
			关于Repeater控件使用的一些总结,希望能对将来有机会看到这篇日志的同事有所帮助.也是为了在自己开发有所遗忘的时候能够参考一下.前言:Repeater是一个迭代控件,什么是迭代控件呢?书本上的 ... 
- 【VS Hacks】定制VS
			# Hack 24 定制快捷键 VS能够做很多键盘的配置,其实在VS中目前已经发现有很多的快捷键了,但是在这个技巧篇里会学到如何创建新的快捷键,以及编辑已有的快捷键.VS中包含很多的命令,只有其中 ... 
