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

Jul 25, 2016 am 08:51 AM

  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. 图片批量缩放









复制代码


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles