php批量缩放图片的代码举例

原创
2016-07-25 08:59:18 665浏览
  1. ;Translate the image format using the original image size

  2. [Translation]
  3. width=0
  4. height=0
  5. ;Stretch the image to the specified size

  6. [Stretch]
  7. width=800
  8. height=600
  9. ;Zoom the image to the specified Width with height auto size

  10. [AutoHeight]
  11. width=740
  12. height=0
  13. ;Zoom the image to the specified Height with width auto size

  14. [AutoWidth]
  15. width=0
  16. height=380
  17. */ ?>
复制代码

2、缩放图片的php代码 变量classes是一个数组,可以选择任意多个ini文件中指定的设置。

  1. /**
  2. * 批量绽放图片
  3. * edit bbs.it-home.org
  4. * at 2013/5/15
  5. */
  6. $oimg = "test.jpg";//Original image name
  7. $classes = array('Translation','AutoHeight','AutoWidth','Stretch');//Give classes for the new creating images' size which are defined in the specified
  8. ini file
  9. $suffix = 'jpg';//The new image's suffix
  10. $inifile = 'image.ini.php';
  11. $size = getimagesize($oimg);
  12. $x = $size[0]/$size[1];
  13. $name = explode('.',$oimg);
  14. if(!file_exists($inifile)) die('Ini file does not exist!');
  15. $cn = parse_ini_file($inifile,true);//Parse the class style image size from ini file
  16. foreach($classes as $class){
  17. foreach($cn as $k=>$v){
  18. if($k==$class){
  19. if($v['width'] && $v['height']){
  20. $thumbWidth = $v['width'];
  21. $thumbHeight = $v['height'];
  22. }elseif($v['width']){
  23. $thumbWidth = $v['width'];
  24. $thumbHeight = round($thumbWidth/$x);
  25. }elseif($v['height']){
  26. $thumbHeight = $v['height'];
  27. $thumbWidth = round($thumbHeight*$x);
  28. }else{
  29. $thumbWidth = $size[0];
  30. $thumbHeight = $size[1];
  31. }
  32. break;
  33. }
  34. }
  35. if(!isset($thumbHeight) && !isset($thumbWidth)) die('Ini file Settings error!');
  36. $nimg = $name[0].'_'.$class.'.'.$suffix;//New image file name
  37. $source = imagecreatefromjpeg($oimg);
  38. $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  39. imagecopyresampled($thumb,$source,0,0,0,0,$thumbWidth,$thumbHeight,$size[0],$size[1]);
  40. if($suffix=='jpg') $method = 'imagejpeg';
  41. else $method='image'.$suffix;
  42. $method($thumb, $nimg);
  43. imagedestroy($thumb);//Release the image source
  44. imagedestroy($source);
  45. }
  46. ?>
复制代码

php有专门处理图片的函数,对于一些要求较高的图片缩放,php也能做到。

>>> 您可能感兴趣的文章: php缩放图片的实例代码 php等比例缩放图片的示例参考 php等比例缩放图片的工具SimpleImage实例学习



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。