How to implement image thumbnails in php
Function: Supports jpg, jpeg, gif, png, bmp image formats, supports scaling according to the proportion of the original image, you can choose whether the image needs to be cropped during the image scaling process, and has added image quality control to achieve reduction. Optimize picture quality. The code of the complete class is as follows:
<?<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>?>
The usage method is very simple. The code is as follows:
<span>$resizeimage</span>=<span>new</span> ResizeImage('upload/abc.bmp', '120', '90', '0', 'upload/xabc.bmp');
Related reading:
php generate image thumbnail class code sharing
php method to batch generate image thumbnails
php pictures Thumbnail class phpThumb
php Image thumbnail implementation method
php uploads images to generate thumbnails (GD library)
The above introduces the implementation method of PHP image thumbnails, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

AI Hentai Generator
Generate AI Hentai for free.

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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

How to compress and format images in Vue? In front-end development, we often encounter the need to compress and format images. Especially in mobile development, in order to improve page loading speed and save user traffic, it is critical to compress and format images. In the Vue framework, we can use some tool libraries to compress and format images. Compression using the compressor.js library compressor.js is a JavaS for compressing images

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Resize, crop, rotate, and flip images. First, our original images are 10 images of different sizes downloaded from the Internet, as follows: Operation 1: resize Resize the image to the same size (320,240) fromPILimportImageimporttorchvision.transformsastransforms# Use the PIL library to read in Image and resizeefResizeImage():ifnotos.path.exists(rdir):os.makedirs(rdir)foriinrange(10):im=Image.open(d

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.
