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和js交互一例-PHP教程,PHP应用
Jan 03 PHP
在windows服务器开启php的gd库phpinfo中未发现
Jan 13 PHP
php 启动时报错的简单解决方法
Jan 27 PHP
PHP内置过滤器FILTER使用实例
Jun 25 PHP
将FCKeditor导入PHP+SMARTY的实现方法
Jan 15 PHP
php 在字符串指定位置插入新字符的简单实现
Jun 28 PHP
php使用Jpgraph创建折线图效果示例
Feb 15 PHP
ThinkPHP+EasyUI之ComboTree中的会计科目树形菜单实现方法
Jun 09 PHP
PHP流Streams、包装器wrapper概念与用法实例详解
Nov 17 PHP
PHP设计模式之原型设计模式原理与用法分析
Apr 25 PHP
PHP CURL使用详解
Mar 21 PHP
Laravel validate error处理,ajax,json示例
Oct 25 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学习笔记之二 php入门知识
2011/01/12 PHP
在windows服务器开启php的gd库phpinfo中未发现
2013/01/13 PHP
php修改上传图片尺寸的方法
2015/04/14 PHP
WordPress中用于获取文章信息以及分类链接的函数用法
2015/12/18 PHP
PHP利用imagick生成组合缩略图
2016/02/19 PHP
PHP实现数据库统计时间戳按天分组输出数据的方法
2017/10/10 PHP
php获取ajax的headers方法与内容实例
2017/12/27 PHP
在 Laravel 6 中缓存数据库查询结果的方法
2019/12/11 PHP
初学Javascript的一些总结
2008/11/03 Javascript
在javascript中对于DOM的加强
2013/04/11 Javascript
『JavaScript』限制Input只能输入数字实现思路及代码
2013/04/22 Javascript
Javascript 按位取反运算符 (~)
2014/02/04 Javascript
nodejs加密Crypto的实例代码
2016/07/07 NodeJs
原生JS实现图片轮播效果
2016/12/26 Javascript
jQuery源码分析之init的详细介绍
2017/02/13 Javascript
ES6知识点整理之Proxy的应用实例详解
2019/04/16 Javascript
[01:24:34]2014 DOTA2华西杯精英邀请赛5 24 DK VS LGD
2014/05/25 DOTA
Python多进程原理与用法分析
2018/08/21 Python
在python2.7中用numpy.reshape 对图像进行切割的方法
2018/12/05 Python
python url 参数修改方法
2018/12/26 Python
详解python中sort排序使用
2019/03/23 Python
pytest中文文档之编写断言
2019/09/12 Python
python入门之井字棋小游戏
2020/03/05 Python
Django中FilePathField字段的用法
2020/05/21 Python
基于python代码批量处理图片resize
2020/06/04 Python
Python3爬虫中识别图形验证码的实例讲解
2020/07/30 Python
python爬虫智能翻页批量下载文件的实例详解
2021/02/02 Python
网页布局中CSS样式无效的十个重要原因详解
2017/08/10 HTML / CSS
css3隔行变换色实现示例
2014/02/19 HTML / CSS
使用HTML5的表单验证的简单示例
2015/09/09 HTML / CSS
法国在线药房:1001Pharmacies
2021/03/07 全球购物
计算机网络专业个人的自我评价
2013/10/17 职场文书
竞选班干部演讲稿500字
2014/08/20 职场文书
2014年幼儿园教研工作总结
2014/12/04 职场文书
《我的伯父鲁迅先生》教学反思
2016/02/16 职场文书
Android实现图片九宫格
2022/06/28 Java/Android