Strip photo metadata with GD (Imagick keeps EXIF), --force option, stricter test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lars StommerandClaude Opus 5.5 committed 2026-10-22 21:43:54 +02:00
1 parent 4f69b426a4
commit 6690beafe6
4 files changed
+16 -7

No files matched your search

+2 -2
View File
@@ -9,7 +9,7 @@
use Illuminate\Console\Command;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
#[Signature('photos:strip-metadata')]
#[Signature('photos:strip-metadata {--force : Auch bereits bereinigte Fotos erneut bearbeiten}')]
#[Description('Remove metadata (GPS position, camera data) from all stored original photos – new uploads are cleaned automatically')]
class StripPhotoMetadata extends Command
{
@@ -19,7 +19,7 @@ class StripPhotoMetadata extends Command
public function handle(): int
{
$cleaned = Media::query()->where('collection_name', Photo::COLLECTION)->get()
->filter(fn (Media $media): bool => StripImageMetadata::strip($media))
->filter(fn (Media $media): bool => StripImageMetadata::strip($media, (bool) $this->option('force')))
->count();
$this->info("{$cleaned} Original-Foto(s) bereinigt.");
+5 -3
View File
@@ -2,6 +2,7 @@
namespace App\Support;
use Spatie\Image\Enums\ImageDriver;
use Spatie\Image\Image;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Throwable;
@@ -14,9 +15,9 @@ class StripImageMetadata
{
private const TYPES = ['image/jpeg', 'image/png', 'image/webp'];
public static function strip(Media $media): bool
public static function strip(Media $media, bool $force = false): bool
{
if (! in_array($media->mime_type, self::TYPES, true) || $media->getCustomProperty('metadata_stripped')) {
if (! in_array($media->mime_type, self::TYPES, true) || ($media->getCustomProperty('metadata_stripped') && ! $force)) {
return false;
}
@@ -26,7 +27,8 @@ public static function strip(Media $media): bool
}
try {
Image::load($path)->quality(90)->save($path);
// GD on purpose: Imagick (default when installed, e.g. on Strato) keeps the EXIF data when saving.
Image::useImageDriver(ImageDriver::Gd)->loadFile($path)->quality(90)->save($path);
} catch (Throwable $exception) {
report($exception);