Thumb component registry set

I have registered a thumb component where I intend to manipulate the crop function.

The crop function is dependent on the dimensions class and all the output of every function is stored there as well. Because I want to make the crop outside of the dimensions class I need some way to still return the data to the dimensions object.

I have a code snippet below where I try to show some things.

  1. I commented what $dimensions returns, a Dimension object
  2. I made a custom array. If it’s possible to convert somehow and return it to the dimensions object, it’s good enough.
  3. I have a comment about what I have in mind for my final output. (may not be that important)
  4. I have a comment about the crop conversion (may not be that important).

(I also have another file to register this function as a plugin)

use Thumb as Generator;

class MyThumbClass extends Kirby\Component\Thumb {
    protected function dimensions(Generator $thumb) {
        
            $dimensions = clone $thumb->source->dimensions();
        
            if(isset($thumb->options['crop'])) {
                if($thumb->options['crop'] === true) {
                    $dimensions->crop(a::get($thumb->options, 'width'), a::get($thumb->options, 'height'));

                    print_r($dimensions);

                    /*
                      Dimensions Object
                        (
                            [width] => 400
                            [height] => 400
                            [ratio] => 1
                            [orientation] => square
                        )
                    */

                    $array = [
                        'width' => 300,
                        'height' => 300,
                        'ratio' => 1,
                        'orentation' => 'square'
                    ];

                    // Convert to Dimensions Object
                }
            } else {
              $dimensions->resize(a::get($thumb->options, 'width'), a::get($thumb->options, 'height'), a::get($thumb->options, 'upscale'));
            }

            /*
            echo thumb($image, array('width' => 400, 'height' => 250, 'crop' => 'top left'));
            */

            /*
            $image = new abeautifulsite\SimpleImage( $image_or_thumb_path );
$image->crop(0, 0, 580, 280);
$image->save( $new_thumb_path );
            */
        
            return $dimensions;
        
          }
  }

I think I was messing in the wrong function. It seems like create is a better way in.

Out of topic but to get a new image url for a new Asset is not super simple…

$asset = new Asset($thumb->result); // Inside create
$url = str_replace([kirby()->roots()->index() . DS, DS], ['', '/'], $asset->root());
echo $url; // thumbs/projects/project-a/creative-tools-400x400.jpg
$test = new Asset($url);
print_r($test); // Works!

I have added an example to your GitHub issue.

But your example is a bit different: You are trying to get a new Asset object from another one. Why that? Can’t you just use the existing one?

1 Like

Thanks! :slight_smile:

You are right, but I have a better example of what I did (read more below):

It’s not the exact same url I’m using. I change the extension from jpg to png in this case.

Your code isn’t stable as you define $asset inside of an if clause. Afterwards you return it, even if the condition was false.

But to answer your question, what about something like this (untested):

$output_url = dirname($asset->url()) . '/' . $asset->name() . '.jpg';
$media = new Media($output_path, $output_url);
$asset = new Asset($media);

I’ll try it out on monday. Thanks! :slight_smile:

About the return of $asset, if it’s false it will return the $asset object that was sent to the function. It should be stable.

You are right, I didn’t check if the variable was defined earlier as I have seen this pattern of defining variables in an if clause too often already. :slight_smile:

As I’ve run into this again, I’ve added a new reply to the issue: https://github.com/getkirby/kirby/issues/599#issuecomment-328109415

In short it can be helpful with a new function:

There is probably a better word for the function than thumbnail.

function thumbnail($filepath, $url) {
    $media = new Media($filepath, $url);
    $asset = new Asset($media);
    return $asset;
}

It’s very easy to resize an image from any location:

echo thumbnail($filepath, $url)->resize(500)->url();