php实例 生活便利小代码,拍照后,批量递归缩放目录图片.
来源:互联网 发布:聚划算往期数据 编辑:程序博客网 时间:2024/05/17 11:59
新入手单反一周了,今天终于找上了机会带上老婆老妈去荔枝公园拍了一天的照,回来准备上传至相册,突然发现,每张图片都有点偏大,找工具也很累,直接上网,东拼西凑了点代码.实现将指定目录的图片,按指定大小范围缩放并输出到指定目录(含递归) ,供自己以后处理相片使用. 不多废话了,附代码.
lib 库代码.
1
2
3
4
5
6
7
header(
'Content-type:text/html; charset=utf-8'
);
require
"lib/imgHelper.php"
;
$imgHelper
=
new
imgHelper(
"dir1"
);
$imgHelper
->setOutputDir(
"dir2"
);
//默认输出在1024 768 下等比缩放,需要自定义时,$imgHelper->setOutputSize(1440,900);
$imgHelper
->execution();
lib 库代码.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* 图片处理助手
*/
class
imgHelper
{
public
$srcFiles
;
//源文件 array
public
$srcDirs
;
//源目录
public
$exportDir
;
//输出目录
public
$exportFiles
;
//输出文件 array
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
);
//todo 简便处理.
$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>"
;
}
}
- php实例 生活便利小代码,拍照后,批量递归缩放目录图片.
- php上传文件,创建递归目录的实例代码
- php图片缩放实现代码
- opencv批量输入图片缩放后保存到文件夹里面
- 图片批量缩放工具
- 图片批量缩放工具
- opencv批量缩放图片
- python3 批量缩放图片
- linux递归降序便利目录层次结构
- 拍照后旋转图片
- php图片裁剪实例代码
- Android 拍照 代码实例
- VBS批量缩放Word图片
- 批量缩放图片的脚本
- python批量按比例缩放图片
- python图片批量处理缩放
- Opencv 图片缩放实例
- 图片相关--相册选择、拍照、缩放、裁减
- 跳投要点-附上精彩的gif跳投图片
- 无法启动SQL Server 2005中的SQL Server(MSSQLSERVER)服务--zt
- 在Eclipse里关联Android源码的
- 批处理经典入门教程!
- 取N个不重复的随机数
- php实例 生活便利小代码,拍照后,批量递归缩放目录图片.
- as3中平铺背景
- SQLite3 使用教学
- 简单闹钟程序
- 进程状态转换图
- 成功的花
- AS3.0清理缓存,彻底清除对象
- 如何使用拼合图播放一个序列帧动画
- Flex和Flash定时器 setTimeout & setInterval 使用要点