国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
How to implement image thumbnails in php,
Home Backend Development PHP Tutorial How to implement image thumbnails in php, _PHP tutorial

How to implement image thumbnails in php, _PHP tutorial

Jul 12, 2016 am 08:55 AM
gif jpeg jpg p php Function picture accomplish support method of

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 to Whether the image needs to be cropped during the image scaling process, image quality control has been added to maximize the quality of the thumbnail image. 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>實(shí)際寬度</span>
  <span>public</span> <span>$height</span>;<span>//</span><span>實(shí)際高度</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>目標(biāo)圖象地址</span>
  <span>public</span> <span>$im</span>;<span>//</span><span>臨時(shí)創(chuàng)建的圖象</span>
  <span>public</span> <span>$quality</span>;<span>//</span><span>圖片質(zhì)量</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>目標(biāo)圖象地址</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>實(shí)際圖象的比例</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>針對(duì)php版本大于5.12參數(shù)變化后的處理情況</span>
     <span>$quality</span>=9<span>;
    }
    </span><span>if</span>(<span>$ratio</span>>=<span>$resize_ratio</span>){<span>//</span><span>高度優(yōu)先</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>寬度優(yōu)先</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>圖象目標(biāo)地址</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>自定義函數(shù)處理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 is very simple and the code is as follows:

<span>$resizeimage</span>=<span>new</span> ResizeImage('upload/abc.bmp', '120', '90', '0', 'upload/xabc.bmp');

Related reading:

Sharing code for php generating image thumbnails

How to batch generate image thumbnails in php

php image thumbnail class phpThumb

How to implement image thumbnails in php

php uploads images to generate thumbnails (GD library)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1114475.htmlTechArticlephp Image thumbnail implementation method, function: support jpg, jpeg, gif, png, bmp image format, support Scale according to the proportion of the original image. You can choose whether you need to adjust the image during the scaling process...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I stay up-to-date with the latest PHP developments and best practices? How do I stay up-to-date with the latest PHP developments and best practices? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

What is PHP, and why is it used for web development? What is PHP, and why is it used for web development? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

How do I validate user input in PHP to ensure it meets certain criteria? How do I validate user input in PHP to ensure it meets certain criteria? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

What is data serialization in PHP (serialize(), unserialize())? What is data serialization in PHP (serialize(), unserialize())? Jun 22, 2025 am 01:03 AM

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

How do I embed PHP code in an HTML file? How do I embed PHP code in an HTML file? Jun 22, 2025 am 01:00 AM

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

What are the best practices for writing clean and maintainable PHP code? What are the best practices for writing clean and maintainable PHP code? Jun 24, 2025 am 12:53 AM

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

How do I execute SQL queries using PHP? How do I execute SQL queries using PHP? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles