-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4088e5d
commit 76cd859
Showing
5 changed files
with
232 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Mindee Image handling Exceptions. | ||
*/ | ||
|
||
namespace Mindee\Error; | ||
|
||
/** | ||
* Exceptions relating to the handling of images. | ||
*/ | ||
class MindeeImageException extends MindeeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Mindee\Image; | ||
|
||
use Mindee\Error\MindeeImageException; | ||
|
||
/** | ||
* Image compressor class to handle image compression. | ||
*/ | ||
class ImageCompressor | ||
{ | ||
/** | ||
* @param \Imagick|\SplFileObject $inputImage Input image. | ||
* @param integer|null $quality Quality to apply to the image. | ||
* @param integer|null $maxWidth Maximum width to constrain the image to. | ||
* Defaults to the image's size if unset. | ||
* @param integer|null $maxHeight Maximum Height to constrain the image to. | ||
* Defaults to the image's size if unset. | ||
* @return \CURLFile Curlfile handle for the image. | ||
* @throws MindeeImageException Throws if image processing fails. | ||
*/ | ||
public static function compressImage( | ||
$inputImage, | ||
?int $quality = 85, | ||
?int $maxWidth = null, | ||
?int $maxHeight = null | ||
): \CURLFile { | ||
try { | ||
$image = ImageUtils::toMagickImage($inputImage); | ||
ImageUtils::resizeImage($image, $maxWidth, $maxHeight); | ||
ImageUtils::compressImageQuality($image, $quality); | ||
return ImageUtils::toCURLFile($image); | ||
} catch (\Exception $e) { | ||
throw new MindeeImageException("Image compression failed.\n" . $e->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Mindee\Image; | ||
|
||
use Mindee\Error\MindeeImageException; | ||
|
||
/** | ||
* Miscellaneous image operations. | ||
*/ | ||
class ImageUtils | ||
{ | ||
/** | ||
* @param mixed $image Image handle. | ||
* @return \Imagick|\CURLFile|\SplFileObject|resource A valid Imagick handle, CURLFile, or resource. | ||
* The resulting image is formatted to jpeg. | ||
* @throws MindeeImageException Throws if something goes wrong during image conversion. | ||
*/ | ||
public static function toMagickImage($image): \Imagick | ||
{ | ||
try { | ||
if ($image instanceof \Imagick) { | ||
$imagickHandle = $image; | ||
$imagickHandle->setImageFormat('jpeg'); | ||
} elseif ($image instanceof \SplFileObject) { | ||
$imagickHandle = new \Imagick(); | ||
$imagickHandle->readImage($image->getRealPath()); | ||
} elseif ($image instanceof \CURLFile) { | ||
$imagickHandle = new \Imagick(); | ||
$imagickHandle->readImage($image->getFilename()); | ||
} elseif (is_resource($image)) { | ||
$imagickHandle = new \Imagick(); | ||
$imagickHandle->readImageBlob($image); | ||
} else { | ||
throw new MindeeImageException('Input image must be a SplFileObject, resource or Imagick handle.'); | ||
} | ||
$imagickHandle->setImageFormat('jpeg'); | ||
return $imagickHandle; | ||
} catch (MindeeImageException $e) { | ||
throw $e; | ||
} catch (\Exception $e) { | ||
throw new MindeeImageException("Conversion to MagickImage failed.\n" . $e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Resizes a provided MiniMagick Image with the given width & height, if present. | ||
* | ||
* @param \Imagick $image Imagick image handle. | ||
* @param integer|null $width Width to comply with. | ||
* @param integer|null $height Height to comply with. | ||
* @return void | ||
* @throws \ImagickException Throws if resizing fails. | ||
*/ | ||
public static function resizeImage(\Imagick $image, ?int $width = null, int $height = null) | ||
{ | ||
$width ??= $image->getImageWidth(); | ||
$height ??= $image->getImageHeight(); | ||
$image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1); | ||
} | ||
|
||
|
||
/** | ||
* Compresses the quality of the provided MiniMagick image. | ||
* @param \Imagick $image Imagick image handle. | ||
* @param integer $quality Quality to apply to the image. This operation is independent of a JPG's base quality. | ||
* @return void | ||
* @throws \ImagickException Throws if compression fails. | ||
*/ | ||
public static function compressImageQuality(\Imagick $image, int $quality = 85) | ||
{ | ||
$image->setImageCompressionQuality($quality); | ||
} | ||
|
||
/** | ||
* Converts an Imagick into a valid CURLFile handle. | ||
* @param \Imagick $image Imagick image handle. | ||
* @return \CURLFile | ||
* @throws MindeeImageException Throws if the image can't be converted back into a CURLFile. | ||
*/ | ||
public static function toCURLFile(\Imagick $image): \CURLFile | ||
{ | ||
try { | ||
$tempFile = tempnam(sys_get_temp_dir(), 'convert_image_'); | ||
file_put_contents($tempFile, $image->getImageBlob()); | ||
$filenameWithoutExtension = pathinfo($image->getFilename(), PATHINFO_FILENAME); | ||
return new \CURLFile($tempFile, 'image/jpeg', $filenameWithoutExtension . '.jpg'); | ||
} catch (\Exception $e) { | ||
throw new MindeeImageException("Conversion to CURLFile failed.\n" . $e->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters