Skip to content

Commit

Permalink
Merge pull request #1 from Assmay/add_presets
Browse files Browse the repository at this point in the history
add presets
  • Loading branch information
Assmay authored Sep 22, 2019
2 parents 35f5be3 + 1ebd1a6 commit 31d2825
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
40 changes: 32 additions & 8 deletions src/AlexGiuvara/ImgProxy/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ class Image
* @var mixed
*/
protected $url;
/**
* @var string
*/
protected $preset;

/**
* Init most common resize settings. Later you can update defaults
* @param string $path
* @param int $width
* @param int $height
*/
public function make(string $path, int $width, int $height)
public function make(string $path, int $width, int $height, $extension=null)
{
$this->setOriginalPictureUrl($path)
->setWidth($width)
Expand All @@ -63,11 +67,26 @@ public function make(string $path, int $width, int $height)
->setGravity('no')
->setEnlarge(0)
//convert img to extension
->setExtension('jpg');
->setExtension($extension);

return $this;
}
public function makePreset(string $path, string $preset, $extension=null)
{
$this->setOriginalPictureUrl($path)
->setPreset($preset)
->setExtension($extension);

return $this;
}

public function getPreset(){
return $this->preset;
}
public function setPreset($preset){
$this->preset = $preset;
return $this;
}
/**
* @param string $argument1
* @return mixed
Expand Down Expand Up @@ -183,16 +202,21 @@ public function getEnlarge(): int
/**
* @param $argument1
*/
public function setExtension(string $argument1)
public function setExtension($argument1)
{
$argument1 = Str::lower($argument1);

if (! in_array($argument1, config('img-proxy.formats'))) {
throw new InvalidFormat($argument1);
if (is_null($argument1)){
$argument1 = config('img-proxy.default_extension');
}
if ($argument1 != false){
$argument1 = Str::lower($argument1);

if (! in_array($argument1, config('img-proxy.formats'))) {
throw new InvalidFormat($argument1);
}
}
if (!$argument1)
$argument1 = '';
$this->extension = $argument1;

return $this;
}

Expand Down
46 changes: 34 additions & 12 deletions src/AlexGiuvara/ImgProxy/ImageSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ class ImageSignature implements ImageSignatureInterface
* @var Image
*/
private $img;
/**
* @var int
*/
private $signature_size = null;

/**
* TODO ImageInterface
* @param ImageInterface $img
*/
public function __construct($img)
{
if (env('IMGPROXY_SIGNATURE_SIZE', null)){
$signature_size = env('IMGPROXY_SIGNATURE_SIZE', null);
if (is_numeric($signature_size))
$this->signature_size = (int)$signature_size;
}
$this->img = $img;
}

Expand All @@ -33,13 +42,16 @@ public function __construct($img)
public function take(): string
{
$path = $this->getPath();
$signature = rtrim(strtr(base64_encode(hash_hmac(
$signature = hash_hmac(
'sha256',
$this->getBinarySalt() . $path,
$this->getBinaryKey(),
true
)), '+/', '-_'), '=');
);
if ($this->signature_size)
$signature = pack('A'.$this->signature_size, $signature);

$signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '=');
return sprintf("/%s%s", $signature, $path);
}
// below are the methods used by take()
Expand Down Expand Up @@ -107,15 +119,25 @@ public function getEncodedUrl(): string

public function getPath(): string
{
return sprintf(
"/%s/%d/%d/%s/%d/%s.%s",
$this->img->getResize(),
$this->img->getWidth(),
$this->img->getHeight(),
$this->img->getGravity(),
$this->img->getEnlarge(),
$this->getEncodedURL(),
$this->img->getExtension()
);
if ($this->img->getPreset()){
$path = sprintf(
"/%s/%s%s",
$this->img->getPreset(),
$this->getEncodedURL(),
$this->img->getExtension()
);
}else{
$path = sprintf(
"/%s/%d/%d/%s/%d/%s%s",
$this->img->getResize(),
$this->img->getWidth(),
$this->img->getHeight(),
$this->img->getGravity(),
$this->img->getEnlarge(),
$this->getEncodedURL(),
$this->img->getExtension()
);
}
return $path;
}
}
12 changes: 10 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
* @param int $width
* @param int $height
*/
function imgProxy(string $path, int $width, int $height): string
function imgProxy(string $path, int $width, int $height, $extension=null): string
{
app()->instance(Image::class, (new Image)->make($path, $width, $height));
app()->instance(Image::class, (new Image)->make($path, $width, $height, $extension));

return config('img-proxy.base_url') .
app(ImageSignatureInterface::class)->take();
}

function imgProxyPreset(string $path, string $preset, $extension=null): string
{
app()->instance(Image::class, (new Image)->makePreset($path, $preset, $extension));

return config('img-proxy.base_url') .
app(ImageSignatureInterface::class)->take();
}
}
3 changes: 2 additions & 1 deletion tests/config/img-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
/**
* The supported formats
*/
'formats' => ['jpeg', 'jpg', 'png', 'gif', 'webp'],
'formats' => ['.jpeg', '.jpg', '.png', '.gif', '.webp'],
'default_extinsion' => false,
];

0 comments on commit 31d2825

Please sign in to comment.