Rumah >pembangunan bahagian belakang >tutorial php >php缩略图代码,imagecopyresampled函数生成缩略图

php缩略图代码,imagecopyresampled函数生成缩略图

WBOY
WBOYasal
2016-07-25 08:51:29833semak imbas
  1. function CreateImage($SrcImageUrl, $DirImageUrl, $Width, $Height)

  2. {
  3. $img;
  4. $srcw;
  5. $new_width;
  6. $srch;
  7. $new_height;
  8. // 图片类型
  9. $type = substr(strrchr($SrcImageUrl, "."), 1);
  10. // 初始化图像
  11. if($type == "jpg")
  12. $img = imagecreatefromjpeg($SrcImageUrl);
  13. if($type == "gif")
  14. $img = imagecreatefromgif($SrcImageUrl);
  15. if($type == "png")
  16. $img = imagecreatefrompng($SrcImageUrl);
  17. $srcw = imagesx($img);

  18. $srch = imagesy($img);
  19. if ($srcw / $srch > $Width / $Height)

  20. {
  21. if ($srcw > $Width)
  22. {
  23. $new_width = $Width;
  24. $new_height = $srch * ($Width / $srcw);
  25. }
  26. else
  27. {
  28. $new_width = $srcw;
  29. $new_height = $srch;
  30. }
  31. }
  32. else
  33. {
  34. if ($srch > $Height)
  35. {
  36. $new_height = $Height;
  37. $new_width = $srcw * ($Height / $srch);
  38. }
  39. else
  40. {
  41. $new_width = $srcw;
  42. $new_height = $srch;
  43. }
  44. }
  45. $new_image = imagecreatetruecolor($new_width, $new_height);

  46. imagecopyresampled($new_image, $img, 0, 0, 0, 0, $new_width, $new_height, $srcw, $srch);
  47. imagejpeg($new_image, $DirImageUrl);
  48. imagedestroy($img);

  49. imagedestroy($new_image);
  50. }
复制代码

3、改进后代码:

  1. /*

  2. *$o_photo 原图路径

  3. *$d_photo 处理后图片路径

  4. *$width 定义宽

  5. *$height 定义高

  6. *调用方法 cutphoto("test.jpg","temp.jpg",256,146);

  7. */

  8. function cutphoto($o_photo,$d_photo,$width,$height){

  9. $temp_img = imagecreatefromjpeg($o_photo);

  10. $o_width = imagesx($temp_img); //取得原图宽

  11. $o_height = imagesy($temp_img); //取得原图高

  12. //判断处理方法

  13. if($width>$o_width || $height>$o_height){//原图宽或高比规定的尺寸小,进行压缩

  14. $newwidth=$o_width;

  15. $newheight=$o_height;

  16. if($o_width>$width){

  17. $newwidth=$width;

  18. $newheight=$o_height*$width/$o_width;

  19. }

  20. if($newheight>$height){

  21. $newwidth=$newwidth*$height/$newheight;

  22. $newheight=$height;

  23. }

  24. //缩略图片

  25. $new_img = imagecreatetruecolor($newwidth, $newheight);

  26. imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);

  27. imagejpeg($new_img , $d_photo);

  28. imagedestroy($new_img);

  29. }else{//原图宽与高都比规定尺寸大,进行压缩后裁剪

  30. if($o_height*$width/$o_width>$height){//先确定width与规定相同,如果height比规定大,则ok

  31. $newwidth=$width;

  32. $newheight=$o_height*$width/$o_width;

  33. $x=0;

  34. $y=($newheight-$height)/2;

  35. }else{ //否则确定height与规定相同,width自适应

  36. $newwidth=$o_width*$height/$o_height;

  37. $newheight=$height;

  38. $x=($newwidth-$width)/2;

  39. $y=0;

  40. }

  41. //缩略图片

  42. $new_img = imagecreatetruecolor($newwidth, $newheight);

  43. imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);

  44. imagejpeg($new_img , $d_photo);

  45. imagedestroy($new_img);

  46. $temp_img = imagecreatefromjpeg($d_photo);

  47. $o_width = imagesx($temp_img); //取得缩略图宽

  48. $o_height = imagesy($temp_img); //取得缩略图高

  49. //裁剪图片

  50. $new_imgx = imagecreatetruecolor($width,$height);

  51. imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);

  52. imagejpeg($new_imgx , $d_photo);

  53. imagedestroy($new_imgx);

  54. }
  55. }
  56. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn