Skip to content

Commit

Permalink
Creating small thumbnail preview on request if it doesnt already exist
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Fey <info@sebastianfey.de>
  • Loading branch information
seyfeb committed Dec 6, 2020
1 parent 1d5ac91 commit 4b0a841
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/Service/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,37 @@ public function getRecipeImageFileByFolderId($id, $size = 'thumb') {
$image_file = null;
$image_filename = $size . '.jpg';

if (($size == 'thumb16' || $size == 'thumb') && !$recipe_folder->nodeExists($image_filename)) {

if ($recipe_folder->nodeExists('full.jpg')) {
// Write the thumbnail
$recipe_full_image_file = $recipe_folder->get('full.jpg');
$full_image_data = $recipe_full_image_file->getContent();
$thumb_image = new Image();
$thumb_image->loadFromData($full_image_data);
$thumb_image->fixOrientation();
$thumb_image->resize(256);
$thumb_image->centerCrop();

try {
$thumb_image_file = $recipe_folder->get('thumb.jpg');
} catch (NotFoundException $e) {
$thumb_image_file = $recipe_folder->newFile('thumb.jpg');
}

$thumb_image_file->putContent($thumb_image->data());

// Create low-resolution thumbnail preview
$low_res_thumb_image = $thumb_image->resizeCopy(16);
try {
$low_res_thumb_image_file = $recipe_folder->get('thumb16.jpg');
} catch (NotFoundException $e) {
$low_res_thumb_image_file = $recipe_folder->newFile('thumb16.jpg');
}
$low_res_thumb_image_file->putContent($low_res_thumb_image->data());
}
}

$image_file = $recipe_folder->get($image_filename);

if ($image_file && $this->isImage($image_file)) {
Expand Down

0 comments on commit 4b0a841

Please sign in to comment.