Skip to content

Instantly share code, notes, and snippets.

@lfjeff
Last active January 13, 2019 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfjeff/0e9d2c96b2b8bc7fd9005f44b96870d5 to your computer and use it in GitHub Desktop.
Save lfjeff/0e9d2c96b2b8bc7fd9005f44b96870d5 to your computer and use it in GitHub Desktop.
Create Symfony 4 Twig image_proc() function to optimize images using images.weserv.nl
<?php
// src/Twig/AppExtension.php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
private $imageProcUrl;
public function __construct(?string $imageProcUrl)
{
$this->imageProcUrl = $imageProcUrl; // value from config parameter app.image_proc_url
}
public function getFunctions()
{
return [
new TwigFunction('image_proc', [$this, 'imageProc']),
];
}
/**
* Process images according to $queryOpts values.
*
* For details on how this works, see https://images.weserv.nl
*
*/
public function imageProc(string $imageUrl, string $queryOpts = '', $processLocalFiles = false)
{
if (!trim($this->imageProcUrl)) {
// bypass if imageProcUrl is blank
return $imageUrl;
}
$imageProcUrl = $this->imageProcUrl;
//
// add host if missing (only for local files)
//
if (!preg_match('~^(//|https?://)~', $imageUrl)) {
if (!$processLocalFiles) {
return $imageUrl;
}
$dir = '';
if (!preg_match('~^/~', $imageUrl)) {
$dir = dirname($_SERVER['REQUEST_URI']);
}
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443) {
$imageUrl = "https://$_SERVER[SERVER_NAME]$dir$imageUrl";
} else {
$imageUrl = "http://$_SERVER[SERVER_NAME]$dir$imageUrl";
}
}
// if webp is not supported, remove any output option
if (false === stripos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {
$queryOpts = preg_replace('/&?output=webp/', '', $queryOpts);
}
$imageUrl = preg_replace(['~^//~', '~^http://~', '~^https://~'], ['', '', 'ssl:'], $imageUrl);
return "$imageProcUrl/?url=$imageUrl&$queryOpts";
}
}
{# example of how to use image_proc() function in a template #}
<html>
<body>
<img width="100" src="{{ image_proc('https://images.mysafetysign.com/img/lg/S/danger-high-voltage-area-sign-s-8285.png', 'w=100') }}" />
</body>
</html>
# config/services.yaml
#
# Add these parameters to your services.yaml file
#
parameters:
# URL of image processing service (used by Twig image_proc() function)
# if blank, disable image processing
app.image_proc_url: '//images.weserv.nl'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
bind: # set up special global autowiring rules
$imageProcUrl: '%app.image_proc_url%'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment