使用PHP生成图片的缩略图的方法

2024-06-08 15:32

本文主要是介绍使用PHP生成图片的缩略图的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<?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生成图片的缩略图的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1042585

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依