Home >Backend Development >PHP Tutorial >How to implement image thumbnails in php, _PHP tutorial

How to implement image thumbnails in php, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:55:52765browse

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>实际宽度</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 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:
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