Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method to output base64 #7

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/Alterations/Fill.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function applyWithImagick(Image $image): null

// replace image core
$this->replaceCore($image, $canvas);
// flood fill with color
// flood fill with color
} elseif ($this->color !== null) {
// create canvas with filling
$canvas = new \Imagick();
Expand Down
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
12 changes: 12 additions & 0 deletions tests/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,15 @@ function prepare($instance, string $name, Backend $driver, string $ext = 'png'):
->imageSimilarTo($expected);
unlink($out);
})->with('drivers', 'fruit');

it('can output an image as base64 with png', function ($driver, $file) {
prepare($this, '_', $driver);
$b64 = Image::make($file, $driver)->base64();
expect($b64)->toStartWith('data:image/png;base64,iVBORw0KGgo');
})->with('drivers', 'tile');

it('can output an image as base64 with jpg', function ($driver, $file) {
prepare($this, '_', $driver);
$b64 = Image::make($file, $driver)->base64(\SergiX44\ImageZen\Format::JPG);
expect($b64)->toStartWith('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ');
})->with('drivers', 'tile');