Thumb() of image outside of content folder not really working

the docs suggest the following snippet for creating thumbnails of images outside of the content folder:

$myimage = new Media(
  // the absolute path for the file
  '/var/www/myimages/image.jpg',         
  // the absolute url
  'http://mydomain.com/images/image.jpg' 
);

echo thumb($myimage, array('width' => 300));

However this is misleading as it doesn’t work as expected.
It seems like using the “thumb()” function like this makes it ignore any configuration you have in your config.php file (like the driver to use…). And for some reason it defaults to the “im” driver (so on systems without imagemagick you get a “sh: 1: convert: not found” error).

I’ve made a quick example with starterkit here: GitHub - rasteiner/kirbythumbs: kirby thumb not working as expected so people can verify it.
Look at the home template.

Since Kirby 2.3 this is intended behavior for the Media class (it is part of the Toolkit and we made the thumb generation independent from the Kirby CMS so that it can be used in other projects).

What you are looking for is the new Asset class:

$myimage = new Asset($filepath);

echo thumb($myimage, array('width' => 300));

$filepath should be a relative path like assets/images/myimage.jpg.


It looks like we forgot to update the docs, I will do so now. Thanks for letting us know.

Does this really work? I’m redesigning an old site right now and it does not work.

I do this:

$image_path = 'D:\projects\sites\vardagsfinans.se\assets\images\avatars\128-15.jpg';
$image_object = new Asset($image_path);
echo thumb($image_object, array('width' => 100));

It displays nothing and shows no errors. When I use var_dump on the $image_object I get this:

Asset Object
(
    [root] => 
    [url] => http://localhost/sites/vardagsfinans.se/D:\projects\sites\vardagsfinans.se\assets\images\avatars\128-15.jpg
    [hash] => d41d8cd98f00b204e9800998ecf8427e
    [dir] => 
    [filename] => 128-15.jpg
    [name] => 128-15
    [safeName] => 128-15.jpg
    [extension] => jpg
    [size] => 
    [niceSize] => 0 kB
    [modified] => 
    [mime] => 
    [type] => 
    [dimensions] => Dimensions Object
        (
            [width] => 0
            [height] => 0
            [ratio] => 0
            [orientation] => 
        )

    [isWritable] => 
    [isReadable] => 
    [isExecutable] => 
    [header] => Content-type: 
    [exif] => Exif Object
        (
            [camera] => 
            [location] => 
            [timestamp] => 
            [exposure] => 
            [aperture] => 
            [iso] => 
            [focalLength] => 
            [isColor] => 
        )

)

The url is messed up as it contains both the url and the path on the same row. The dimensions are set to 0 so I guess it does not read the image file correctly.

I use Kirby 2.5.4.

You path is not correct:

$image_path = 'assets/images/image.jpg';
$image_object = new Asset($image_path);
dump($image_object);
echo thumb($image_object, array('width' => 100));

It works, thanks! :slight_smile:

So the assets class does not support an absolut path as a param.

I get many paths and foreach them with glob, which mean I need to modify the paths afterwards:

// $image_path is generated with glob
$image_path = ‘D:\domain.com\assets\images\image.jpg’;
$image_object = new Asset(str_replace(kirby()->roots()->index(), '', $image_path));
echo thumb($image_object, array('width' => 100));

I feel it’s a bit hacky. Is there a better way to solve it? Maybe add an issue to make it support absolute paths?

You can pass an absolute URL (like, one starting with http(s)://, but not an absolute file path, it seems:

<?php

abstract class AssetAbstract extends Media {
  
  public $kirby = null;  

  use Kirby\Traits\Image;

  public function __construct($path) {
    $this->kirby = kirby::instance();
    if(is_a($path, 'Media')) {
      parent::__construct($path->root(), $path->url());
    } else {
      parent::__construct(
        url::isAbsolute($path) ? null : $this->kirby->roots()->index() . DS . ltrim($path, DS), 
        url::makeAbsolute($path)
      );
    }
  }

}

Ahh, I see. It does not help me with this issue but good to know how it’s ment to be used.

I added an issue about it: https://github.com/getkirby/kirby/issues/599

Thanks!