Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Add 23.1: Optimizing Images with Imagine
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 13, 2022
1 parent cc58b3b commit f9efa33
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.11",
"easycorp/easyadmin-bundle": "^4",
"imagine/imagine": "^1.2",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpdoc-parser": "^1.2",
"sensio/framework-extra-bundle": "^6.1",
Expand Down
64 changes: 63 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/ImageOptimizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App;

use Imagine\Gd\Imagine;
use Imagine\Image\Box;

class ImageOptimizer
{
private const MAX_WIDTH = 200;
private const MAX_HEIGHT = 150;

private $imagine;

public function __construct()
{
$this->imagine = new Imagine();
}

public function resize(string $filename): void
{
list($iwidth, $iheight) = getimagesize($filename);
$ratio = $iwidth / $iheight;
$width = self::MAX_WIDTH;
$height = self::MAX_HEIGHT;
if ($width / $height > $ratio) {
$width = $height * $ratio;
} else {
$height = $width / $ratio;
}

$photo = $this->imagine->open($filename);
$photo->resize(new Box($width, $height))->save($filename);
}
}
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
"friendsofphp/proxy-manager-lts": {
"version": "v1.0.5"
},
"imagine/imagine": {
"version": "1.2.4"
},
"laminas/laminas-code": {
"version": "4.5.1"
},
Expand Down

0 comments on commit f9efa33

Please sign in to comment.