-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_photos.php
More file actions
69 lines (51 loc) · 1.72 KB
/
resize_photos.php
File metadata and controls
69 lines (51 loc) · 1.72 KB
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
<?php
date_default_timezone_set('America/Vancouver');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', getcwd() . DS);
define('SOURCE', ROOT . 'src' . DS);
define('PHOTOS', ROOT . 'src' . DS . 'photography' . DS);
if ($argc >= 2) {
array_shift($argv);
$dir = $argv[0];
} else {
exit(1);
}
// image functions
function max_sizes($width, $height) {
$maxwidth = 1008;
$maxheight = 1008;
$ratio = $width / $height;
if ($maxwidth / $maxheight > $ratio) {
$maxwidth = $maxheight * $ratio;
} else {
$maxheight = $maxwidth / $ratio;
}
return array($maxwidth, $maxheight);
}
function resize($imagepath) {
$finfo = pathinfo($imagepath);
$newpath = $finfo['dirname'] . DS . $finfo['filename'] . '.' . $finfo['extension'];
set_time_limit(0);
$image = imagecreatefromjpeg($imagepath);
$width = imagesx($image);
$height = imagesy($image);
list($maxwidth, $maxheight) = max_sizes($width, $height);
$sharpen = array([0, -1, 0], [-1, 10, -1], [0, -1, 0]);
$divisor = array_sum(array_map('array_sum', $sharpen));
$offset = 0;
imageconvolution($image, $sharpen, $divisor, $offset);
$resized = imagecreatetruecolor($maxwidth, $maxheight);
imagecopyresampled($resized, $image, 0, 0, 0, 0, $maxwidth, $maxheight, $width, $height);
imagejpeg($resized, $newpath, 84);
}
// iterate photos to resize in `src/`
$staticIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(SOURCE . $dir));
while ($staticIterator->valid()) {
if (!$staticIterator->isDot() && substr($staticIterator->getBasename(), '0', 1) !== '.') {
$ext = $staticIterator->getExtension();
if ($ext === 'jpeg' || $ext === 'jpg') {
resize($staticIterator->getPathname());
}
}
$staticIterator->next();
}