0
1
Fork 0
hedera-web-mindshore/rest/image/resize.php

71 lines
1.5 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
2016-08-25 10:47:09 +00:00
require_once (__DIR__.'/lib.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
**/
2016-08-25 10:47:09 +00:00
class Resize extends Vn\Lib\Method
2016-07-22 20:00:27 +00:00
{
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'
];
2016-07-22 20:00:27 +00:00
function run ()
{
$options = getopt ('', $params);
2017-12-20 11:34:04 +00:00
if (!$this->checkParams ($options, self::PARAMS))
2016-07-22 20:00:27 +00:00
$this->usage ();
2017-12-20 11:34:04 +00:00
$srcDir = $options['srcDir'];
$dstDir = $options['dstDir'];
$maxHeight = $options['maxHeight'];
$maxWidth = $options['maxWidth'];
2016-07-22 20:00:27 +00:00
$rewrite = isset ($options['rewrite']);
$crop = isset ($options['crop']);
$symbolic = isset ($options['symbolic']);
set_time_limit (0);
$count = 0;
$dir = opendir ($srcDir);
if ($dir)
while ($fileName = readdir ($dir))
if (!in_array ($fileName, ['.', '..']))
{
$srcFile = "$srcDir/$fileName";
$dstFile = "$dstDir/". substr ($fileName, 0, -4).'.png';
if (!file_exists ($dstFile) || $rewrite)
try
{
$symbolicSrc = ($symbolic) ? $srcFile : NULL;
$image = Image::create ($srcFile);
Image::resizeSave ($image, $dstFile, $maxHeight, $maxWidth, $crop, $symbolicSrc);
imagedestroy ($image);
$count++;
}
catch (\Exception $e) {}
}
echo "$count files resized.\n";
2016-07-22 20:00:27 +00:00
}
}