-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Glide for on-demand image resizing
- Loading branch information
Showing
11 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Standard\Controllers; | ||
|
||
use League\Glide\Server; | ||
use Standard\Abstracts\Controller; | ||
use Twig_Environment; | ||
|
||
class ImageController extends Controller | ||
{ | ||
/** | ||
* @Inject("glide") | ||
* @var Server | ||
*/ | ||
private $glide; | ||
|
||
private $allowedWidths = [ | ||
320, | ||
460, | ||
768, | ||
992, | ||
1280, | ||
1920, | ||
]; | ||
|
||
/** | ||
* @var Twig_Environment | ||
*/ | ||
private $twig; | ||
|
||
public function __construct(Twig_Environment $twig) | ||
{ | ||
$this->twig = $twig; | ||
} | ||
|
||
public function renderImage($image) | ||
{ | ||
$width = $this->getWidth($image); | ||
|
||
$imageSource = str_replace('-'.$width, '', $image); | ||
|
||
$imagePath = $this->glide->makeImage($imageSource, ['w' => $width]); | ||
$imageBase = $this->glide->getCache()->read($imagePath); | ||
$this->glide->getCache()->put($image, $imageBase); | ||
$this->glide->outputImage($imageSource, ['w' => $width]); | ||
} | ||
|
||
/** | ||
* @param string $imagePath | ||
* @return int | ||
*/ | ||
private function getWidth(string $imagePath) : int | ||
{ | ||
$fragments = explode('-', $imagePath); | ||
|
||
if (count($fragments) < 2) { | ||
header( | ||
($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0') . ' 500 ' . 'Server Error ImgErr00' | ||
); | ||
die("Nope! Image fragments missing."); | ||
} | ||
|
||
$width = (int)explode('.', $fragments[1])[0]; | ||
if (!in_array($width, $this->allowedWidths)) { | ||
header( | ||
($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0') . ' 500 ' . 'Server Error ImgErr01' | ||
); | ||
die("Nope! Disallowed width: ".$width." for image ".$imagePath); | ||
} | ||
|
||
return $width; | ||
} | ||
|
||
public function demo() | ||
{ | ||
echo $this->twig->render('image/demo.twig'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends 'layout/master.twig' %} | ||
|
||
{% block content %} | ||
|
||
<h1>Demo of on-demand images</h1> | ||
|
||
<p>The image below was generated by requesting "public/static/image/sitepoint_logo-320.png", which did not exist beforehand. It was generated from "assets/image/sitepoint_logo.png". Try changing its width in dev tools to one of the following to generate a new one, or to any other width to generate an error. You can alter the allowed widths in "Standard/Controllers/ImageController", and you can change the path from which the source is read and to which the processed image is saved in "app/config.php".</p> | ||
|
||
<p><img src="/static/image/sitepoint_logo-320.png" | ||
alt="SitePoint Logo Resized"></p> | ||
|
||
<p>Feel free to delete "public/static/image/sitepoint_logo-320.png" and refresh this page - it will re-appear.</p> | ||
|
||
<p>Adding more images is just a matter of adding extra source images into "assets/image" and using them with a width suffix. Note that the widths are limited to prevent an image-processing attack (someone could request a billion sizes if it weren't limited).</p> | ||
|
||
{% endblock %} |