Skip to content

Commit

Permalink
Watermarking photos configurable in env setting
Browse files Browse the repository at this point in the history
  • Loading branch information
MGeurts committed Nov 9, 2024
1 parent 60e1659 commit bbdb4ce
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ IMAGE_UPLOAD_MAX_WIDTH=600
IMAGE_UPLOAD_MAX_HEIGHT=800
IMAGE_UPLOAD_QUALITY=80
IMAGE_UPLOAD_TYPE=webp
IMAGE_UPLOAD_ADD_WATERMARK=true
67 changes: 45 additions & 22 deletions app/PersonPhotos.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public static function save(?Person $person = null, $photos = []): void
}

// set image parameters
$image_width = config('app.image_upload_max_width');
$image_height = config('app.image_upload_max_height');
$image_quality = config('app.image_upload_quality');
$image_type = config('app.image_upload_type');
$image_width = config('app.image_upload_max_width');
$image_height = config('app.image_upload_max_height');
$image_quality = config('app.image_upload_quality');
$image_type = config('app.image_upload_type');
$image_add_watermark = config('app.image_upload_add_watermark');

// set image manager
$manager = new ImageManager(new Driver);
Expand All @@ -43,24 +44,46 @@ public static function save(?Person $person = null, $photos = []): void
$next_index = str_pad(strval(++$last_index), 3, '0', STR_PAD_LEFT);
$image_name = $person->id . '_' . $next_index . '_' . now()->format('YmdHis') . '.' . $image_type;

// image: resize, add watermark and save
$manager->read($current_photo)
->scaleDown(width: $image_width, height: $image_height)
->place(public_path('img/watermark.png'), 'bottom-left', 5, 5)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos/' . $person->team_id . '/' . $image_name));

// image : resize width 96px and save
$manager->read($current_photo)
->scaleDown(width: 96)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos-096/' . $person->team_id . '/' . $image_name));

// image : resize width 384px and save
$manager->read($current_photo)
->scaleDown(width: 384)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos-384/' . $person->team_id . '/' . $image_name));
if ($image_add_watermark) {
// image: resize, add watermark and save
$manager->read($current_photo)
->scaleDown(width: $image_width, height: $image_height)
->place(public_path('img/watermark.png'), 'bottom-left', 5, 5)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos/' . $person->team_id . '/' . $image_name));

// image : resize width 96px, add watermark and save
$manager->read($current_photo)
->scaleDown(width: 96)
->place(public_path('img/watermark.png'), 'bottom-left', 5, 5)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos-096/' . $person->team_id . '/' . $image_name));

// image : resize width 384px, add watermark and save
$manager->read($current_photo)
->scaleDown(width: 384)
->place(public_path('img/watermark.png'), 'bottom-left', 5, 5)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos-384/' . $person->team_id . '/' . $image_name));
} else {
// image: resize and save
$manager->read($current_photo)
->scaleDown(width: $image_width, height: $image_height)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos/' . $person->team_id . '/' . $image_name));

// image : resize width 96px and save
$manager->read($current_photo)
->scaleDown(width: 96)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos-096/' . $person->team_id . '/' . $image_name));

// image : resize width 384px and save
$manager->read($current_photo)
->scaleDown(width: 384)
->toWebp(quality: $image_quality)
->save(storage_path('app/public/photos-384/' . $person->team_id . '/' . $image_name));
}

// update person: photo
if (! isset($person->photo)) {
Expand Down
9 changes: 5 additions & 4 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@
'backup_daily_run' => env('BACKUP_DAILY_RUN', '23:00'),
'backup_mail_address' => env('BACKUP_MAIL_ADDRESS', '[email protected]'),

'image_upload_max_width' => (int) env('IMAGE_UPLOAD_MAX_WIDTH', 600),
'image_upload_max_height' => (int) env('IMAGE_UPLOAD_MAX_HEIGHT', 800),
'image_upload_quality' => (int) env('IMAGE_UPLOAD_QUALITY', 80),
'image_upload_type' => env('IMAGE_UPLOAD_TYPE', 'webp'),
'image_upload_max_width' => (int) env('IMAGE_UPLOAD_MAX_WIDTH', 600),
'image_upload_max_height' => (int) env('IMAGE_UPLOAD_MAX_HEIGHT', 800),
'image_upload_quality' => (int) env('IMAGE_UPLOAD_QUALITY', 80),
'image_upload_type' => env('IMAGE_UPLOAD_TYPE', 'webp'),
'image_upload_add_watermark' => env('IMAGE_UPLOAD_ADD_WATERMARK', true),
];

0 comments on commit bbdb4ce

Please sign in to comment.