php实现图片缩略图的方法


Posted in PHP onMarch 29, 2016

本段代码实现功能有这些:

支持jpg,jpeg,gif,png,bmp图片格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质量控制,可以实现缩略图片质量最高化。

完整类的代码如下:

<?php
/**
* 功能:php生成缩略图片的类
*/
class ResizeImage{
public $type;//图片类型
public $width;//实际宽度
public $height;//实际高度
public $resize_width;//改变后的宽度
public $resize_height;//改变后的高度
public $cut;//是否裁图
public $srcimg;//源图象 
public $dstimg;//目标图象地址
public $im;//临时创建的图象
public $quality;//图片质量
function resizeimage($img,$wid,$hei,$c,$dstpath,$quality=100){
$this->srcimg=$img;
$this->resize_width=$wid;
$this->resize_height=$hei;
$this->cut=$c;
$this->quality=$quality;
$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($img_func==='imagepng'&&(str_replace('.','',PHP_VERSION)>=512)){ //针对php版本大于5.12参数变化后的处理情况
$quality=9;
}
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,$this->quality);
}
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,$this->quality);
}
}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,$this->quality);
}
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,$this->quality);
}
}
}
function initi_img(){//初始化图象
if($this->type=='jpg' || $this->type=='jpeg'){
$this->im=imagecreatefromjpeg($this->srcimg);
}
if($this->type=='gif'){
$this->im=imagecreatefromgif($this->srcimg);
}
if($this->type=='png'){
$this->im=imagecreatefrompng($this->srcimg);
}
if($this->type=='wbm'){
@$this->im=imagecreatefromwbmp($this->srcimg);
}
if($this->type=='bmp'){
$this->im=$this->ImageCreateFromBMP($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;
}
function ImageCreateFromBMP($filename){ //自定义函数处理bmp图片
if(!$f1=fopen($filename,"rb"))returnFALSE;
$FILE=unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset",fread($f1,14));
if($FILE['file_type']!=19778)returnFALSE;
$BMP=unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
'/Vvert_resolution/Vcolors_used/Vcolors_important',fread($f1,40));
$BMP['colors']=pow(2,$BMP['bits_per_pixel']);
if($BMP['size_bitmap']==0)$BMP['size_bitmap']=$FILE['file_size']-$FILE['bitmap_offset'];
$BMP['bytes_per_pixel']=$BMP['bits_per_pixel']/8;
$BMP['bytes_per_pixel2']=ceil($BMP['bytes_per_pixel']);
$BMP['decal']=($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal']-=floor($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal']=4-(4*$BMP['decal']);
if($BMP['decal']==4)$BMP['decal']=0;
$PALETTE=array();
if($BMP['colors']<16777216)
{
$PALETTE=unpack('V'.$BMP['colors'],fread($f1,$BMP['colors']*4));
}
$IMG=fread($f1,$BMP['size_bitmap']);
$VIDE=chr(0);
$res=imagecreatetruecolor($BMP['width'],$BMP['height']);
$P=0;
$Y=$BMP['height']-1;
while($Y>=0)
{
$X=0;
while($X<$BMP['width'])
{
if($BMP['bits_per_pixel']==24)
$COLOR=unpack("V",substr($IMG,$P,3).$VIDE);
elseif($BMP['bits_per_pixel']==16)
{
$COLOR=unpack("n",substr($IMG,$P,2));
$COLOR[1]=$PALETTE[$COLOR[1]+1];
}
elseif($BMP['bits_per_pixel']==8)
{
$COLOR=unpack("n",$VIDE.substr($IMG,$P,1));
$COLOR[1]=$PALETTE[$COLOR[1]+1];
}
elseif($BMP['bits_per_pixel']==4)
{
$COLOR=unpack("n",$VIDE.substr($IMG,floor($P),1));
if(($P*2)%2==0)$COLOR[1]=($COLOR[1]>>4);else$COLOR[1]=($COLOR[1]&0x0F);
$COLOR[1]=$PALETTE[$COLOR[1]+1];
}
elseif($BMP['bits_per_pixel']==1)
{
$COLOR=unpack("n",$VIDE.substr($IMG,floor($P),1));
if(($P*8)%8==0)$COLOR[1]=$COLOR[1]>>7;
elseif(($P*8)%8==1)$COLOR[1]=($COLOR[1]&0x40)>>6;
elseif(($P*8)%8==2)$COLOR[1]=($COLOR[1]&0x20)>>5;
elseif(($P*8)%8==3)$COLOR[1]=($COLOR[1]&0x10)>>4;
elseif(($P*8)%8==4)$COLOR[1]=($COLOR[1]&0x8)>>3;
elseif(($P*8)%8==5)$COLOR[1]=($COLOR[1]&0x4)>>2;
elseif(($P*8)%8==6)$COLOR[1]=($COLOR[1]&0x2)>>1;
elseif(($P*8)%8==7)$COLOR[1]=($COLOR[1]&0x1);
$COLOR[1]=$PALETTE[$COLOR[1]+1];
}
else
returnFALSE;
imagesetpixel($res,$X,$Y,$COLOR[1]);
$X++;
$P+=$BMP['bytes_per_pixel'];
}
$Y--;
$P+=$BMP['decal'];
}
fclose($f1);
return$res;
}
}
?>

使用方法很简单代码如下:

$resizeimage=new ResizeImage('upload/abc.bmp', '120', '90', '0', 'upload/xabc.bmp');

以上代码是小编给大家分享的php实现图片缩略图的方法,希望对大家有所帮助!

PHP 相关文章推荐
PHP 配置open_basedir 让各虚拟站点独立运行
Nov 12 PHP
php xml 入门学习资料
Jan 01 PHP
使用NetBeans + Xdebug调试PHP程序的方法
Apr 12 PHP
linux下使用ThinkPHP需要注意大小写导致的问题
Aug 02 PHP
PHP echo,print,printf,sprintf函数之间的区别与用法详解
Nov 27 PHP
php实现文件下载简单示例(代码实现文件下载)
Mar 10 PHP
DOM基础及php读取xml内容操作的方法
Jan 23 PHP
php实现的xml操作类
Jan 15 PHP
PHP命名空间与自动加载机制的基础介绍
Aug 25 PHP
Yii框架的布局文件实例分析
Sep 04 PHP
Laravel 使用查询构造器配合原生sql语句查询的例子
Oct 12 PHP
thinkphp5 路由分发原理
Mar 18 PHP
YII动态模型(动态表名)支持分析
Mar 29 #PHP
Yii操作数据库实现动态获取表名的方法
Mar 29 #PHP
Yii数据库缓存实例分析
Mar 29 #PHP
PHP+HTML+JavaScript+Css实现简单爬虫开发
Mar 28 #PHP
Yii框架上传图片用法总结
Mar 28 #PHP
Yii开启片段缓存的方法
Mar 28 #PHP
CI操作cookie的方法分析(基于helper类库)
Mar 28 #PHP
You might like
kohana框架上传文件验证规则写法示例
2014/07/14 PHP
PHP下的浮点运算不准的解决方法
2016/10/27 PHP
php base64 编码与解码实例代码
2017/03/21 PHP
thinkPHP框架实现的无限回复评论功能示例
2018/06/09 PHP
兼容FireFox 的 js 日历 支持时间的获取
2009/03/04 Javascript
模拟电子签章盖章效果的jQuery插件源码
2013/06/24 Javascript
SuperSlide2实现图片滚动特效
2014/06/20 Javascript
js获取表格的行数和列数的方法
2015/10/23 Javascript
js中flexible.js实现淘宝弹性布局方案
2020/06/23 Javascript
Bootstrap弹出带合法性检查的登录框实例代码【推荐】
2016/06/23 Javascript
jquery uploadify如何取消已上传成功文件
2017/02/08 Javascript
React中ES5与ES6写法的区别总结
2017/04/21 Javascript
详解Vue中过度动画效果应用
2017/05/25 Javascript
Vue2.0使用嵌套路由实现页面内容切换/公用一级菜单控制页面内容切换(推荐)
2019/05/08 Javascript
小程序接口的promise化的实现方法
2019/12/11 Javascript
JavaScript实现消消乐的源代码
2021/01/12 Javascript
[03:21]【TI9纪实】Old Boys
2019/08/23 DOTA
python获取文件版本信息、公司名和产品名的方法
2014/10/05 Python
Python单元测试unittest的具体使用示例
2018/12/17 Python
python之pexpect实现自动交互的例子
2019/07/25 Python
python英语单词测试小程序代码实例
2019/09/09 Python
python对输出的奇数偶数排序实例代码
2020/12/04 Python
检测浏览器对HTML5和CSS3支持度的方法
2015/06/25 HTML / CSS
介绍一下代理模式(Proxy)
2014/10/17 面试题
房地产广告词大全
2014/03/19 职场文书
学雷锋标语
2014/06/25 职场文书
奶茶店创业计划书
2014/08/14 职场文书
小学运动会演讲稿
2014/08/25 职场文书
专题组织生活会发言材料
2014/10/17 职场文书
企业财务总监岗位职责
2015/04/03 职场文书
2015新员工工作总结范文
2015/10/15 职场文书
考生诚信考试承诺书(2016版)
2016/03/25 职场文书
详解Redis实现限流的三种方式
2021/04/27 Redis
浅析Python中的套接字编程
2021/06/22 Python
在 HTML 页面中使用 React的场景分析
2022/01/18 Javascript
搭建Yolov5服务器
2022/04/30 Servers