Skip to content

Commit

Permalink
Improve images handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benji07 authored and ogizanagi committed Apr 22, 2022
1 parent 2d9909a commit de45074
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/packages/glide.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
glide:
base_url: /image/resize
source: '%kernel.project_dir%/public'
source: '%kernel.project_dir%/content'
cache: '%kernel.project_dir%/public/resized'
pre_generate: '%env(bool:GLIDE_PRE_GENERATE_CACHE)%'
cache_with_file_extensions: true
Expand Down
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ services:
arguments:
$type: App\Model\CaseStudy
$preset: case_study_content
$source: '%kernel.project_dir%/content'

resize_images_content_processor.article:
class: App\Stenope\Processor\ResizeImagesContentProcessor
arguments:
$type: App\Model\Article
$preset: article_content
$source: '%kernel.project_dir%/content'

App\Stenope\Provider\SampleRemovalProvider.Article:
class: App\Stenope\Provider\SampleRemovalProvider
Expand Down
26 changes: 26 additions & 0 deletions src/Bridge/Glide/Bundle/DecoratingApiServerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Bridge\Glide\Bundle;

use League\Glide\ServerFactory;

class DecoratingApiServerFactory extends ServerFactory
{
public function getApi()
{
// @phpstan-ignore-next-line
return new SkippingMimeTypesApi(parent::getApi(), $this->config['skipped_types'] ?? []);
}

public static function create(array $config = [])
{
$server = parent::create($config);
$decoratedFactory = (new self($config));

$server->setApi($decoratedFactory->getApi());

return $server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
League\Glide\Responses\SymfonyResponseFactory: ~

League\Glide\Server:
factory: [League\Glide\ServerFactory, create]
factory: [App\Bridge\Glide\Bundle\DecoratingApiServerFactory, create]
arguments:
- ~ # $config set by the extension

Expand Down
33 changes: 33 additions & 0 deletions src/Bridge/Glide/Bundle/SkippingMimeTypesApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Bridge\Glide\Bundle;

use League\Glide\Api\ApiInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Mime\MimeTypesInterface;

/**
* An API implementation allowing to skip Glide resize on some specific MIME types
* (usually unsupported by Glide, or removing GIF animations for instance),
* but still allowing to pass through Glide's server and move the images to a properly public location.
*/
class SkippingMimeTypesApi implements ApiInterface
{
public function __construct(
private ApiInterface $decorated,
private array $skippedTypes = ['image/gif'],
private MimeTypesInterface $types = new MimeTypes()
) {
}

public function run($source, array $params): string
{
if (\in_array($this->types->guessMimeType($source), $this->skippedTypes, true)) {
return (string) file_get_contents($source);
}

return $this->decorated->run($source, $params);
}
}
7 changes: 7 additions & 0 deletions src/Stenope/Processor/ResizeImagesContentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ class ResizeImagesContentProcessor implements ProcessorInterface
private string $property;
private MimeTypes $mimeTypes;

private string $source;

public function __construct(
ResizedUrlGenerator $resizedUrlGenerator,
HtmlCrawlerManagerInterface $crawlers,
string $source,
string $type,
string $preset,
string $property = 'content'
Expand All @@ -36,6 +39,7 @@ public function __construct(
$this->preset = $preset;
$this->property = $property;
$this->mimeTypes = new MimeTypes();
$this->source = $source;
}

public function __invoke(array &$data, Content $content): void
Expand Down Expand Up @@ -73,6 +77,7 @@ private function processImage(\DOMElement $element): void

// Ignore unsupported image formats
if (!$this->isSupported($source)) {
// need to retrieve those as they are not in the public directory
return;
}

Expand All @@ -88,6 +93,8 @@ private function processImage(\DOMElement $element): void

private function isSupported(string $url): bool
{
$url = $this->source . $url;

try {
$mimeType = $this->mimeTypes->guessMimeType($url);
} catch (\InvalidArgumentException $exception) {
Expand Down
2 changes: 1 addition & 1 deletion templates/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{% if page == minPage and loop.first %}
<li class="miniature-highlight">
<a href="{{ path('blog_article', { article: article.slug }) }}" class="miniature-highlight__image" data-aos="fade-down">
<span class="image" style="background: url('{{ asset(article.thumbnail) }}')"></span>
<span class="image" style="{{ macros.backgroundImageSrcset(article.thumbnail, 'article_thumbnail.lg') }}"></span>
</a>
<div class="miniature-highlight__content" data-aos="zoom-in">
<div class="info">
Expand Down

0 comments on commit de45074

Please sign in to comment.