Step 23: Resizing Images

Resizing Images

On the conference page design, photos are constrained to a maximum size of 200 by 150 pixels. What about optimizing the images and reducing their size if the uploaded original is larger than the limits?

That is a perfect job that can be added to the comment workflow, probably just after the comment is validated and just before it is published.

Let’s add a new ready state and an optimize transition:

patch_file

  1. --- a/config/packages/workflow.yaml
  2. +++ b/config/packages/workflow.yaml
  3. @@ -16,6 +16,7 @@ framework:
  4. - potential_spam
  5. - spam
  6. - rejected
  7. + - ready
  8. - published
  9. transitions:
  10. accept:
  11. @@ -29,13 +30,16 @@ framework:
  12. to: spam
  13. publish:
  14. from: potential_spam
  15. - to: published
  16. + to: ready
  17. reject:
  18. from: potential_spam
  19. to: rejected
  20. publish_ham:
  21. from: ham
  22. - to: published
  23. + to: ready
  24. reject_ham:
  25. from: ham
  26. to: rejected
  27. + optimize:
  28. + from: ready
  29. + to: published

Generate a visual representation of the new workflow configuration to validate that it describes what we want:

  1. $ symfony console workflow:dump comment | dot -Tpng -o workflow.png

../_images/workflow-final.png

Optimizing Images with Imagine

Image optimizations will be done thanks to GD (check that your local PHP installation has the GD extension enabled) and Imagine:

  1. $ symfony composer req "imagine/imagine:^1.2"

Resizing an image can be done via the following service class:

src/ImageOptimizer.php

  1. namespace App;
  2. use Imagine\Gd\Imagine;
  3. use Imagine\Image\Box;
  4. class ImageOptimizer
  5. {
  6. private const MAX_WIDTH = 200;
  7. private const MAX_HEIGHT = 150;
  8. private $imagine;
  9. public function __construct()
  10. {
  11. $this->imagine = new Imagine();
  12. }
  13. public function resize(string $filename): void
  14. {
  15. list($iwidth, $iheight) = getimagesize($filename);
  16. $ratio = $iwidth / $iheight;
  17. $width = self::MAX_WIDTH;
  18. $height = self::MAX_HEIGHT;
  19. if ($width / $height > $ratio) {
  20. $width = $height * $ratio;
  21. } else {
  22. $height = $width / $ratio;
  23. }
  24. $photo = $this->imagine->open($filename);
  25. $photo->resize(new Box($width, $height))->save($filename);
  26. }
  27. }

After optimizing the photo, we store the new file in place of the original one. You might want to keep the original image around though.

Adding a new Step in the Workflow

Modify the workflow to handle the new state:

patch_file

  1. --- a/src/MessageHandler/CommentMessageHandler.php
  2. +++ b/src/MessageHandler/CommentMessageHandler.php
  3. @@ -2,6 +2,7 @@
  4. namespace App\MessageHandler;
  5. +use App\ImageOptimizer;
  6. use App\Message\CommentMessage;
  7. use App\Repository\CommentRepository;
  8. use App\SpamChecker;
  9. @@ -21,10 +22,12 @@ class CommentMessageHandler implements MessageHandlerInterface
  10. private $bus;
  11. private $workflow;
  12. private $mailer;
  13. + private $imageOptimizer;
  14. private $adminEmail;
  15. + private $photoDir;
  16. private $logger;
  17. - public function __construct(EntityManagerInterface $entityManager, SpamChecker $spamChecker, CommentRepository $commentRepository, MessageBusInterface $bus, WorkflowInterface $commentStateMachine, MailerInterface $mailer, string $adminEmail, LoggerInterface $logger = null)
  18. + public function __construct(EntityManagerInterface $entityManager, SpamChecker $spamChecker, CommentRepository $commentRepository, MessageBusInterface $bus, WorkflowInterface $commentStateMachine, MailerInterface $mailer, ImageOptimizer $imageOptimizer, string $adminEmail, string $photoDir, LoggerInterface $logger = null)
  19. {
  20. $this->entityManager = $entityManager;
  21. $this->spamChecker = $spamChecker;
  22. @@ -32,7 +35,9 @@ class CommentMessageHandler implements MessageHandlerInterface
  23. $this->bus = $bus;
  24. $this->workflow = $commentStateMachine;
  25. $this->mailer = $mailer;
  26. + $this->imageOptimizer = $imageOptimizer;
  27. $this->adminEmail = $adminEmail;
  28. + $this->photoDir = $photoDir;
  29. $this->logger = $logger;
  30. }
  31. @@ -64,6 +69,12 @@ class CommentMessageHandler implements MessageHandlerInterface
  32. ->to($this->adminEmail)
  33. ->context(['comment' => $comment])
  34. );
  35. + } elseif ($this->workflow->can($comment, 'optimize')) {
  36. + if ($comment->getPhotoFilename()) {
  37. + $this->imageOptimizer->resize($this->photoDir.'/'.$comment->getPhotoFilename());
  38. + }
  39. + $this->workflow->apply($comment, 'optimize');
  40. + $this->entityManager->flush();
  41. } elseif ($this->logger) {
  42. $this->logger->debug('Dropping comment message', ['comment' => $comment->getId(), 'state' => $comment->getState()]);
  43. }

Note that $photoDir is automatically injected as we defined a container bind on this variable name in a previous step:

config/packages/services.yaml

  1. services:
  2. _defaults:
  3. bind:
  4. $photoDir: "%kernel.project_dir%/public/uploads/photos"

Storing Uploaded Data in Production

We have already defined a special read-write directory for uploaded files in .symfony.cloud.yaml. But the mount is local. If we want the web container and the message consumer worker to be able to access the same mount, we need to create a file service:

patch_file

  1. --- a/.symfony/services.yaml
  2. +++ b/.symfony/services.yaml
  3. @@ -19,3 +19,7 @@ varnish:
  4. vcl: !include
  5. type: string
  6. path: config.vcl
  7. +
  8. +files:
  9. + type: network-storage:1.0
  10. + disk: 256

Use it for the photos upload directory:

patch_file

  1. --- a/.symfony.cloud.yaml
  2. +++ b/.symfony.cloud.yaml
  3. @@ -37,7 +37,7 @@ web:
  4. mounts:
  5. "/var": { source: local, source_path: var }
  6. - "/public/uploads": { source: local, source_path: uploads }
  7. + "/public/uploads": { source: service, service: files, source_path: uploads }
  8. hooks:
  9. build: |

This should be enough to make the feature work in production.


This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.