hedera-web/rest/image/resize.php

68 lines
1.6 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
require_once(__DIR__.'/image.php');
2016-07-22 20:00:27 +00:00
/**
* Resizes all images allocated in a directory.
*
* @param string $srcDir The source directory
* @param string $dstDir The destination directory
* @param integer $maxHeight The maximum height of resized image in pixels
* @param integer $maxWidth The maximum width of resized image in pixels
* @param boolean $rewrite Wether to rewrite the destination file if it exits
*/
2018-05-23 10:14:20 +00:00
class Resize extends Vn\Lib\Method {
2016-09-06 14:25:02 +00:00
const PARAMS = [
2017-12-20 11:34:04 +00:00
'srcDir'
,'dstDir'
,'maxHeight'
,'maxWidth'
2016-09-06 14:25:02 +00:00
,'rewrite'
,'crop'
,'symbolic'
];
function run($db) {
$options = getopt('', self::PARAMS);
2016-07-22 20:00:27 +00:00
2017-12-20 11:34:04 +00:00
$srcDir = $options['srcDir'];
$dstDir = $options['dstDir'];
$maxHeight = $options['maxHeight'];
$maxWidth = $options['maxWidth'];
2018-05-23 10:14:20 +00:00
$rewrite = isset($options['rewrite']);
$crop = isset($options['crop']);
$symbolic = isset($options['symbolic']);
$verbose = isset($options['verbose']);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
set_time_limit(0);
2016-07-22 20:00:27 +00:00
$count = 0;
2018-05-23 10:14:20 +00:00
$dir = opendir($srcDir);
2016-07-22 20:00:27 +00:00
if ($dir)
2018-05-23 10:14:20 +00:00
while ($fileName = readdir($dir))
if (!in_array($fileName, ['.', '..'])) {
2016-07-22 20:00:27 +00:00
$srcFile = "$srcDir/$fileName";
2018-05-23 10:14:20 +00:00
$dstFile = "$dstDir/". substr($fileName, 0, -4).'.png';
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (!file_exists($dstFile) || $rewrite)
try {
$symbolicSrc = $symbolic ? $srcFile : NULL;
if ($verbose) {
echo "SRC: $srcFile\n";
echo "DST: $dstFile\n";
}
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$image = Image::create($srcFile);
Image::resizeSave($image, $dstFile, $maxHeight, $maxWidth, $crop, $symbolicSrc);
imagedestroy($image);
2016-07-22 20:00:27 +00:00
$count++;
2018-05-23 11:09:55 +00:00
} catch (\Exception $e) {}
2016-07-22 20:00:27 +00:00
}
echo "$count files resized.\n";
2016-07-22 20:00:27 +00:00
}
}