Skip to content

Commit

Permalink
Add functionality to add, remove and get color Profile data
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 24, 2023
1 parent 90a650e commit 49a2e8a
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 63 deletions.
10 changes: 10 additions & 0 deletions src/Colors/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Intervention\Image\Colors;

use Intervention\Image\GenericData;

class Profile extends GenericData
{
//
}
29 changes: 29 additions & 0 deletions src/Drivers/Imagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
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;
Expand Down Expand Up @@ -157,4 +161,29 @@ public function setColorspace(string|ColorspaceInterface $colorspace): ImageInte
{
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 changes: 32 additions & 0 deletions src/Drivers/Imagick/Modifiers/ProfileModifier.php
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions src/Drivers/Imagick/Modifiers/ProfileRemovalModifier.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
64 changes: 1 addition & 63 deletions src/EncodedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Intervention\Image;

use Intervention\Image\Exceptions\NotWritableException;

class EncodedImage
class EncodedImage extends GenericData
{
/**
* Create new instance
Expand All @@ -29,22 +27,6 @@ public function mimetype(): string
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
*
Expand All @@ -54,48 +36,4 @@ public function toDataUri(): string
{
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 changes: 78 additions & 0 deletions src/GenericData.php
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 49a2e8a

Please sign in to comment.