Skip to content

Commit

Permalink
fix: move Encoder manipulator do Api
Browse files Browse the repository at this point in the history
  • Loading branch information
konnng-dev committed Jun 3, 2024
1 parent b0086f8 commit 62623fe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace League\Glide\Api;

use Intervention\Image\ImageManager;
use League\Glide\Manipulators\Encode;
use Intervention\Image\Interfaces\ImageInterface;
use League\Glide\Manipulators\ManipulatorInterface;

class Api implements ApiInterface
Expand Down Expand Up @@ -92,13 +92,23 @@ public function run(string $source, array $params): string

foreach ($this->manipulators as $manipulator) {
$manipulator->setParams($params);
/** @var \Intervention\Image\Interfaces\ImageInterface */
$image = $manipulator->run($image);
}

$encoder = new Encode();
$encoder->setParams($params);
/** @var \Intervention\Image\Interfaces\EncodedImageInterface */
return $this->encode($image, $params);
}

/**
* Perform image encoding to a given format.
*
* @param ImageInterface $image Image object
* @param array $params the manipulator params
*
* @return string Manipulated image binary data
*/
public function encode(ImageInterface $image, array $params): string
{
$encoder = new Encode($params);
$encoded = $encoder->run($image);

return $encoded->toString();
Expand Down
36 changes: 33 additions & 3 deletions src/Manipulators/Encode.php → src/Api/Encode.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
<?php

namespace League\Glide\Manipulators;
namespace League\Glide\Api;

use Exception;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\ImageInterface;

class Encode extends BaseManipulator
{
/**
* Encoder Api class to convert a given image to a specific format.
*/
class Encode {

/**
* The manipulation params.
*/
private $params;

/**
* Class constructor.
*
* @param array $params the manipulator params
*/
public function __construct(array $params = [])
{
$this->params = $params;
}

/**
* Get a specific manipulation param.
*/
public function getParam(string $name): mixed
{
return array_key_exists($name, $this->params)
? $this->params[$name]
: null;
}

/**
* Perform output image manipulation.
*
Expand Down Expand Up @@ -42,6 +71,7 @@ public function run(ImageInterface $image): EncodedImageInterface
$encoderOptions['interlaced'] = $shouldInterlace;
break;
default:
throw new Exception("Invalid format provided: {$format}");
}

return $image->encodeByExtension(...$encoderOptions);
Expand Down

0 comments on commit 62623fe

Please sign in to comment.