Home > Backend Development > PHP Tutorial > Scan all pictures in a file directory, scale them and place them in another directory

Scan all pictures in a file directory, scale them and place them in another directory

WBOY
Release: 2016-07-25 08:51:05
Original
1082 people have browsed it
  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. * Read all files in a folder
  12. * @param $src folder path
  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. * Get some image information
  49. * getimagesize returns an array of four units ($width, $height, $type, $attr)
  50. * @param $src
  51. * @return array
  52. */
  53. public function getImgInfo($src){
  54. return getimagesize($src);
  55. }
  56. /**
  57. * Get file suffix
  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 < $height_ratio){
  81. $scale_org[0] = $width * $width_ratio;
  82. $scale_org[1] = $height * $width_ratio;
  83. }
  84. //高度优先
  85. else{
  86. $scale_org[0] = $width * $height_ratio;
  87. $scale_org[1] = $height * $height_ratio;
  88. }
  89. }
  90. elseif($resize_width){
  91. $scale_org[0] = $dst_width;
  92. $scale_org[1] = $dst_width*$height/$width;
  93. }
  94. elseif($resize_height){
  95. $scale_org[0] = $dst_height*$width/$height;
  96. $scale_org[1] = $dst_height;
  97. }
  98. if(function_exists("imagecopyresampled")){
  99. $newim = imagecreatetruecolor($scale_org[0], $scale_org[1]);
  100. imagecopyresampled($newim, $image, 0, 0, 0, 0, $scale_org[0], $scale_org[1], $width, $height);
  101. }else{
  102. $newim = imagecreate($scale_org[0], $scale_org[1]);
  103. imagecopyresized($newim, $image, 0, 0, 0, 0,$scale_org[0], $scale_org[1], $width, $height);
  104. }
  105. ImageJpeg ($newim,$dstpath);
  106. ImageDestroy ($newim);
  107. }
  108. }
  109. $a=new tool_slt_resize(); //接受表单数据开始处理图片
  110. $oldPath=isset($_GET['oldPath'])?$_GET['oldPath']:'';
  111. $newPath=isset($_GET['newPath'])?$_GET['newPath']:'';
  112. $width=isset($_GET['width'])?$_GET['width']:'';
  113. $height=isset($_GET['height'])?$_GET['height']:'';
  114. $a->width=$width;
  115. $a->height=$height;
  116. if(isset($_GET['submit'])){
  117. if($oldPath==''||$newPath==''||$width==''){
  118. echo '请正确填写表单';
  119. exit;
  120. }else{
  121. $a->getAllFile($oldPath,$newPath);
  122. }
  123. }
  124. ?>
  125. 图片批量缩放









复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template