ホームページ  >  記事  >  バックエンド開発  >  ファイル ディレクトリ内のすべての写真をスキャンし、拡大縮小して別のディレクトリに配置します

ファイル ディレクトリ内のすべての写真をスキャンし、拡大縮小して別のディレクトリに配置します

WBOY
WBOYオリジナル
2016-07-25 08:51:051018ブラウズ
  1. 图片批判量放
  2. < style type="text/css">
  3. label{width:150px;display: inline-block;}
  4. class tool_slt_resize{
  5. public $width =null;
  6. public $height=null;
  7. public $Msg=null;
  8. public $extension=array('jpg','gif','jpeg','png','bmp');
  9. public function __construct( ){
  10. header('Content-type: text/html; charset=UTF-8');
  11. }
  12. /**
  13. * フォルダー内のすべてのファイルを読み取ります
  14. * @param $src フォルダーのパス
  15. * @return bool
  16. */
  17. public function getAllFile($src,$new){
  18. set_time_limit(0) ); //php スクリプト実行時間無制限
  19. ob_end_clean(); //关键1
  20. echo str_pad('',1024); //关键2
  21. $handle=opendir($src); //打开一句个目录柄
  22. while(($file=readdir($handle)) !== false){ //巡环遍历目录下のすべての文件
  23. if($file != '.' && $file != ' ..' ){ //如果不是当前目录または上層目录
  24. $fullPath = $src.'/'.$file; //現在の文書の全路径を取得
  25. $newPath = $new.'/'.$file; //新しい文書の存放路径
  26. $dir=dirname($newPath);
  27. if(!file_exists($dir)){
  28. mkdir($dir);
  29. }
  30. if(is_dir($fullPath)){ // 判断1 つのファイルを表示するかどうか $this->getAllFile($fullPath,$newPath); // 文件夹の话再递归実行一次関数
  31. }else{ //画像処理の開始 $extentsion=$this->get_extension($fullPath);
  32. if(in_array($extension,$this->extension) ){ //後缀か合法
  33. // echo $newPath.'
    ';
  34. $im = imagecreatefromjpeg($fullPath);
  35. $width = imagex($im);
  36. $height = imagey($im );
  37. $this->resize_to($im,$width,$height,$this->width,$this->height,$fullPath,$newPath);
  38. $msg= $fullPath.'処理が完了しました
    ';
  39. echo $msg;
  40. flash(); //刷新输出缓冲
  41. }
  42. }
  43. }
  44. }
  45. echo '全部处処理完了';
  46. }
  47. /**
  48. * いくつかの画像情報を取得します
  49. * getimagesize は 4 つの単位 ($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. * @returnmixed
  60. */
  61. function get_extension($file){
  62. $info = pathinfo($file);
  63. return strto lower($info['extension']);
  64. }
  65. //画像缩放
  66. public function raise_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. imagecopyresize( $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=新しい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['高さ']:'';
  114. $a->幅=$幅;
  115. $a->高さ=$高さ;
  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. 图片批量缩放





  126. < input type="text" name="width" value="">



复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:サエ画像生成次の記事:サエ画像生成