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 相关文章推荐
linux下删除7天前日志的代码(php+shell)
Jan 02 PHP
提高define性能的php扩展hidef的安装和使用
Jun 14 PHP
ASP和PHP实现生成网站快捷方式并下载到桌面的方法
May 08 PHP
PHP实现克鲁斯卡尔算法实例解析
Aug 22 PHP
php调用mysql存储过程实例分析
Dec 29 PHP
PHP计算百度地图两个GPS坐标之间距离的方法
Jan 09 PHP
PHP中$_SERVER使用说明
Jul 05 PHP
thinkPHP导出csv文件及用表格输出excel的方法
Dec 30 PHP
[原创]php实现数组按拼音顺序排序的方法
May 03 PHP
PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)实例详解
Apr 20 PHP
laravel 解决crontab不执行的问题
Oct 22 PHP
Laravel框架源码解析之入口文件原理分析
May 14 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
用php实现像JSP,ASP里Application那样的全局变量
2007/01/12 PHP
PHP 模拟$_PUT实现代码
2010/03/15 PHP
discuz的php防止sql注入函数
2011/01/17 PHP
PHP Try-catch 语句使用技巧
2016/02/28 PHP
解决 firefox 不支持 document.all的方法
2007/03/12 Javascript
jquery 可排列的表实现代码
2009/11/13 Javascript
javascript showModalDialog 多层模态窗口实现页面提交及刷新的代码
2009/11/28 Javascript
Eclipse配置Javascript开发环境图文教程
2015/01/29 Javascript
AngularJS中的API(接口)简单实现
2016/07/28 Javascript
深入学习jQuery中的data()
2016/12/22 Javascript
jquery处理checkbox(复选框)是否被选中实例代码
2017/06/12 jQuery
浅谈原生JS中的延迟脚本和异步脚本
2017/07/12 Javascript
JS数组push、unshift、pop、shift方法的实现与使用方法示例
2020/04/29 Javascript
js实现ajax的用户简单登入功能
2020/06/18 Javascript
JS实现canvas简单小画板功能
2020/06/23 Javascript
vue项目接口域名动态获取操作
2020/08/13 Javascript
[01:33:14]LGD vs VP Supermajor 败者组决赛 BO3 第二场 6.10
2018/07/04 DOTA
[50:12]EG vs Fnatic 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
[56:13]DOTA2-DPC中国联赛定级赛 LBZS vs Phoenix BO3第一场 1月10日
2021/03/11 DOTA
python提示No module named images的解决方法
2014/09/29 Python
Python fileinput模块使用介绍
2014/11/30 Python
Python实现对excel文件列表值进行统计的方法
2015/07/25 Python
Python 字符串转换为整形和浮点类型的方法
2018/07/17 Python
python 画出使用分类器得到的决策边界
2019/08/21 Python
完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误
2020/04/01 Python
opencv之颜色过滤只留下图片中的红色区域操作
2020/06/05 Python
伦敦高达60%折扣的钻石珠宝商:Purely Diamonds
2018/06/24 全球购物
实习护理工作自我评价
2013/09/25 职场文书
区域销售主管岗位职责
2014/06/15 职场文书
活动新闻稿范文
2015/07/17 职场文书
二年级数学教学反思
2016/02/16 职场文书
php 防护xss,PHP的防御XSS注入的终极解决方案
2021/04/01 PHP
使用pytorch实现线性回归
2021/04/11 Python
基于flask实现五子棋小游戏
2021/05/25 Python
Python实现灰色关联分析与结果可视化的详细代码
2022/03/25 Python
nginx搭建NFS网络文件系统
2022/04/14 Servers