使用PHP缩略图和剪切图

先说说缩略图,它用得比较多,代码如下:

 

PHP代码
  1. <?php   
  2. header("Content-type: image/png");   
  3. //原图   
  4. $filename='source.jpg';   
  5. //缩放比例 新图/原图   
  6. $percent = '0.5';   
  7. list($width,$height) = getimagesize($filename);   
  8. $newwidth = $width * $percent;   
  9. $newheight = $height * $percent;   
  10. // Load   
  11. $thumb = imagecreatetruecolor($newwidth$newheight);   
  12. $source = imagecreatefromjpeg($filename);   
  13. // Resize   
  14. imagecopyresized($thumb$source, 0, 0, 0, 0, $newwidth$newheight$width$height);   
  15. // Output   
  16. imagepng($thumb);   
  17. ?>  

 

再说说剪切图,就是不缩放,而是从原图中剪切出一块小图,比较个性。代码如下:

 

PHP代码
  1. <?php   
  2. $maxW=300;   
  3. $maxH=300;   
  4. //图片路径   
  5. $link"big.jpg";   
  6. $img = imagecreatefromjpeg($link);   
  7. list($width$height$type$attr) = getimagesize($link);   
  8. $widthnum=ceil($width/$maxW);   
  9. $heightnum=ceil($height/$maxH);   
  10. $iOut = imagecreatetruecolor ($maxW,$maxH);   
  11. //bool imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )   
  12. //将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。   
  13.   
  14. //整图循环切割   
  15. for ($i=0;$i < $heightnum;$i++) {   
  16.  for ($j=0;$j < $widthnum;$j++) {   
  17.  imagecopy($iOut,$img,0,0,($j*$maxW),($i*$maxH),$maxW,$maxH);//复制图片的一部分   
  18.  imagejpeg($iOut,"images/".$i."_".$j.".jpg"); //输出成0_0.jpg,0_1.jpg这样的格式   
  19.  }   
  20. }   
  21.   
  22. //只剪切一个开始部位的小图.复制图片的一部分   
  23.  imagecopy($iOut,$img,0,0,0,0,$maxW,$maxH);   
  24.  imagejpeg($iOut,"images/sm.jpg");   
  25. ?>  

 

注释比较明白,看看就懂了

Tags: 缩略图, 剪切图

« 上一篇 | 下一篇 »

只显示10条记录相关文章

Trackbacks

点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5

发表评论

评论内容 (必填):