Get SimpleImage file url correctly

Hi
I’m re-opening my thread about simpleImage (finnaly changing my mind) and would like to know how to output an image file created by the class.
I’m using :

$image = $img->url();
if($image) {
  $img = new SimpleImage($image);
  $img = $img->resize(500)->pixelate(20)->toFile($page->root() . '/' . $gal->image()->name() . '-overlay.jpg', 'image/png');
}

and get

The problems are

  • The script is loading on every page load, slowing down the page.
  • I don’t know how to get the url() of the image from the folder

If you need more options of the SimpleImage class than Kirby offers by default, you can create your own custom thumbs driver.

https://k2.getkirby.com/docs/developer-guide/toolkit/thumb-driver

1 Like

Okay, this is more complex than I thought…
I added the /site/plugins/mythumbdriver/mythumbdriver.php file and paste the SimpleImage class into, and wrote c::set('thumbs.driver', 'mythumbdriver');

So my php file is like : `

<?php
thumb::$drivers['mythumbdriver'] = function($thumb) {

namespace claviska;

class SimpleImage {
.....

I get the error unexpected 'claviska' , and don’t know if I have to put the SimpeleImage class into the file or something else

Na, not like that. Check out the example here: https://github.com/flokosiol/kirby-focus/blob/kirby-2/drivers/gd.php

Thank you !!!
I used this file and added

if ($thumb->options['pixelate']) {
      $img->pixelate($thumb->options['pixelate']);
    }

This works fine !
Questions :

  • I have the feeling that the thumbs are generated on each page reload, the page is much slower, is that possible ?
  • Don’t you think the pixelate() could be implemented by default ?

are there docs on the thumb driver for v3?

Couldn’t find any (so need to add them), but the K3 version of the above mentioned plugin has an example.

GD: https://github.com/flokosiol/kirby-focus/blob/master/core/Focus/GdLib.php
IM: https://github.com/flokosiol/kirby-focus/blob/master/core/Focus/ImageMagick.php

Sorry to bother you, but I have noticed that my images from the panel are not rendered as thumbs and I don’t see saved files into the thumbs folder, I have set this in my file :

<?php

thumb::$drivers['mythumbdriver'] = function($thumb) {
  try {
    $img = new abeautifulsite\SimpleImage($thumb->root());
    $img->quality = $thumb->options['quality'];

    if (isset($thumb->options['focus']) && isset($thumb->options['fit']) && isset($thumb->options['ratio']) && isset($thumb->options['focusX']) && isset($thumb->options['focusY'])) {
      
      // calculate crop coordinates and width/height for the original image
      $focusCropValues = focus::cropValues($thumb);

      // crop original image with thumb ratio and resize it to thumb dimensions
      $img->crop($focusCropValues['x1'], $focusCropValues['y1'], $focusCropValues['x2'], $focusCropValues['y2'])->thumbnail($thumb->options['width'], $thumb->options['height']);
    }
    else if ($thumb->options['crop']) {
      @$img->thumbnail($thumb->options['width'], $thumb->options['height']);
    }
    else {
      $dimensions = clone $thumb->source->dimensions();
      $dimensions->fitWidthAndHeight($thumb->options['width'], $thumb->options['height'], $thumb->options['upscale']);
      @$img->resize($dimensions->width(), $dimensions->height());
    }

    if ($thumb->options['grayscale']) {
      $img->desaturate();
    }
    
    if ($thumb->options['width']) {
      $dimensions = clone $thumb->source->dimensions();
      $dimensions->fitWidthAndHeight($thumb->options['width'], $thumb->options['height'], $thumb->options['upscale']);
      @$img->resize($dimensions->width(), $dimensions->height());
    }
    
    if ($thumb->options['pixelate']) {
      $img->pixelate($thumb->options['pixelate']);
    }

    if ($thumb->options['blur']) {
      $img->blur('gaussian', $thumb->options['blurpx']);
    }

    if ($thumb->options['autoOrient']) {
      $img->auto_orient();
    }    

    @$img->save($thumb->destination->root);
  } catch(Exception $e) {
    $thumb->error = $e;
  }
};

// keep backwards compatibility
thumb::$drivers['focus'] = thumb::$drivers['gd'];

Well, the focus stuff only makes sense in the context of the focus crop plugin, so you should leave this out. As I said, it’s an example of how to extend the driver, and you should use the default thumbs driver as a basis…

1 Like