streams/Code/Lib/Resizer.php

53 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace Code\Lib;
require_once('include/photos.php');
/**
* Performs initial thumbnail creation to a "sane size" using in this case, imagick convert via exec.
* This will get around PHP memory issues but is currently platform dependent.
*/
class Resizer
{
const DEFAULT_MAX_SIZE = 1600;
private string $converter_path;
private array $getimagesize;
public function __construct($converter_path,$getimagesize)
{
if (@file_exists($converter_path)) {
$this->converter_path = $converter_path;
}
if ($getimagesize) {
$this->getimagesize = $getimagesize;
}
}
2022-09-16 10:57:04 +00:00
private function constructDimension($max): bool|string
{
if (!isset($this->getimagesize)) {
return false;
}
2022-09-12 08:55:30 +00:00
if ($this->getimagesize[0] > $max || $this->getimagesize[1] > $max) {
return photo_calculate_scale(array_merge($this->getimagesize, ['max' => $max]));
}
return false;
}
2022-09-16 10:57:04 +00:00
public function resize($infile,$outfile,$max_size = self::DEFAULT_MAX_SIZE): bool
{
$dim = $this->constructDimension($max_size);
if ($dim && $this->converter_path) {
$cmd = $this->converter_path . ' ' . escapeshellarg($infile) . ' -resize ' . $dim . ' ' . escapeshellarg($outfile);
exec($cmd);
if (@file_exists($outfile)) {
return true;
}
}
return false;
}
}