Skip to content

Commit

Permalink
Merge branch 'master' into gd-text-integration
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/DriverTest.php
  • Loading branch information
sergix44 committed Dec 21, 2023
2 parents eeb2a11 + 9f1127f commit 44c3a29
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 16.x
cache: yarn
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ jobs:
args: --config=.php-cs-fixer.dist.php --allow-risky=yes

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: fix code style
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- name: Upload images in case of failure
if: steps.tests.outcome == 'failure'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: failures
path: tests/Tmp/
Expand Down
31 changes: 0 additions & 31 deletions .github/workflows/update-changelog.yml

This file was deleted.

4 changes: 0 additions & 4 deletions CHANGELOG.md

This file was deleted.

29 changes: 29 additions & 0 deletions docs/docs/alterations/base64.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
sidebar_position: 1
_modified_: true
---
# `base64()`

```php
->base64([SergiX44\ImageZen\Format $format = SergiX44\ImageZen\Format::PNG], [int $quality = 90]): string
```
Return the image as base64 encoded string.

## Parameters

- `SergiX44\ImageZen\Format $format`: The image format, default is PNG.
- `int $quality`: The image quality, default is 90, if supported by the format.


## Returns

`string`: The image as base64 string.

## Example

```php
use SergiX44\ImageZen\Image;

$base64 = Image::make('path/to/image.jpg')->base64(\SergiX44\ImageZen\Format::JPG); // data:image/jpg;base64,...

```
2 changes: 1 addition & 1 deletion docs/docs/alterations/greyscale.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 26
sidebar_position: 27
_modified_: false
---
# `greyscale()`
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/alterations/invert.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 30
sidebar_position: 31
_modified_: false
---
# `invert()`
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/alterations/save.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ $success = Image::make('path/to/image.jpg')
->save('path/to/image.png', \SergiX44\ImageZen\Format::PNG); // save the image as PNG

$success = Image::make('path/to/image.jpg')
->save('path/to/image.png', \SergiX44\ImageZen\Format::\SergiX44\ImageZen\JPG, 50); // save the image as JPG with a quality of 50
->save('path/to/image.png', \SergiX44\ImageZen\Format::JPG, 50); // save the image as JPG with a quality of 50

```
1 change: 1 addition & 0 deletions generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

$availableMethods = [
$imageReflection->getMethod('make'),
$imageReflection->getMethod('base64'),
$imageReflection->getMethod('canvas'),
$imageReflection->getMethod('register'),
$imageReflection->getMethod('width'),
Expand Down
20 changes: 19 additions & 1 deletion src/Alterations/PolygonShape.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,34 @@
use SergiX44\ImageZen\Drivers\Imagick\Imagick;
use SergiX44\ImageZen\Drivers\Imagick\ImagickAlteration;
use SergiX44\ImageZen\Image;
use SergiX44\ImageZen\Shapes\Point;
use SergiX44\ImageZen\Shapes\Polygon;

class PolygonShape extends Alteration implements GdAlteration, ImagickAlteration
{
public static string $id = 'polygon';

protected array $points = [];

public function __construct(
protected array $points,
array $points,
protected ?Closure $callback = null
) {
foreach ($points as $point) {
if ($point instanceof Point) {
$this->points[] = $point->x;
$this->points[] = $point->y;
} elseif (is_array($point)) {
array_walk_recursive($point, function ($value) {
if (!is_int($value)) {
throw new InvalidArgumentException("The given array must contain only integers.");
}
$this->points[] = $value;
});
} else {
$this->points[] = $point;
}
}
}

public function applyWithGd(Image $image): null
Expand Down
58 changes: 48 additions & 10 deletions src/Draws/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function toHex(): string
/**
* Reads RGBA values from string into array
*
* @param string $value
* @param string $value
* @return array
*/
protected function rgbaFromString($value)
Expand All @@ -94,24 +94,62 @@ protected function rgbaFromString($value)

$result = [];
if (preg_match($hexPattern, $value, $matches)) {
$result[0] = strlen($matches[1]) === 1 ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]);
$result[1] = strlen($matches[2]) === 1 ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2]);
$result[2] = strlen($matches[3]) === 1 ? hexdec($matches[3] . $matches[3]) : hexdec($matches[3]);
$result[0] = strlen($matches[1]) === 1 ? hexdec($matches[1].$matches[1]) : hexdec($matches[1]);
$result[1] = strlen($matches[2]) === 1 ? hexdec($matches[2].$matches[2]) : hexdec($matches[2]);
$result[2] = strlen($matches[3]) === 1 ? hexdec($matches[3].$matches[3]) : hexdec($matches[3]);
$result[3] = 1;
} elseif (preg_match($rgbPattern, $value, $matches)) {
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int)$matches[1] : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int)$matches[2] : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int)$matches[3] : 0;
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int) $matches[1] : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int) $matches[2] : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int) $matches[3] : 0;
$result[3] = 1;
} elseif (preg_match($rgbaPattern, $value, $matches)) {
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int)$matches[1] : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int)$matches[2] : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int)$matches[3] : 0;
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int) $matches[1] : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int) $matches[2] : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int) $matches[3] : 0;
$result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? $matches[4] : 0;
} else {
throw new InvalidArgumentException("Unable to read color ({$value}).");
}

return $result;
}

public function copy(): self
{
return new self(
red: $this->red,
green: $this->green,
blue: $this->blue,
alpha: $this->alpha
);
}

public function setRed(int $value): self
{
$this->red = $value;

return $this;
}

public function setGreen(int $value): self
{
$this->green = $value;

return $this;
}

public function setBlue(int $value): self
{
$this->blue = $value;

return $this;
}

public function setAlpha(float $value): self
{
$this->alpha = $value;

return $this;
}
}
2 changes: 2 additions & 0 deletions src/Drivers/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public function clone(Image $image): GdImage|Imagick;
public function clear(?Image $image = null, ?object $raw = null): void;

public function apply(Alteration $alteration, Image $image): mixed;

public function getData(): array;
}
14 changes: 12 additions & 2 deletions src/Drivers/Gd/Gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

class Gd implements Driver
{
private $data = [];

public function isAvailable(): bool
{
return extension_loaded('gd') && function_exists('gd_info');
Expand Down Expand Up @@ -55,9 +57,12 @@ public function parseColor(Color $color): GdColor
*/
public function loadImageFrom(string $path): GdImage
{
if (file_exists($path)) {
$resource = imagecreatefromstring(file_get_contents($path));
if (file_exists($path) || filter_var($path, FILTER_VALIDATE_URL) !== false) {
$data = file_get_contents($path);
$this->data = getimagesizefromstring($data);
$resource = imagecreatefromstring($data);
} else {
$this->data = getimagesizefromstring($path);
$resource = imagecreatefromstring($path);
}

Expand Down Expand Up @@ -138,4 +143,9 @@ private function toTrueColor(GdImage $resource): GdImage

return $canvas;
}

public function getData(): array
{
return $this->data;
}
}
5 changes: 5 additions & 0 deletions src/Drivers/Imagick/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,9 @@ public function clone(Image $image): ImagickBackend
{
return clone $image->getCore();
}

public function getData(): array
{
return [];
}
}
14 changes: 14 additions & 0 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ public function name(): string
self::AVIF => 'avif',
};
}

public function mime()
{
return match ($this) {
self::PNG => 'image/png',
self::JPG => 'image/jpeg',
self::WEBP => 'image/webp',
self::GIF => 'image/gif',
self::BMP => 'image/bmp',
self::TIFF => 'image/tiff',
self::HEIC => 'image/heic',
self::AVIF => 'image/avif',
};
}
}
20 changes: 4 additions & 16 deletions src/Getters/GetMime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SergiX44\ImageZen\Getters;

use RuntimeException;
use SergiX44\ImageZen\Alteration;
use SergiX44\ImageZen\Drivers\Gd\GdAlteration;
use SergiX44\ImageZen\Drivers\Imagick\ImagickAlteration;
Expand All @@ -12,26 +11,15 @@ class GetMime extends Alteration implements GdAlteration, ImagickAlteration
{
public static string $id = 'mime';

protected function getMimeType(Image $image): string
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_buffer($finfo, $image->stream()->getContents());
finfo_close($finfo);

if ($mime === false) {
throw new RuntimeException('Unable to get mime type');
}

return $mime;
}

public function applyWithGd(Image $image): string
{
return $this->getMimeType($image);
$imageInfo = $image->getDriver()->getData();

return $imageInfo['mime'];
}

public function applyWithImagick(Image $image): string
{
return $this->getMimeType($image);
return str_ireplace('/x-', '/', $image->getCore()->getImageMimeType());
}
}
14 changes: 13 additions & 1 deletion src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ public function stream(Format $format = Format::PNG, int $quality = 90): StreamI
return $this->driver->getStream($this, $format, $quality);
}

/**
* Return the image as base64 string.
*
* @param Format $format The image format, default is PNG.
* @param int $quality The image quality, default is 90, if supported by the format.
* @return string The image as base64 string.
*/
public function base64(Format $format = Format::PNG, int $quality = 90): string
{
return 'data:'.$format->mime().';base64,'.base64_encode($this->stream($format, $quality)->getContents());
}

/**
* Get the image path if it was loaded from a file.
*
Expand All @@ -242,7 +254,7 @@ public function response(Format $format = Format::PNG, int $quality = 90): Respo
return new Response(
status: 200,
headers: [
'Content-Type' => $this->mime(),
'Content-Type' => $format->mime(),
],
body: $this->stream($format, $quality),
);
Expand Down
Loading

0 comments on commit 44c3a29

Please sign in to comment.