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 ...
随机推荐
- 五、Mosquitto 高级应用之权限管理
本文将讲解 Mosquitto 权限管理.如果还没有搭建 Mosquitto 服务的可以参考我的另外两篇文章<< 一.Mosquitto 介绍&安装>> << ...
- 【java】System.out重定向到文件,并重定向会console上
重定向到文件: System.setOut(new PrintStream(new File("data\\train.1.scale"))); 重定向回console: //把输 ...
- Log Parser 2.2 + Log Parser Lizard GUI 分析IIS日志示例
Log Parser 日志分析工具,用命令行操作,可以分析 IIS logs,event logs,active directory,log4net,file system,t-sql Log Par ...
- 批量导入数据到mssql数据库的
概述 批量导入数据到数据库中,我们有好几种方式. 从一个数据表里生成数据脚本,到另一个数据库里执行脚本 从EXCEL里导入数据 上面两种方式,导入的数据都会生成大量的日志.如果批量导入5W条数据到数据 ...
- 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- uva12206 后缀数组
这题说的是给了一串字符 我们要将这个字符 中找出至少出现m次的最长字符串 一个字符课多次使用 利用后缀数组计算最长的lcp 这里有一个点 记得将后缀数组中加入一个空串 如果遇到全部相同的字符时 没办法 ...
- Java overload和override的区别分析
Java overload和override的区别分析 方法的重写(Overriding)和重载(Overloading)是Java多态性的不同表现.重写(Overriding)是父类与子类之间多态性 ...
- linux常用命令:which 命令
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置 ...
- IO(字节流)
1. 字节流类以InputStream 和 OutputStream为顶层类,他们都是抽象类(abstract) 2. 最重要的两种方法是read()和write(),它们分别对数据的字节进行读写.两 ...
- 微信小程序——3、逻辑js文件
逻辑层js文件 微信小程序前端进行了层次划分,分为逻辑层和视图层.逻辑层实现对数据的加工和处理.与HTML页面相似,逻辑层使用JavaScript编写.逻辑层将数据处理后发送至视图层,同时接受视图层的 ...