1
0
Fork 0
mirror of synced 2024-06-29 19:50:26 +12:00
appwrite/src/Appwrite/Resize/Resize.php

220 lines
5.2 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
namespace Appwrite\Resize;
2019-05-09 18:54:39 +12:00
use Exception;
use Imagick;
class Resize
{
private $image;
private $width;
private $height;
/**
* @param string $data
*
2019-05-09 18:54:39 +12:00
* @throws Exception
*/
public function __construct($data)
{
$this->image = new Imagick();
2019-05-09 18:54:39 +12:00
$this->image->readImageBlob($data);
$this->width = $this->image->getImageWidth();
2019-05-09 18:54:39 +12:00
$this->height = $this->image->getImageHeight();
}
/**
* @param int $width
* @param int $height
*
2019-05-09 18:54:39 +12:00
* @return Resize
*
* @throws \Throwable
2019-05-09 18:54:39 +12:00
*/
public function crop(int $width, int $height)
{
$originalAspect = $this->width / $this->height;
if (empty($width)) {
2019-05-09 18:54:39 +12:00
$width = $height * $originalAspect;
}
if (empty($height)) {
2019-05-09 18:54:39 +12:00
$height = $width / $originalAspect;
}
if (empty($height) && empty($width)) {
2019-05-09 18:54:39 +12:00
$height = $this->height;
$width = $this->width;
2019-05-09 18:54:39 +12:00
}
if ($this->image->getImageFormat() == 'GIF') {
$this->image = $this->image->coalesceImages();
foreach ($this->image as $frame) {
$frame->cropThumbnailImage($width, $height);
}
$this->image->deconstructImages();
} else {
2019-05-09 18:54:39 +12:00
$this->image->cropThumbnailImage($width, $height);
}
return $this;
}
/**
* @param $color
*
2019-05-09 18:54:39 +12:00
* @return Resize
*
* @throws \Throwable
2019-05-09 18:54:39 +12:00
*/
public function setBackground($color)
{
2019-05-09 18:54:39 +12:00
$this->image->setImageBackgroundColor($color);
$this->image = $this->image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
return $this;
}
/**
* Output.
2019-05-09 18:54:39 +12:00
*
* Prints manipulated image.
*
* @param string $type
* @param int $quality
*
2019-05-09 18:54:39 +12:00
* @return string
*
2019-05-09 18:54:39 +12:00
* @throws Exception
*/
public function output(string $type, int $quality = 75)
{
return $this->save(null, $type, $quality);
}
/**
* @param string $path
* @param $type
* @param int $quality
*
2019-05-09 18:54:39 +12:00
* @return string
*
2019-05-09 18:54:39 +12:00
* @throws Exception
*/
public function save(string $path = null, string $type = '', int $quality = 75)
{
// Create directory with write permissions
if (null !== $path && !\file_exists(\dirname($path))) {
if (!@\mkdir(\dirname($path), 0755, true)) {
throw new Exception('Can\'t create directory '.\dirname($path));
2019-11-13 11:32:59 +13:00
}
2019-05-09 18:54:39 +12:00
}
switch ($type) {
case 'jpg':
case 'jpeg':
$this->image->setImageCompressionQuality($quality);
$this->image->setImageFormat('jpg');
break;
case 'gif':
$this->image->setImageFormat('gif');
break;
case 'webp':
2020-07-08 01:20:04 +12:00
try {
$this->image->setImageFormat('webp');
} catch (\Throwable $th) {
2020-07-08 01:20:04 +12:00
$signature = $this->image->getImageSignature();
$temp = '/tmp/temp-'.$signature.'.'.\strtolower($this->image->getImageFormat());
$output = '/tmp/output-'.$signature.'.webp';
2019-05-09 18:54:39 +12:00
2020-07-08 01:20:04 +12:00
// save temp
$this->image->writeImages($temp, true);
2019-05-09 18:54:39 +12:00
2020-07-08 01:20:04 +12:00
// convert temp
\exec("cwebp -quiet -metadata none -q $quality $temp -o $output");
2019-05-09 18:54:39 +12:00
2020-07-08 01:20:04 +12:00
$data = \file_get_contents($output);
2019-05-09 18:54:39 +12:00
2020-07-08 01:20:04 +12:00
//load webp
if (empty($path)) {
return $data;
} else {
\file_put_contents($path, $data, LOCK_EX);
}
2019-05-09 18:54:39 +12:00
2020-07-08 01:20:04 +12:00
$this->image->clear();
$this->image->destroy();
2019-05-09 18:54:39 +12:00
2020-07-08 01:20:04 +12:00
//delete webp
\unlink($output);
\unlink($temp);
2019-05-09 18:54:39 +12:00
2020-07-08 01:20:04 +12:00
return;
}
2019-05-09 18:54:39 +12:00
break;
case 'png':
/* Scale quality from 0-100 to 0-9 */
$scaleQuality = \round(($quality / 100) * 9);
2019-05-09 18:54:39 +12:00
/* Invert quality setting as 0 is best, not 9 */
$invertScaleQuality = 9 - $scaleQuality;
$this->image->setImageCompressionQuality($invertScaleQuality);
$this->image->setImageFormat('png');
2019-05-09 18:54:39 +12:00
break;
default:
throw new Exception('Invalid output type given');
break;
}
if (empty($path)) {
2019-05-09 18:54:39 +12:00
return $this->image->getImagesBlob();
} else {
2019-05-09 18:54:39 +12:00
$this->image->writeImages($path, true);
}
$this->image->clear();
$this->image->destroy();
}
/**
* @param int $newHeight
*
2019-05-09 18:54:39 +12:00
* @return int
*/
protected function getSizeByFixedHeight(int $newHeight):int
{
$ratio = $this->width / $this->height;
$newWidth = $newHeight * $ratio;
return $newWidth;
}
/**
* @param int $newWidth
*
2019-05-09 18:54:39 +12:00
* @return int
*/
protected function getSizeByFixedWidth(int $newWidth):int
{
$ratio = $this->height / $this->width;
$newHeight = $newWidth * $ratio;
return $newHeight;
}
2020-07-08 01:20:04 +12:00
}