mirror of
https://github.com/Intervention/image.git
synced 2025-09-01 18:02:45 +02:00
Add functionality to add, remove and get color Profile data
This commit is contained in:
10
src/Colors/Profile.php
Normal file
10
src/Colors/Profile.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors;
|
||||||
|
|
||||||
|
use Intervention\Image\GenericData;
|
||||||
|
|
||||||
|
class Profile extends GenericData
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
@@ -5,10 +5,14 @@ namespace Intervention\Image\Drivers\Imagick;
|
|||||||
use Imagick;
|
use Imagick;
|
||||||
use ImagickException;
|
use ImagickException;
|
||||||
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
|
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
|
||||||
|
use Intervention\Image\Colors\Profile;
|
||||||
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
|
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
|
||||||
use Intervention\Image\Drivers\Abstract\AbstractImage;
|
use Intervention\Image\Drivers\Abstract\AbstractImage;
|
||||||
use Intervention\Image\Drivers\Imagick\Modifiers\ColorspaceModifier;
|
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\Drivers\Imagick\Traits\CanHandleColors;
|
||||||
|
use Intervention\Image\Exceptions\ColorException;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
use Intervention\Image\Interfaces\FrameInterface;
|
use Intervention\Image\Interfaces\FrameInterface;
|
||||||
@@ -157,4 +161,29 @@ class Image extends AbstractImage implements ImageInterface, Iterator
|
|||||||
{
|
{
|
||||||
return $this->modify(new ColorspaceModifier($colorspace));
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
32
src/Drivers/Imagick/Modifiers/ProfileModifier.php
Normal file
32
src/Drivers/Imagick/Modifiers/ProfileModifier.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
26
src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php
Normal file
26
src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
namespace Intervention\Image;
|
namespace Intervention\Image;
|
||||||
|
|
||||||
use Intervention\Image\Exceptions\NotWritableException;
|
class EncodedImage extends GenericData
|
||||||
|
|
||||||
class EncodedImage
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Create new instance
|
* Create new instance
|
||||||
@@ -29,22 +27,6 @@ class EncodedImage
|
|||||||
return $this->mimetype;
|
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
|
* 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));
|
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
78
src/GenericData.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user