Heim >Backend-Entwicklung >PHP-Tutorial >php 图片缩略图的实现方法

php 图片缩略图的实现方法

WBOY
WBOYOriginal
2016-07-29 08:55:591328Durchsuche

功能:支持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>$resize_ratio){<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>$resize_ratio<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'])
   {
    <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>$BMP['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库)

以上就介绍了php 图片缩略图的实现方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn