1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 09:31:53 +02:00

Add functionality to add, remove and get color Profile data

This commit is contained in:
Oliver Vogel
2023-10-24 17:02:21 +02:00
parent 90a650eb94
commit 49a2e8a81a
6 changed files with 176 additions and 63 deletions

10
src/Colors/Profile.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace Intervention\Image\Colors;
use Intervention\Image\GenericData;
class Profile extends GenericData
{
//
}

View File

@@ -5,10 +5,14 @@ namespace Intervention\Image\Drivers\Imagick;
use Imagick;
use ImagickException;
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
use Intervention\Image\Colors\Profile;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Drivers\Abstract\AbstractImage;
use Intervention\Image\Drivers\Imagick\Modifiers\ColorspaceModifier;
use Intervention\Image\Drivers\Imagick\Modifiers\ProfileModifier;
use Intervention\Image\Drivers\Imagick\Modifiers\ProfileRemovalModifier;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
use Intervention\Image\Interfaces\FrameInterface;
@@ -157,4 +161,29 @@ class Image extends AbstractImage implements ImageInterface, Iterator
{
return $this->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());
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Intervention\Image\Colors\Profile;
use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Traits\CanCheckType;
class ProfileModifier implements ModifierInterface
{
use CanCheckType;
public function __construct(protected Profile $profile)
{
//
}
public function apply(ImageInterface $image): ImageInterface
{
$imagick = $this->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;
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Traits\CanCheckType;
class ProfileRemovalModifier implements ModifierInterface
{
use CanCheckType;
public function apply(ImageInterface $image): ImageInterface
{
$imagick = $this->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;
}
}

View File

@@ -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();
}
}

78
src/GenericData.php Normal file
View File

@@ -0,0 +1,78 @@
<?php
namespace Intervention\Image;
use Intervention\Image\Exceptions\NotWritableException;
class GenericData
{
/**
* Create new instance
*
* @param string $data
*/
public function __construct(protected string $data)
{
//
}
/**
* 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})."
);
}
}
/**
* 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();
}
}