johannschopplich / kirby-blurry-placeholder

🖼 Blurry image placeholders for better UX
MIT License
65 stars 3 forks source link

Division by zero Error (image inside Structure Field) #16

Closed manuel-fleig closed 2 years ago

manuel-fleig commented 2 years ago

Hey, I´m getting the following error when using the plugin for images inside a structure field:

Whoops \ Exception \ ErrorException (E_WARNING):
Division by zero

it's in this line of site/plugins/kirby-blurry-placeholder/classes/KirbyExtended/BlurryPlaceholder.php:

$height = sqrt($pixelTarget / ($ratio ?? $file->ratio()));

What might be wrong here?

johannschopplich commented 2 years ago

Are you sure your image is actually an image? Doesn't seem that way. The ratio() method will work on images solely.

Could you provide a minimal reproducible example?

manuel-fleig commented 2 years ago

Yep, sure:

<?php $items = $page->projects()->toStructure()->shuffle()?>
    <?php foreach ($items as $item): ?>
    <div>
        <img src="<?= $item->project()->toFile()->placeholderUri() ?>"
            data-src="<?= $item->project()->toFile()->url() ?>" data-lazyload
            alt="<?= $item->project()->toFile()->alt() ?>" />
    </div>
    <?php endforeach ?>

it works perfectly on all other pages, but here it crashes.

johannschopplich commented 2 years ago

Could you implement a safeguard?

<?php foreach ($items as $item): ?>
  <?php if (!$item->project()->toFile()) continue ?>

Actually, I postulate that it's not the plugin, but an error in the data structure.

manuel-fleig commented 2 years ago

Found the mistake, haha. You were right in the first place. There was a PDF between the images i didn't notice.

Thanks for the quick Support @johannschopplich , i really love your plugin :)

johannschopplich commented 2 years ago

Thanks, glad to hear you're enjoying the plugin and also found the bug! 🙂