Skip to content

Commit

Permalink
Merge branch 'master' into run_script_improvemetns
Browse files Browse the repository at this point in the history
  • Loading branch information
angelk committed Nov 21, 2024
2 parents 7a60bfa + 10193d4 commit 26713c6
Show file tree
Hide file tree
Showing 9 changed files with 2,181 additions and 2,139 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"symfony/webpack-encore-bundle": "^2.1.0",
"symfony/yaml": "*",
"tekstove/url-video-parser": "^1.0",
"twig/twig": "^2.15.5"
"twig/twig": "^3.1.12"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
Expand Down
1,512 changes: 751 additions & 761 deletions composer.lock

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions src/Controller/Place/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Image\ImageEdit;
use App\Repository\PlaceRepository;
use App\Upload\ImageUploader;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function addImage(string $id, Request $request, PlaceRepository $placeRep
* This method is called when thumbnails doesn't exists.
* Thumbnail will be created and saved in the "public" directory.
*/
public function generateThumbnail(int $year, string $placeId, string $imagePath, int $maxWidth, int $maxHeight, Request $request)
public function generateThumbnail(int $year, string $placeId, string $imagePath, int $maxWidth, int $maxHeight, Request $request, LoggerInterface $logger)
{
$originalImagePath = $this->getParameter('place_images_directory') . DIRECTORY_SEPARATOR;
$originalImagePath .= $year . DIRECTORY_SEPARATOR . $placeId . DIRECTORY_SEPARATOR . $imagePath;
Expand All @@ -55,9 +56,17 @@ public function generateThumbnail(int $year, string $placeId, string $imagePath,
$thumbnailPathDir .= DIRECTORY_SEPARATOR . $year . DIRECTORY_SEPARATOR . $placeId;
$thumbnailPath = $thumbnailPathDir . DIRECTORY_SEPARATOR . $imagePath;

$resizer = new ImageEdit($originalImagePath);
$resizer = new ImageEdit($originalImagePath, $logger);
$resizer->resize($maxWidth, $maxHeight);
$resizer->watermark('track-hub.com');

if ($resizer->shouldBeConverted()) {
$resizer->convertToJpeg();
$thumbnailPathArray = explode('.', $thumbnailPath);
array_push($thumbnailPathArray, 'jpeg');
$thumbnailPath = implode(".", $thumbnailPathArray);
}

$resizer->save($thumbnailPath);

return new Response(
Expand Down
13 changes: 11 additions & 2 deletions src/Controller/Track/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Image\ImageEdit;
use App\Repository\TrackRepository;
use App\Upload\ImageUploader;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function addImage(string $id, Request $request, TrackRepository $trackRep
* This method is called when thumbnails doesn't exists.
* Thumbnail will be created and saved in the "public" directory.
*/
public function generateThumbnail(int $year, string $trackId, string $imagePath, int $maxWidth, int $maxHeight, Request $request)
public function generateThumbnail(int $year, string $trackId, string $imagePath, int $maxWidth, int $maxHeight, Request $request, LoggerInterface $logger)
{
$originalImagePath = $this->getParameter('track_images_directory') . DIRECTORY_SEPARATOR;
$originalImagePath .= $year . DIRECTORY_SEPARATOR . $trackId . DIRECTORY_SEPARATOR . $imagePath;
Expand All @@ -55,9 +56,17 @@ public function generateThumbnail(int $year, string $trackId, string $imagePath,
$thumbnailPathDir .= DIRECTORY_SEPARATOR . $year . DIRECTORY_SEPARATOR . $trackId;
$thumbnailPath = $thumbnailPathDir . DIRECTORY_SEPARATOR . $imagePath;

$resizer = new ImageEdit($originalImagePath);
$resizer = new ImageEdit($originalImagePath, $logger);
$resizer->resize($maxWidth, $maxHeight);
$resizer->watermark('track-hub.com');

if ($resizer->shouldBeConverted()) {
$resizer->convertToJpeg();
$thumbnailPathArray = explode('.', $thumbnailPath);
array_push($thumbnailPathArray, 'jpeg');
$thumbnailPath = implode(".", $thumbnailPathArray);
}

$resizer->save($thumbnailPath);

return new Response(
Expand Down
37 changes: 34 additions & 3 deletions src/Image/ImageEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

namespace App\Image;

use Imagick;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;

class ImageEdit
{
private $input;
private $image;

public function __construct(string $input)
private LoggerInterface $logger;

public function __construct(string $input, LoggerInterface $logger)
{
$this->input = $input;
$this->image = new \Imagick($input);
$this->logger = $logger;
$this->image = new Imagick($input);

$this->image->setImageOrientation($this->image->getImageOrientation());
$rotateAngle = $this->getRotateAngle();
Expand All @@ -31,6 +36,22 @@ public function __construct(string $input)
}
}

public function shouldBeConverted(): bool
{
$format = $this->image->getImageFormat();

$convertableFormats = [
'HEIC',
];

return in_array($format, $convertableFormats);
}

public function convertToJpeg()
{
$this->image->setImageFormat('jpeg');
}

public function resize(int $maxW, int $maxH)
{
$resizedW = min($this->image->getImageWidth(), $maxW);
Expand All @@ -54,7 +75,17 @@ public function watermark(string $text)

public function getRotateAngle(): int
{
$exifData = exif_read_data($this->input);
$exifData = [];
try {
$exifData = exif_read_data($this->input);
} catch (\Exception $e) {
$this->logger->warning(
'unable to read exif data',
[
'error' => $e->getMessage(),
]
);
}

if (isset($exifData['Orientation'])) {
switch ($exifData['Orientation']) {
Expand Down
2 changes: 1 addition & 1 deletion src/Upload/ImageUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function addImage(string $id, Request $request, callable $entityCreator):
$extension = $file->getClientOriginalExtension();
$extension = mb_strtolower($extension);

if (!in_array($extension, ['jpeg', 'jpg', 'png', 'gif'])) {
if (!in_array($extension, ['jpeg', 'jpg', 'png', 'gif', 'heic'])) {
return [
null,
new Response(
Expand Down
2 changes: 0 additions & 2 deletions tests/phpcs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
/vendor
/composer.lock

99 changes: 99 additions & 0 deletions tests/phpcs/composer.lock

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

Loading

0 comments on commit 26713c6

Please sign in to comment.