From 49a2e8a81a60545b1525058f414ab4965438749e Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 24 Oct 2023 17:02:21 +0200 Subject: [PATCH] Add functionality to add, remove and get color Profile data --- src/Colors/Profile.php | 10 +++ src/Drivers/Imagick/Image.php | 29 +++++++ .../Imagick/Modifiers/ProfileModifier.php | 32 ++++++++ .../Modifiers/ProfileRemovalModifier.php | 26 +++++++ src/EncodedImage.php | 64 +-------------- src/GenericData.php | 78 +++++++++++++++++++ 6 files changed, 176 insertions(+), 63 deletions(-) create mode 100644 src/Colors/Profile.php create mode 100644 src/Drivers/Imagick/Modifiers/ProfileModifier.php create mode 100644 src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php create mode 100644 src/GenericData.php diff --git a/src/Colors/Profile.php b/src/Colors/Profile.php new file mode 100644 index 00000000..f0a847fd --- /dev/null +++ b/src/Colors/Profile.php @@ -0,0 +1,10 @@ +modify(new ColorspaceModifier($colorspace)); } + + public function setProfile(string $filepath): ImageInterface + { + return $this->modify( + new ProfileModifier( + new Profile(file_get_contents($filepath)) + ) + ); + } + + public function getProfile(): Profile + { + $profiles = $this->imagick->getImageProfiles('icc'); + + if (!array_key_exists('icc', $profiles)) { + throw new ColorException('No ICC profile found.'); + } + + return new Profile($profiles['icc']); + } + + public function withoutProfile(): ImageInterface + { + return $this->modify(new ProfileRemovalModifier()); + } } diff --git a/src/Drivers/Imagick/Modifiers/ProfileModifier.php b/src/Drivers/Imagick/Modifiers/ProfileModifier.php new file mode 100644 index 00000000..bf0ae013 --- /dev/null +++ b/src/Drivers/Imagick/Modifiers/ProfileModifier.php @@ -0,0 +1,32 @@ +failIfNotClass($image, Image::class)->getImagick(); + $result = $imagick->profileImage('icc', (string) $this->profile); + + if ($result === false) { + throw new ColorException('ICC color profile could not be set.'); + } + + return $image; + } +} diff --git a/src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php b/src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php new file mode 100644 index 00000000..7a4583f2 --- /dev/null +++ b/src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php @@ -0,0 +1,26 @@ +failIfNotClass($image, Image::class)->getImagick(); + $result = $imagick->profileImage('icc', null); + + if ($result === false) { + throw new ColorException('ICC color profile could not be removed.'); + } + + return $image; + } +} diff --git a/src/EncodedImage.php b/src/EncodedImage.php index 739e53b5..330fc590 100644 --- a/src/EncodedImage.php +++ b/src/EncodedImage.php @@ -2,9 +2,7 @@ namespace Intervention\Image; -use Intervention\Image\Exceptions\NotWritableException; - -class EncodedImage +class EncodedImage extends GenericData { /** * Create new instance @@ -29,22 +27,6 @@ class EncodedImage return $this->mimetype; } - /** - * Save encoded image data in file system - * - * @param string $filepath - * @return void - */ - public function save(string $filepath): void - { - $saved = @file_put_contents($filepath, (string) $this); - if ($saved === false) { - throw new NotWritableException( - "Can't write image data to path ({$filepath})." - ); - } - } - /** * Transform encoded image data into an data uri string * @@ -54,48 +36,4 @@ class EncodedImage { return sprintf('data:%s;base64,%s', $this->mimetype, base64_encode($this->data)); } - - /** - * Cast encoded image object to string - * - * @return string - */ - public function toString(): string - { - return $this->data; - } - - /** - * Create file pointer from encoded image - * - * @return resource - */ - public function toFilePointer() - { - $pointer = fopen('php://temp', 'rw'); - fputs($pointer, $this->toString()); - rewind($pointer); - - return $pointer; - } - - /** - * Return byte size of encoded image - * - * @return int - */ - public function size(): int - { - return mb_strlen($this->data); - } - - /** - * Cast encoded image object to string - * - * @return string - */ - public function __toString(): string - { - return $this->toString(); - } } diff --git a/src/GenericData.php b/src/GenericData.php new file mode 100644 index 00000000..e16e3d28 --- /dev/null +++ b/src/GenericData.php @@ -0,0 +1,78 @@ +data; + } + + /** + * Create file pointer from encoded image + * + * @return resource + */ + public function toFilePointer() + { + $pointer = fopen('php://temp', 'rw'); + fputs($pointer, $this->toString()); + rewind($pointer); + + return $pointer; + } + + /** + * Return byte size of encoded image + * + * @return int + */ + public function size(): int + { + return mb_strlen($this->data); + } + + /** + * Cast encoded image object to string + * + * @return string + */ + public function __toString(): string + { + return $this->toString(); + } +}