Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d61848f
Add support for HEIC/HEIF to JPEG conversion
NikolayBalkandzhiyski Dec 18, 2025
ec8755f
Normalize image extension input in ConvertableImageType enum
NikolayBalkandzhiyski Dec 18, 2025
6cdac9d
-Add SPDX license headers to multiple files and normalize variable names
NikolayBalkandzhiyski Dec 18, 2025
0c7cc69
Add migration to change 'title' column type to TEXT in photos table b…
NikolayBalkandzhiyski Dec 22, 2025
14ccea0
Update migration to change 'title' column type from TEXT to VARCHAR(1…
NikolayBalkandzhiyski Jan 6, 2026
70e7d01
Refactor migration to change 'title' column type from VARCHAR(1000) t…
NikolayBalkandzhiyski Jan 6, 2026
74d7567
Enhance migration for 'title' column in photos table by adding index …
NikolayBalkandzhiyski Jan 6, 2026
303ee3f
Add support for HEIC to JPEG conversion using maestroerror/php-heic-t…
NikolayBalkandzhiyski Jan 6, 2026
30e0553
Improve error handling in HEIC to JPEG conversion by adding nested ex…
NikolayBalkandzhiyski Jan 6, 2026
f24e705
added comment
NikolayBalkandzhiyski Jan 6, 2026
7e0b9f6
Update ConvertMediaFileInterface to support both Imagick and HeicToJp…
NikolayBalkandzhiyski Jan 6, 2026
25cfca0
Merge remote-tracking branch 'upstream/master' into feature/#298-HEIC…
NikolayBalkandzhiyski Jan 6, 2026
584deef
chore: Update HEIC to JPEG conversion logic and improve error handling
NikolayBalkandzhiyski Jan 6, 2026
3fad818
chore: Update composer.lock and add new dependencies
NikolayBalkandzhiyski Jan 6, 2026
a64fbc4
Improve error messages in HEIF to JPEG conversion and update migratio…
NikolayBalkandzhiyski Jan 6, 2026
142f145
Fix formatting of SPDX license comment in migration file for photos t…
NikolayBalkandzhiyski Jan 6, 2026
f8f91e4
chore: update copyright year in multiple files to 2026
NikolayBalkandzhiyski Jan 6, 2026
c2d5a1f
Refactor ImageTypeFactory and ConvertUnsupportedMedia for improved er…
NikolayBalkandzhiyski Jan 7, 2026
3213960
Update index creation logic for title column in photos table based on…
NikolayBalkandzhiyski Jan 7, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions app/Actions/Photo/Convert/HeifToJpeg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Actions\Photo\Convert;

use App\Contracts\Image\ConvertMediaFileInterface;
use App\Image\Files\NativeLocalFile;
use App\Image\Files\TemporaryJobFile;
use Http\Client\Exception;
use Maestroerror\HeicToJpg;
use Safe\Exceptions\FilesystemException;
use function Safe\unlink;

class HeifToJpeg implements ConvertMediaFileInterface
{
/**
* @throws \Exception
*/
public function handle(NativeLocalFile $tmp_file): TemporaryJobFile
{
try {
$base_name = $tmp_file->getBasename();
$path = $tmp_file->getRealPath();
$pathinfo = pathinfo($path);
$new_path = $pathinfo['dirname'] . '/' . $base_name . '.jpg';

// Convert to Jpeg
$imagick_converted = $this->convertToJpeg($path);

// Store converted image
$this->storeNewImage($imagick_converted, $new_path);

// Delete old file
$this->deleteOldFile($path);

return new TemporaryJobFile($new_path);
} catch (\Exception $e) {
throw new \Exception('Failed to convert HEIC/HEIF to JPEG. ' . $e->getMessage());
}
}

/**
* @throws \Exception
*/
public function storeNewImage(\Imagick|HeicToJpg $image_instance, string $store_to_path): void
{
try {
if ($image_instance instanceof \Imagick) {
$image_instance->writeImage($store_to_path);
} elseif ($image_instance instanceof HeicToJpg) {
$image_instance->saveAs($store_to_path);
}
} catch (\ImagickException $e) {
throw new \Exception('Failed to store converted image');
}
}

/**
* @throws \Exception
*/
public function deleteOldFile(string $path): void
{
try {
unlink($path);
} catch (FilesystemException $e) {
throw new \Exception('Failed to delete old file');
}
}

/**
* Try ImageMagick, if fails try php-heic-to-jpg package because ImageMagick fails to convert newer IPhone images.
*
* @throws \Exception
*/
private function convertToJpeg(string $path): \Imagick|HeicToJpg
{
try {
$img = new \Imagick($path);

if ($img->getNumberImages() > 1) {
$img->setIteratorIndex(0);
}

$img->setImageFormat('jpeg');
$img->setImageCompression(\Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(92);

$img->autoOrient();

return $img;
} catch (\ImagickException $e) {
try {
return HeicToJpg::convert($path);
} catch (Exception $exception) {
throw new \Exception('Failed to convert HEIC/HEIF to JPEG. ' . $e->getMessage() . ' ' . $exception->getMessage());
}
}
}
}
32 changes: 32 additions & 0 deletions app/Actions/Photo/Convert/ImageTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Actions\Photo\Convert;

use App\Enum\ConvertableImageType;

class ImageTypeFactory
{
public ?string $convertionClass = null;

public function __construct(string $extension)
{
$this->convertionClass = match (true) {
ConvertableImageType::isHeifImageType($extension) => 'HeifToJpeg',
// TODO: Add more convertion types/classes
default => null,
};
}

public function make(): mixed
{
$class = 'App\Actions\Photo\Convert\\' . $this->convertionClass;

return new $class();
}
}
1 change: 1 addition & 0 deletions app/Actions/Photo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function add(NativeLocalFile $source_file, ?AbstractAlbum $album, ?int $f
);

$pre_pipes = [
Init\ConvertUnsupportedMedia::class,
Init\AssertSupportedMedia::class,
Init\FetchLastModifiedTime::class,
Init\MayLoadFileMetadata::class,
Expand Down
41 changes: 41 additions & 0 deletions app/Actions/Photo/Pipes/Init/ConvertUnsupportedMedia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Actions\Photo\Pipes\Init;

use App\Actions\Photo\Convert\ImageTypeFactory;
use App\Contracts\PhotoCreate\InitPipe;
use App\DTO\PhotoCreate\InitDTO;
use App\Exceptions\CannotConvertMediaFileException;

class ConvertUnsupportedMedia implements InitPipe
{
/**
* Tries to convert the file to a supported format.
*
* @throws \Exception
*/
public function handle(InitDTO $state, \Closure $next): InitDTO
{
$ext = ltrim($state->source_file->getOriginalExtension(), '.');

$factory = new ImageTypeFactory($ext);

if ($factory->convertionClass === null) {
return $next($state);
}

try {
$state->source_file = $factory->make()->handle($state->source_file);
} catch (\Exception $exception) {
throw new CannotConvertMediaFileException($exception->getMessage());
}

return $next($state);
}
}
22 changes: 22 additions & 0 deletions app/Contracts/Image/ConvertMediaFileInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Contracts\Image;

use App\Image\Files\NativeLocalFile;
use App\Image\Files\TemporaryJobFile;
use Maestroerror\HeicToJpg;

interface ConvertMediaFileInterface
{
public function handle(NativeLocalFile $tmp_file): TemporaryJobFile;

public function storeNewImage(\Imagick|HeicToJpg $image_instance, string $store_to_path);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Add explicit return type and reconsider the tight coupling.

The storeNewImage method is missing a return type declaration. Additionally, the union type \Imagick|HeicToJpg tightly couples this interface to specific implementation classes. Consider accepting a more generic type or creating an abstraction to reduce coupling.

🔎 Suggested refactor
-	public function storeNewImage(\Imagick|HeicToJpg $image_instance, string $store_to_path);
+	public function storeNewImage(\Imagick|HeicToJpg $image_instance, string $store_to_path): void;

For better design, consider creating an adapter interface:

interface ImageResourceInterface {
    public function save(string $path): void;
}

Then change the signature to:

public function storeNewImage(ImageResourceInterface $image_instance, string $store_to_path): void;

As per coding guidelines, all methods should have explicit return types.


public function deleteOldFile(string $path);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Add explicit return type.

The deleteOldFile method is missing a return type declaration.

🔎 Suggested fix
-	public function deleteOldFile(string $path);
+	public function deleteOldFile(string $path): void;

As per coding guidelines, all methods should have explicit return types.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function deleteOldFile(string $path);
public function deleteOldFile(string $path): void;

}
25 changes: 25 additions & 0 deletions app/Enum/ConvertableImageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Enum;

enum ConvertableImageType: string
{
case HEIC = 'heic';
case HEIF = 'heif';

public static function isHeifImageType(string $extension): bool
{
$extension = str($extension)->lower()->toString();

return in_array($extension, [
self::HEIC->value,
self::HEIF->value,
], true);
}
}
19 changes: 19 additions & 0 deletions app/Enum/ImageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Enum;

enum ImageType: int
{
case JPEG = IMAGETYPE_JPEG;
case JPEG2000 = IMAGETYPE_JPEG2000;
case PNG = IMAGETYPE_PNG;
case GIF = IMAGETYPE_GIF;
case WEBP = IMAGETYPE_WEBP;
}

26 changes: 26 additions & 0 deletions app/Exceptions/CannotConvertMediaFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Exceptions;

use Symfony\Component\HttpFoundation\Response;

/**
* CannotConvertMediaFileException.
*
* Indicates that a media file cannot be converted to another format.
*/
class CannotConvertMediaFileException extends BaseLycheeException
{
public const string DEFAULT_MESSAGE = 'Cannot convert media file to another format';

public function __construct(string $msg = self::DEFAULT_MESSAGE, ?\Throwable $previous = null)
{
parent::__construct(Response::HTTP_UNPROCESSABLE_ENTITY, $msg, $previous);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"lychee-org/nestedset": "^11.0.0",
"lychee-org/php-exif": "^1.0.4",
"maennchen/zipstream-php": "^2.1 || ^3.1",
"maestroerror/php-heic-to-jpg": "^1.0",
"mavinoo/laravel-batch": "^2.4",
"moneyphp/money": "^4.7",
"omnipay/dummy": "^3.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

require_once 'TemporaryModels/OptimizeTables.php';

return new class() extends Migration {
private OptimizeTables $optimize;

public function __construct()
{
$this->optimize = new OptimizeTables();
}

/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('photos', function (Blueprint $table) {
// Drop the existing index
if (Schema::hasIndex('photos', 'photos_album_id_is_starred_title_index')) {
$table->dropIndex('photos_album_id_is_starred_title_index');
}

// Change to text
$table->text('title')->change();

// Recreate the index with a key length for the TEXT column
$table->index(['old_album_id', 'is_starred', DB::raw('title(100)')], 'photos_album_id_is_starred_title_index');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('photos', function (Blueprint $table) {
// Drop the existing index
if (Schema::hasIndex('photos', 'photos_album_id_is_starred_title_index')) {
$table->dropIndex('photos_album_id_is_starred_title_index');
}

$table->string('title', 100)->nullable()->change();

$table->index(['album_id', 'is_starred', 'title']);
});
}
};