Discuz教程网

Php Image Resize图片大小调整的函数代码

[复制链接]
authicon 09927306 发表于 2011-1-18 12:29:32 | 显示全部楼层 |阅读模式

  1. function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
  2. if($dst_width <1 || $dst_height <1) {
  3. echo "params width or height error !";
  4. exit();
  5. }
  6. if(!file_exists($src_file)) {
  7. echo $src_file . " is not exists !";
  8. exit();
  9. }

  10. $type=exif_imagetype($src_file);
  11. $support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);

  12. if(!in_array($type, $support_type,true)) {
  13. echo "this type of image does not support! only support jpg , gif or png";
  14. exit();
  15. }

  16. switch($type) {
  17. case IMAGETYPE_JPEG :
  18. $src_img=imagecreatefromjpeg($src_file);
  19. break;
  20. case IMAGETYPE_PNG :
  21. $src_img=imagecreatefrompng($src_file);
  22. break;
  23. case IMAGETYPE_GIF :
  24. $src_img=imagecreatefromgif($src_file);
  25. break;
  26. default:
  27. echo "Load image error!";
  28. exit();
  29. }
  30. $src_w=imagesx($src_img);
  31. $src_h=imagesy($src_img);
  32. $ratio_w=1.0 * $dst_width/$src_w;
  33. $ratio_h=1.0 * $dst_height/$src_h;
  34. if ($src_w<=$dst_width && $src_h<=$dst_height) {
  35. $x = ($dst_width-$src_w)/2;
  36. $y = ($dst_height-$src_h)/2;
  37. $new_img=imagecreatetruecolor($dst_width,$dst_height);
  38. imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
  39. switch($type) {
  40. case IMAGETYPE_JPEG :
  41. imagejpeg($new_img,$dst_file,100);
  42. break;
  43. case IMAGETYPE_PNG :
  44. imagepng($new_img,$dst_file);
  45. break;
  46. case IMAGETYPE_GIF :
  47. imagegif($new_img,$dst_file);
  48. break;
  49. default:
  50. break;
  51. }
  52. } else {
  53. $dstwh = $dst_width/$dst_height;
  54. $srcwh = $src_w/$src_h;
  55. if ($ratio_w <= $ratio_h) {
  56. $zoom_w = $dst_width;
  57. $zoom_h = $zoom_w*($src_h/$src_w);
  58. } else {
  59. $zoom_h = $dst_height;
  60. $zoom_w = $zoom_h*($src_w/$src_h);
  61. }

  62. $zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
  63. imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
  64. $new_img=imagecreatetruecolor($dst_width,$dst_height);
  65. $x = ($dst_width-$zoom_w)/2;
  66. $y = ($dst_height-$zoom_h)/2+1;
  67. imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
  68. switch($type) {
  69. case IMAGETYPE_JPEG :
  70. imagejpeg($new_img,$dst_file,100);
  71. break;
  72. case IMAGETYPE_PNG :
  73. imagepng($new_img,$dst_file);
  74. break;
  75. case IMAGETYPE_GIF :
  76. imagegif($new_img,$dst_file);
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. }
复制代码






上一篇:php下连接mssql2005的代码
下一篇:php生成随机验证码的几种方法
authicon forever21 发表于 2011-6-18 13:59:46 | 显示全部楼层
有意思~顶顶 ,继续顶顶。继续顶哦
authicon nancybingling 发表于 2011-6-21 14:59:44 | 显示全部楼层
好耶!功能强大啊
authicon 乐娃娃 发表于 2011-6-22 14:59:54 | 显示全部楼层
继续来索要
authicon melody0721 发表于 2011-6-22 21:59:52 | 显示全部楼层
好辛苦才找到啊
authicon kurt226 发表于 2011-6-23 16:00:25 | 显示全部楼层
有意思~顶顶 ,继续顶顶。继续顶哦
authicon №小乖 发表于 2011-6-24 20:59:31 | 显示全部楼层
好东东下下来看看
authicon lilac_yao 发表于 2011-6-26 13:59:45 | 显示全部楼层
这个贴不错!!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 09:57

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表