[散分]生活便利小代码,拍照后,批量递归缩放目录图片.

发布时间:2024-12-28 18:25

拍摄旅行照片技巧:学会构图,利用光线,以及后期处理提升照片质量。 #生活乐趣# #生活分享# #旅行生活体验# #游记撰写#

[散分]生活便利小代码,拍照后,批量递归缩放目录图片.

新入手单反一周了,今天终于找上了机会带上老婆老妈去荔枝公园拍了一天的照,回来准备上传至相册,突然发现,每张图片都有点偏大,找工具也很累,直接上网,东拼西凑了点代码.实现将指定目录的图片,按指定大小范围缩放并输出到指定目录(含递归) ,供自己以后处理相片使用. 不多废话了,附代码.

header('Content-type:text/html; charset=utf-8');

require "lib/imgHelper.php";

$imgHelper = new imgHelper( "dir1" );

$imgHelper->setOutputDir( "dir2" );

$imgHelper->execution();

lib 库代码.

<?php

* 图片处理助手

*/

class imgHelper

{

public $srcFiles;

public $srcDirs;

public $exportDir;

public $exportFiles;

private $_option = array("maxWidth"=>"1024" , "maxHeight"=>"768");

function __construct($dir = '' , $option = array() )

{

if (!$dir) return;

$this->srcDirs = $dir;

$this->srcFiles = $this->traversal($dir);

$this->setOptions( $option );

}

* 设置输出目录

* @param $dir

*/

public function setOutputDir( $dir )

{

if( !is_dir( $dir )) { mkdir($dir , 0777 , 1);}

$this->exportDir = $dir;

}

public function execution()

{

foreach( $this->srcFiles as $key =>$val ):

$srcImg = $val;

$toFile = str_replace( $this->srcDirs , $this->exportDir , $srcImg);

$maxWidth = $this->_option["maxWidth"];

$maxHeight = $this->_option["maxHeight"];

$this->resize($srcImg , $toFile , $maxWidth , $maxHeight );

endforeach;

}

private function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)

{

$pInfo = pathinfo( $toFile );

$dir = $pInfo["dirname"]; if(!is_dir( $dir) ){ mkdir($dir , 0777 , 1);}

list($width, $height, $type, $attr) = getimagesize($srcImage);

if($width < $maxWidth || $height < $maxHeight) return ;

switch ($type) {

case 1: $img = imagecreatefromgif($srcImage); break;

case 2: $img = imagecreatefromjpeg($srcImage); break;

case 3: $img = imagecreatefrompng($srcImage); break;

}

$scale = min($maxWidth/$width, $maxHeight/$height);

if($scale < 1) {

$newWidth = floor($scale*$width);

$newHeight = floor($scale*$height);

$newImg = imagecreatetruecolor($newWidth, $newHeight);

imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

$newName = "";

$toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);

switch($type) {

case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))

return "$newName.gif"; break;

case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))

return "$newName.jpg"; break;

case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))

return "$newName.png"; break;

default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))

return "$newName.jpg"; break;

}

imagedestroy($newImg);

}

imagedestroy($img);

return false;

}

* 设置输出的大小

* @param string $width

* @param string $height

*/

public function setOutputSize( $width = "1024" , $height = "768"){

$_option = array("maxWidth"=>"$width" , "maxHeight"=>"$height");

$this->setOptions( $_option );

}

* 设置可选参数

* @param $option

*/

private function setOptions( $option)

{

foreach( $option as $key =>$val):

if( isset( $option[$key]) && $option[$key] ){

$this->_option[$key] = $val;

}

endforeach;

}

* 遍得到文件夹下的所有文件

*/

private function traversal($path)

{

if (!$path) return array();

$files = array();

if (!is_dir($path)) return;

foreach (scandir($path) as $file)

{

if ($file != '.' && $file != '..') {

$path2 = $path . '/' . $file;

if (is_dir($path2)) {

$temp = $this->traversal($path2);

$files = array_merge($files, $temp);

} else {

if ($this->isIMg($file)) {

$files[] = $path . "/" . $file;

}

}

}

}

return $files;

}

* 判断是否是图片

* @param $file

* @return bool

*/

private function isIMg($file) {

$pInfo = pathinfo( $file);

$extention = $pInfo["extension"];

return preg_match("/(jpg)|(png)|gif/i" , $extention);

}

public function debug() {$this->pr($this->srcFiles, "待处理图片数组.");

$this->pr( $this->srcDirs , "源目录");

$this->pr( $this->exportDir , "目标目录");

}

private function pr($array, $title = 'DEBUG', $type = 'array', $width = '') {

$title .= date("Y-m-d H:i:s");

$widthStr = "";

if ($width) $widthStr = "width:$width" . "px";

echo "<fieldset style=\"-moz-border-radius:5px 5px 5px 5px; -moz-box-shadow:0px 0px 10px rgba(00,00,00,0.45); border: 3px solid transparent; padding:3px; margin-top:20px; \"><legend style=\"color: #069; margin:3px; $widthStr \">$title</legend>";

echo "<div style = '-moz-border-radius:10px 10px 10px 10px;font-size:14px; color:#069; border:1px solid #F0FAF9; font-size:9pt; background:#F0FAF9; padding:5px;'>";

print("<pre>");

if ($type == 'json') { $array = json_decode($array); }

print_r($array);

print("</pre>");

echo "<div>";

echo "</fieldset>";

}

}

网址:[散分]生活便利小代码,拍照后,批量递归缩放目录图片. https://www.yuejiaxmz.com/news/view/594573

相关内容

[散分]生活便利小代码,拍照后,批量递归缩放目录图片.
一款Python实用神器,5 行 Python 代码 实现一键批量扣图
拍照高手 数码照相机使用小技巧
数码产品照片数字相机拍摄技巧
热爱拍照记录生活的日常文案(精选40句)
将模糊照片变成艺术品!Aiarty Image Enhancer,您的AI照片修复神器!,✨将模糊照片变成艺术品!AiartyImageEnhancer,您的AI照片修复神器!在我们日常拍摄的过程中,模糊、噪点、不清晰的照片总是令人遗憾。无论是旧照片的修复,还是提升现代作品的质量,有了AiartyImageEnhancerv2.6.0,这些问题都将迎刃而解!这是一款基于先进AI技术的图...
翻拍or扫描?老照片在数字时代如何重生
时间找对了,随便一拍就好看,日常生活照片你就这么拍
生活小技巧我是如何拍出数码产品照片的秘籍
解锁手机摄影密码,拍出美好生活

随便看看