Media files not getting generated

Is there a reason why Kirby would suddenly stop processing images? its not just one project this is happening on. Im running Herd, PHP 8.2, and Kirby 4.5. All im getting is jobs files now. How do i debug it? i resintalled imagemagic and its properly configered in the config file. im at a loss :frowning:

Does this only happen with the im driver?

Seems to happen with both IM and GD. Using ->toFile()->url() works (which i dont think does any actual processing) work but i get nothing when i try to use thumb() to convert the image to webp (or any format different to the source files format)

It’s when you call url() that the files get actually put into the media folder.

Ok so it must be something im doing in the code because this works

<img src="<?= $page->image('filename.jpg')->thumb(
    [
        'width'     => 300,
        'height'    => 300,
        'format'    => 'webp',
    ]
)->url() ?>" alt="">

Am i right in thinking that thumb() has toFile() built in to it?

No, $page->image('someimage') is a page method and returns a File object if the image exists (or null if it doesn’t); then when you call thumb() on this File object (should always make sure you actually have a File object before doing so), you get a FileVersion object in return.

toFile() is a field method that tries to convert a field value to a File object. It either returns a File object or null.

Ok so the doc ecample here Image thumbnails | Kirby CMS

That assumes you have already done ->toFile() in whatever $image is?

Might be worth improving that code ecample in the docs to clarify that if thats the case.

No, it assumes that $image is a file object, no matter how you got it.

Ah yes i see. Sorry if the questions sound daft, i dont actually use thumbs all that often.

No problem, after all, it might be helpful for others as well.

This is by the way what you get if you ask ChatGPT to explain the code snippet above and ask it why the code might fail as is, it goes to quite some length:

Summary

This code is part of a Kirby CMS template. It dynamically generates an HTML <img> tag by retrieving an image file, resizing it, and converting it to the WebP format before outputting its URL as the src attribute. Let’s break down the steps in detail, explain what kind of object each step returns, and also discuss possible reasons for failure.

Code Breakdown:

<img src="<?= $page->image('filename.jpg')->thumb(
    [
        'width'     => 300,
        'height'    => 300,
        'format'    => 'webp',
    ]
)->url() ?>" alt="">

1. $page->image('filename.jpg')

  • Object: $page is an instance of the Page object in Kirby CMS. The image('filename.jpg') method is used to retrieve a File object that corresponds to the image file with the filename filename.jpg, associated with the current page.
  • Returns: A File object. If the image with the filename filename.jpg exists for the current page, this method returns the corresponding File object; otherwise, it returns null.
  • Possible Failure: If the image does not exist or the file path is incorrect, this will return null, and the rest of the code will fail (because you cannot call thumb() on null).

2. ->thumb([...])

  • Object: The thumb() method is called on the File object returned by $page->image(). The thumb() method creates a Thumb object, which is essentially a resized version of the original image.
  • Parameters: The method accepts an array of options:
    • 'width' => 300: Resizes the image to 300 pixels wide.
    • 'height' => 300: Resizes the image to 300 pixels high.
    • 'format' => 'webp': Converts the image to the WebP format.
  • Returns: A Thumb object that contains the resized image in WebP format. This object provides methods for getting the URL of the resized image, among other things.
  • Possible Failure: If the File object does not exist (i.e., the image was not found), this step will fail, and the method chain will not work.

3. ->url()

  • Object: This method is called on the Thumb object returned by the thumb() method. The url() method generates the URL for the resized (and possibly format-converted) image.
  • Returns: A string containing the URL of the resized image. This URL is used as the value for the src attribute of the <img> tag.
  • Possible Failure: If the thumb() method fails (e.g., due to an invalid image file or incorrect dimensions), calling url() on null will result in an error.

4. <?= ... ?>

  • This is the short form of PHP’s echo statement: <?= ... ?> is equivalent to <?php echo ... ?>. It outputs the result of the expression inside the PHP tags. In this case, the url() method returns a string (the URL of the image), which is then echoed into the src attribute of the <img> tag.

Final HTML Output:

If everything works correctly and the image exists, this PHP code generates an HTML <img> tag similar to:

<img src="https://example.com/path/to/thumbs/filename.jpg?width=300&height=300&format=webp" alt="">

This <img> tag will load the resized WebP version of the image.


Potential Issues and Why the Code Might Fail:

  1. Image Not Found (File Missing):

    • If $page->image('filename.jpg') does not find an image with the specified filename (filename.jpg) for the current page, it returns null. Since null cannot be used to call the thumb() method, the entire chain will fail, and no image will be output. To handle this, you can check if the image exists before calling thumb():
      $image = $page->image('filename.jpg');
      if ($image) {
          echo '<img src="' . $image->thumb([...])->url() . '" alt="">';
      }
      
  2. Incorrect File Format or Unsupported Conversion:

    • If Kirby is unable to convert the image to WebP (perhaps because the necessary libraries for WebP are not available on the server), the thumb() method may fail. While Kirby typically handles image formats well, it’s still a possibility.
  3. Invalid Parameters in thumb():

    • If the thumb() method is provided with invalid parameters (for example, an unsupported image size or format), it might not generate the thumbnail correctly. Kirby usually handles such cases by throwing an exception or returning null. It’s important to check that the parameters are valid and supported.
  4. Missing WebP Support on Server:

    • WebP support may not be enabled on the server or in the environment where the site is hosted. If this is the case, converting images to WebP might not work, and you would need to either ensure WebP support is available or fall back to another format (like JPEG or PNG).
  5. Permissions or File Access Issues:

    • The image or the cache directory where the thumbnails are stored may not have the correct permissions, causing the image processing to fail.

Conclusion:

This code is intended to display a resized WebP image dynamically using Kirby CMS’s image handling methods. It works well when the image exists and all required libraries are in place, but it can fail if the image is missing, the file format is unsupported, or server issues arise (like lack of WebP support). Proper error handling or fallback mechanisms should be implemented to prevent failures from disrupting the page display.

1 Like

Wow, oh boy did it! Thats actually quite impressive.