Heim  >  Artikel  >  Backend-Entwicklung  >  扫描一个文件目录下所有图片,缩放后放在另一个目录

扫描一个文件目录下所有图片,缩放后放在另一个目录

WBOY
WBOYOriginal
2016-07-25 08:51:051018Durchsuche
  1. 图片批量缩放
  2. class tool_slt_resize{
  3. public $width=null;
  4. public $height=null;
  5. public $Msg=null;
  6. public $extension=array('jpg','gif','jpeg','png','bmp');
  7. public function __construct(){
  8. header('Content-type: text/html; charset=UTF-8');
  9. }
  10. /**
  11. * 读取一个文件夹下所有的文件
  12. * @param $src 文件夹路径
  13. * @return bool
  14. */
  15. public function getAllFile($src,$new){
  16. set_time_limit(0); //php脚本运行时间无限时
  17. ob_end_clean(); //关键1
  18. echo str_pad('',1024); //关键2
  19. $handle=opendir($src); //打开一个目录句柄
  20. while(($file=readdir($handle)) !== false){ //循环遍历目录下的所有文件
  21. if($file != '.' && $file != '..' ){ //如果不是当前目录或者上层目录
  22. $fullPath = $src.'/'.$file; //得到当前文件的全路径
  23. $newPath = $new.'/'.$file; //新的文件存放路径
  24. $dir=dirname($newPath);
  25. if(!file_exists($dir)){
  26. mkdir($dir);
  27. }
  28. if(is_dir($fullPath)){ //判断是否表示一个文件夹
  29. $this->getAllFile($fullPath,$newPath); //是文件夹的话再递归执行一次函数
  30. }else{ //开始图像处理
  31. $extentsion=$this->get_extension($fullPath);
  32. if(in_array($extentsion,$this->extension)){ //后缀是否合法
  33. // echo $newPath.'';
  34. $im = imagecreatefromjpeg($fullPath);
  35. $width = imagesx($im);
  36. $height = imagesy($im);
  37. $this->resize_to($im,$width,$height,$this->width,$this->height,$fullPath,$newPath);
  38. $msg= $fullPath.'处理完成';
  39. echo $msg;
  40. flush(); //刷新输出缓冲
  41. }
  42. }
  43. }
  44. }
  45. echo '全部处理完成';
  46. }
  47. /**
  48. * 获得一些图像的信息
  49. * getimagesize返回一个四个单元的数组($width, $height, $type, $attr)
  50. * @param $src
  51. * @return array
  52. */
  53. public function getImgInfo($src){
  54. return getimagesize($src);
  55. }
  56. /**
  57. * 获得文件后缀
  58. * @param $file
  59. * @return mixed
  60. */
  61. function get_extension($file){
  62. $info = pathinfo($file);
  63. return strtolower($info['extension']);
  64. }
  65. //图像缩放
  66. public function resize_to($image,$width,$height,$dst_width,$dst_height,$path,$dstpath){
  67. // set_time_limit(0);
  68. $resize_width = 0;
  69. $resize_height = 0;
  70. if($dst_width && $width > $dst_width ){
  71. $resize_width = 1;
  72. $width_ratio = $dst_width/$width;
  73. }
  74. if($dst_height && $height > $dst_height){
  75. $resize_height = 1;
  76. $height_ratio = $dst_height/$height;
  77. }
  78. if($resize_height&&$resize_width){
  79. //宽度优先
  80. if($width_ratio $scale_org[0] = $width * $width_ratio;
  81. $scale_org[1] = $height * $width_ratio;
  82. }
  83. //高度优先
  84. else{
  85. $scale_org[0] = $width * $height_ratio;
  86. $scale_org[1] = $height * $height_ratio;
  87. }
  88. }
  89. elseif($resize_width){
  90. $scale_org[0] = $dst_width;
  91. $scale_org[1] = $dst_width*$height/$width;
  92. }
  93. elseif($resize_height){
  94. $scale_org[0] = $dst_height*$width/$height;
  95. $scale_org[1] = $dst_height;
  96. }
  97. if(function_exists("imagecopyresampled")){
  98. $newim = imagecreatetruecolor($scale_org[0], $scale_org[1]);
  99. imagecopyresampled($newim, $image, 0, 0, 0, 0, $scale_org[0], $scale_org[1], $width, $height);
  100. }else{
  101. $newim = imagecreate($scale_org[0], $scale_org[1]);
  102. imagecopyresized($newim, $image, 0, 0, 0, 0,$scale_org[0], $scale_org[1], $width, $height);
  103. }
  104. ImageJpeg ($newim,$dstpath);
  105. ImageDestroy ($newim);
  106. }
  107. }
  108. $a=new tool_slt_resize(); //接受表单数据开始处理图片
  109. $oldPath=isset($_GET['oldPath'])?$_GET['oldPath']:'';
  110. $newPath=isset($_GET['newPath'])?$_GET['newPath']:'';
  111. $width=isset($_GET['width'])?$_GET['width']:'';
  112. $height=isset($_GET['height'])?$_GET['height']:'';
  113. $a->width=$width;
  114. $a->height=$height;
  115. if(isset($_GET['submit'])){
  116. if($oldPath==''||$newPath==''||$width==''){
  117. echo '请正确填写表单';
  118. exit;
  119. }else{
  120. $a->getAllFile($oldPath,$newPath);
  121. }
  122. }
  123. ?>
  124. 图片批量缩放

复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:sae 图种生成 Nächster Artikel:验证码破解和反破解的思路