PHP에서 이미지 썸네일을 구현하는 방법

WBOY
풀어 주다: 2016-07-29 08:55:59
원래의
1323명이 탐색했습니다.

기능: 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 라이브러리)

위 내용은 내용적인 측면을 포함하여 PHP 이미지 썸네일의 구현 방법을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿