<转>PHP函数补完:ImageCopyResamples()

PHP缩放图像有两种方法:

  • ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。
  • ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑,质量较好,但该函数的速度比 ImageCopyResized() 慢。
01 if($src_h){
02     $thumb_w = $size;
03     $thumb_h = intval($src_h / $src_w * $size);
04 }else{
05     $thumb_h = $size;
06     $thumb_w = intval($src_w / $src_h * $size);
07 }
08  
09 $thumb = imagecreatetruecolor($thumb_w, $thumb_h);
10 // 旧方法,不过产出来的画质效果极差
11 //imagecopyresized($thumb, $src, 0, 0, 0, 0, $thumb_w, $thumb_h, $src_w, $src_h);
12 imagecopyresampled($thumb, $src, 0, 0, 0, 0, $thumb_w, $thumb_h, $src_w, $src_h);
13  
14 $file= array_pop(explode("/",$filename));
15  
16 imagejpeg($thumb, "/tmp/thumb/i-$size-".$file);

两个函数的参数是一样的。如下:

  • ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
  • ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

它们两个都是从原图像(source)中抓取特定位置(sx,sy)复制图像qu区域到目标t图像(destination)的特定位置 (dx,dy)。另外dw,dh指定复制的图像区域在目标图像上的大小,sw,sh指定从原图像复制的图像区域的大小。如果有ps经验的话,就相当于在原 图像选择一块区域,剪切移动到目的图像上,同时有拉伸或缩小的操作。

在php 手册上写imagecopyresampled 和imagecopyresized 主要的不同点如下,就是让他画质更好,更接近原色:imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values​​ so that, in particular, reducing the size of an image still retains a great deal of clarity

imagecopyresampled() 和imagecopyresized() 的差异很明显,跟本不需要考虑使用imagecopyresized() ,除非是使用gif ,否则只有imagecopyresampled 可以看而已。

本例将以原来的四分之一大小显示图像。

01 <?php
02 // 指定文件路径和缩放比例
03 $filename = 'test.jpg';
04 $percent = 0.5;
05 // 指定头文件Content typezhi值
06 header('Content-type: image/jpeg');
07 //  获取图片的宽高
08 list($width, $height) = getimagesize($filename);
09 $newwidth $width * $percent;
10 $newheight = $height * $percent;
11 //  创建一个图片。接收参数分别为宽高,返回生成的资源句柄
12 $thumb = imagecreatetruecolor($newwidth$newheight);
13 //获取源文件资源句柄。接收参数为图片路径,返回句柄
14 $source =  imagecreatefromjpeg($filename);
15 // 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
16 imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight$width, $height);
17 // 输出给浏览器
18 imagejpeg($thumb);
19 ?>

本例将把一幅图像按最宽或最高 200 像素来显示。

01 <?php
02 // 文件路径
03 $filename = 'test.jpg';
04 // 最大宽高
05 $width = 200;
06 $height = 200;
07 // 设置http头Content type值
08 header('Content-type:  image/jpeg');
09 // 获取图片宽高
10 list($width_orig, $height_orig) =  getimagesize($filename);
11 if ($width && ($width_orig $height_orig))
12 { //高比宽大,高为200,kuan宽按比例缩小
13 $width = ($height $height_orig) * $width_orig;
14 }else {
15 $height = ($width / $width_orig) *  $height_orig;
16 }
17 // 改变大小。和上例一样。
18 $image_p =  imagecreatetruecolor($width, $height);
19 $image =  imagecreatefromjpeg($filename);
20 imagecopyresampled($image_p, $image, 0, 0,  0, 0, $width, $height, $width_orig, $height_orig);
21 // Output
22 imagejpeg($image_p, null,100)
23 ?>

延伸阅读

此文章所在专题列表如下:

  1. PHP函数补完:get_magic_quotes_gpc()
  2. PHP函数补完:error_reporting()
  3. PHP函数补完:preg_match()
  4. PHP函数补完:urlencode()
  5. PHP函数补完:array_multisort()
  6. PHP函数补完:array_splice()
  7. PHP函数补完:isset()
  8. PHP函数补完:getenv()
  9. PHP函数补完:header()
  10. PHP函数补完:mysql_num_rows()
  11. PHP函数补完:list()
  12. PHP函数补完:mysql_query()
  13. PHP函数补完:mysql_fetch_array()
  14. PHP函数补完:number_format()
  15. PHP函数补完:explode()
  16. PHP函数补完:call_user_func()
  17. PHP函数补完:ImageCopyResamples()
  18. PHP函数补完:import_request_variables()
  19. PHP函数补完:parse_url()
  20. PHP函数补完:移除HTML标签strip_tags()
  21. PHP函数补完:输出数组结构与内容var_dump()
  22. PHP函数补完:var_export()
  23. PHP函数补完:判断变量是否为数字is_numeric()
  24. PHP函数补完:session_name()
  25. PHP函数补完:session_id()
  26. PHP函数补完:nl2br()与nl2p()函数
  27. PHP函数补完:shuffle()取数组若干个随机元素
  28. PHP函数补完:http_build_query()构造URL字符串
  29. PHP函数补完:stream_context_create()模拟POST/GET

posted on 2015-12-25 15:43  hahahahahai12  阅读(167)  评论(0)    收藏  举报

导航