php 图片缩略图的实现方法,
php 图片缩略图的实现方法,
功能:支持jpg,jpeg,gif,png,bmp图片格式,支持按原图片的比例进行缩放,可以选择在图片缩放的过程中是否需要对图片进行裁切,加入了图片质量控制,可以实现缩略图片质量最高化。完整类的代码如下:
<?<span>php </span><span>/*</span><span>* * 功能:php生成缩略图片的类 </span><span>*/</span> <span>class</span><span> ResizeImage{ </span><span>public</span> <span>$type</span>;<span>//</span><span>图片类型</span> <span>public</span> <span>$width</span>;<span>//</span><span>实际宽度</span> <span>public</span> <span>$height</span>;<span>//</span><span>实际高度</span> <span>public</span> <span>$resize_width</span>;<span>//</span><span>改变后的宽度</span> <span>public</span> <span>$resize_height</span>;<span>//</span><span>改变后的高度</span> <span>public</span> <span>$cut</span>;<span>//</span><span>是否裁图</span> <span>public</span> <span>$srcimg</span>;<span>//</span><span>源图象 </span> <span>public</span> <span>$dstimg</span>;<span>//</span><span>目标图象地址</span> <span>public</span> <span>$im</span>;<span>//</span><span>临时创建的图象</span> <span>public</span> <span>$quality</span>;<span>//</span><span>图片质量</span> <span>function</span> resizeimage(<span>$img</span>,<span>$wid</span>,<span>$hei</span>,<span>$c</span>,<span>$dstpath</span>,<span>$quality</span>=100<span>){ </span><span>$this</span>->srcimg=<span>$img</span><span>; </span><span>$this</span>->resize_width=<span>$wid</span><span>; </span><span>$this</span>->resize_height=<span>$hei</span><span>; </span><span>$this</span>->cut=<span>$c</span><span>; </span><span>$this</span>->quality=<span>$quality</span><span>; </span><span>$this</span>->type=<span>strtolower</span>(<span>substr</span>(<span>strrchr</span>(<span>$this</span>->srcimg,'.'),1));<span>//</span><span>图片的类型</span> <span>$this</span>->initi_img();<span>//</span><span>初始化图象</span> <span>$this</span> -> dst_img(<span>$dstpath</span>);<span>//</span><span>目标图象地址</span> @<span>$this</span>->width=imagesx(<span>$this</span>-><span>im); @</span><span>$this</span>->height=imagesy(<span>$this</span>-><span>im); </span><span>$this</span>->newimg();<span>//</span><span>生成图象</span> @ImageDestroy(<span>$this</span>-><span>im); } </span><span>function</span><span> newimg(){ </span><span>$resize_ratio</span>=(<span>$this</span>->resize_width)/(<span>$this</span>->resize_height);<span>//</span><span>改变后的图象的比例</span> @<span>$ratio</span>=(<span>$this</span>->width)/(<span>$this</span>->height);<span>//</span><span>实际图象的比例</span> <span>if</span>((<span>$this</span>->cut)=='1'){<span>//</span><span>裁图</span> <span>if</span>(<span>$img_func</span>==='imagepng'&&(<span>str_replace</span>('.','',<span>PHP_VERSION</span>)>=512)){ <span>//</span><span>针对php版本大于5.12参数变化后的处理情况</span> <span>$quality</span>=9<span>; } </span><span>if</span>(<span>$ratio</span>>=<span>$resize_ratio</span>){<span>//</span><span>高度优先</span> <span>$newimg</span>=imagecreatetruecolor(<span>$this</span>->resize_width,<span>$this</span>-><span>resize_height); imagecopyresampled(</span><span>$newimg</span>,<span>$this</span>->im,0,0,0,0,<span>$this</span>->resize_width,<span>$this</span>->resize_height,((<span>$this</span>->height)*<span>$resize_ratio</span>),<span>$this</span>-><span>height); imagejpeg(</span><span>$newimg</span>,<span>$this</span>->dstimg,<span>$this</span>-><span>quality); } </span><span>if</span>(<span>$ratio</span><<span>$resize_ratio</span>){<span>//</span><span>宽度优先</span> <span>$newimg</span>=imagecreatetruecolor(<span>$this</span>->resize_width,<span>$this</span>-><span>resize_height); imagecopyresampled(</span><span>$newimg</span>,<span>$this</span>->im,0,0,0,0,<span>$this</span>->resize_width,<span>$this</span>->resize_height,<span>$this</span>->width,((<span>$this</span>->width)/<span>$resize_ratio</span><span>)); imagejpeg(</span><span>$newimg</span>,<span>$this</span>->dstimg,<span>$this</span>-><span>quality); } }</span><span>else</span>{<span>//</span><span>不裁图</span> <span>if</span>(<span>$ratio</span>>=<span>$resize_ratio</span><span>){ </span><span>$newimg</span>=imagecreatetruecolor(<span>$this</span>->resize_width,(<span>$this</span>->resize_width)/<span>$ratio</span><span>); imagecopyresampled(</span><span>$newimg</span>,<span>$this</span>->im,0,0,0,0,<span>$this</span>->resize_width,(<span>$this</span>->resize_width)/<span>$ratio</span>,<span>$this</span>->width,<span>$this</span>-><span>height); imagejpeg(</span><span>$newimg</span>,<span>$this</span>->dstimg,<span>$this</span>-><span>quality); } </span><span>if</span>(<span>$ratio</span><<span>$resize_ratio</span><span>){ @</span><span>$newimg</span>=imagecreatetruecolor((<span>$this</span>->resize_height)*<span>$ratio</span>,<span>$this</span>-><span>resize_height); @imagecopyresampled(</span><span>$newimg</span>,<span>$this</span>->im,0,0,0,0,(<span>$this</span>->resize_height)*<span>$ratio</span>,<span>$this</span>->resize_height,<span>$this</span>->width,<span>$this</span>-><span>height); @imagejpeg(</span><span>$newimg</span>,<span>$this</span>->dstimg,<span>$this</span>-><span>quality); } } } </span><span>function</span> initi_img(){<span>//</span><span>初始化图象</span> <span>if</span>(<span>$this</span>->type=='jpg' || <span>$this</span>->type=='jpeg'<span>){ </span><span>$this</span>->im=imagecreatefromjpeg(<span>$this</span>-><span>srcimg); } </span><span>if</span>(<span>$this</span>->type=='gif'<span>){ </span><span>$this</span>->im=imagecreatefromgif(<span>$this</span>-><span>srcimg); } </span><span>if</span>(<span>$this</span>->type=='png'<span>){ </span><span>$this</span>->im=imagecreatefrompng(<span>$this</span>-><span>srcimg); } </span><span>if</span>(<span>$this</span>->type=='wbm'<span>){ @</span><span>$this</span>->im=imagecreatefromwbmp(<span>$this</span>-><span>srcimg); } </span><span>if</span>(<span>$this</span>->type=='bmp'<span>){ </span><span>$this</span>->im=<span>$this</span>->ImageCreateFromBMP(<span>$this</span>-><span>srcimg); } } </span><span>function</span> dst_img(<span>$dstpath</span>){<span>//</span><span>图象目标地址</span> <span>$full_length</span>=<span>strlen</span>(<span>$this</span>-><span>srcimg); </span><span>$type_length</span>=<span>strlen</span>(<span>$this</span>-><span>type); </span><span>$name_length</span>=<span>$full_length</span>-<span>$type_length</span><span>; </span><span>$name</span>=<span>substr</span>(<span>$this</span>->srcimg,0,<span>$name_length</span>-1<span>); </span><span>$this</span>->dstimg=<span>$dstpath</span><span>; </span><span>//</span><span>echo $this->dstimg;</span> <span> } </span><span>function</span> ImageCreateFromBMP(<span>$filename</span>){ <span>//</span><span>自定义函数处理bmp图片</span> <span>if</span>(!<span>$f1</span>=<span>fopen</span>(<span>$filename</span>,"rb"<span>))returnFALSE; </span><span>$FILE</span>=<span>unpack</span>("vfile_type/Vfile_size/Vreserved/Vbitmap_offset",<span>fread</span>(<span>$f1</span>,14<span>)); </span><span>if</span>(<span>$FILE</span>['file_type']!=19778<span>)returnFALSE; </span><span>$BMP</span>=<span>unpack</span>('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'. '/Vvert_resolution/Vcolors_used/Vcolors_important',<span>fread</span>(<span>$f1</span>,40<span>)); </span><span>$BMP</span>['colors']=<span>pow</span>(2,<span>$BMP</span>['bits_per_pixel'<span>]); </span><span>if</span>(<span>$BMP</span>['size_bitmap']==0)<span>$BMP</span>['size_bitmap']=<span>$FILE</span>['file_size']-<span>$FILE</span>['bitmap_offset'<span>]; </span><span>$BMP</span>['bytes_per_pixel']=<span>$BMP</span>['bits_per_pixel']/8<span>; </span><span>$BMP</span>['bytes_per_pixel2']=<span>ceil</span>(<span>$BMP</span>['bytes_per_pixel'<span>]); </span><span>$BMP</span>['decal']=(<span>$BMP</span>['width']*<span>$BMP</span>['bytes_per_pixel']/4<span>); </span><span>$BMP</span>['decal']-=<span>floor</span>(<span>$BMP</span>['width']*<span>$BMP</span>['bytes_per_pixel']/4<span>); </span><span>$BMP</span>['decal']=4-(4*<span>$BMP</span>['decal'<span>]); </span><span>if</span>(<span>$BMP</span>['decal']==4)<span>$BMP</span>['decal']=0<span>; </span><span>$PALETTE</span>=<span>array</span><span>(); </span><span>if</span>(<span>$BMP</span>['colors']<16777216<span>) { </span><span>$PALETTE</span>=<span>unpack</span>('V'.<span>$BMP</span>['colors'],<span>fread</span>(<span>$f1</span>,<span>$BMP</span>['colors']*4<span>)); } </span><span>$IMG</span>=<span>fread</span>(<span>$f1</span>,<span>$BMP</span>['size_bitmap'<span>]); </span><span>$VIDE</span>=<span>chr</span>(0<span>); </span><span>$res</span>=imagecreatetruecolor(<span>$BMP</span>['width'],<span>$BMP</span>['height'<span>]); </span><span>$P</span>=0<span>; </span><span>$Y</span>=<span>$BMP</span>['height']-1<span>; </span><span>while</span>(<span>$Y</span>>=0<span>) { </span><span>$X</span>=0<span>; </span><span>while</span>(<span>$X</span><<span>$BMP</span>['width'<span>]) { </span><span>if</span>(<span>$BMP</span>['bits_per_pixel']==24<span>) </span><span>$COLOR</span>=<span>unpack</span>("V",<span>substr</span>(<span>$IMG</span>,<span>$P</span>,3).<span>$VIDE</span><span>); </span><span>elseif</span>(<span>$BMP</span>['bits_per_pixel']==16<span>) { </span><span>$COLOR</span>=<span>unpack</span>("n",<span>substr</span>(<span>$IMG</span>,<span>$P</span>,2<span>)); </span><span>$COLOR</span>[1]=<span>$PALETTE</span>[<span>$COLOR</span>[1]+1<span>]; } </span><span>elseif</span>(<span>$BMP</span>['bits_per_pixel']==8<span>) { </span><span>$COLOR</span>=<span>unpack</span>("n",<span>$VIDE</span>.<span>substr</span>(<span>$IMG</span>,<span>$P</span>,1<span>)); </span><span>$COLOR</span>[1]=<span>$PALETTE</span>[<span>$COLOR</span>[1]+1<span>]; } </span><span>elseif</span>(<span>$BMP</span>['bits_per_pixel']==4<span>) { </span><span>$COLOR</span>=<span>unpack</span>("n",<span>$VIDE</span>.<span>substr</span>(<span>$IMG</span>,<span>floor</span>(<span>$P</span>),1<span>)); </span><span>if</span>((<span>$P</span>*2)%2==0)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]>>4);<span>else</span><span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x0F<span>); </span><span>$COLOR</span>[1]=<span>$PALETTE</span>[<span>$COLOR</span>[1]+1<span>]; } </span><span>elseif</span>(<span>$BMP</span>['bits_per_pixel']==1<span>) { </span><span>$COLOR</span>=<span>unpack</span>("n",<span>$VIDE</span>.<span>substr</span>(<span>$IMG</span>,<span>floor</span>(<span>$P</span>),1<span>)); </span><span>if</span>((<span>$P</span>*8)%8==0)<span>$COLOR</span>[1]=<span>$COLOR</span>[1]>>7<span>; </span><span>elseif</span>((<span>$P</span>*8)%8==1)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x40)>>6<span>; </span><span>elseif</span>((<span>$P</span>*8)%8==2)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x20)>>5<span>; </span><span>elseif</span>((<span>$P</span>*8)%8==3)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x10)>>4<span>; </span><span>elseif</span>((<span>$P</span>*8)%8==4)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x8)>>3<span>; </span><span>elseif</span>((<span>$P</span>*8)%8==5)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x4)>>2<span>; </span><span>elseif</span>((<span>$P</span>*8)%8==6)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x2)>>1<span>; </span><span>elseif</span>((<span>$P</span>*8)%8==7)<span>$COLOR</span>[1]=(<span>$COLOR</span>[1]&0x1<span>); </span><span>$COLOR</span>[1]=<span>$PALETTE</span>[<span>$COLOR</span>[1]+1<span>]; } </span><span>else</span><span> returnFALSE; imagesetpixel(</span><span>$res</span>,<span>$X</span>,<span>$Y</span>,<span>$COLOR</span>[1<span>]); </span><span>$X</span>++<span>; </span><span>$P</span>+=<span>$BMP</span>['bytes_per_pixel'<span>]; } </span><span>$Y</span>--<span>; </span><span>$P</span>+=<span>$BMP</span>['decal'<span>]; } </span><span>fclose</span>(<span>$f1</span><span>); </span><span>return</span><span>$res</span><span>; } } </span>?>
使用方法很简单代码如下:
<span>$resizeimage</span>=<span>new</span> ResizeImage('upload/abc.bmp', '120', '90', '0', 'upload/xabc.bmp');
相关阅读:
php生成图片缩略图类代码分享
php批量生成图片缩略图的方法
php 图片缩略图类phpThumb
php 图片缩略图的实现方法
php上传图片生成缩略图(GD库)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
