php-vips icon indicating copy to clipboard operation
php-vips copied to clipboard

Automatic height using Vips::thumbnail()?

Open dzanfardino opened this issue 8 years ago • 1 comments

Hi, I've only recent started using this library in a project.

I'm using the thumbnail method, which is incredibly fast, however, I haven't found a way of generating a thumbnail with a given width, and and an automatically generated height.

As a workaround I'm doing:

      $originalImage = Vips::newFromFile($tempFullPath . '.' . $imageFormat);
      $aspectRatio = $originalImage->width / $originalImage->height;
      $height = 250 / $aspectRatio;
      $image = Vips::thumbnail($tempFullPath . '.' . $imageFormat, 250, ['height' => $height]);
      $image->writeToFile($tempFullPath . '_thumb.jpg');

But surely there's a better way isn't there ??

dzanfardino avatar Sep 07 '17 13:09 dzanfardino

Hello, you can just give a large value for height, and then size will be constrained by the width. Or vice-versa, of course.

@kleisauke is proposing adding something to make this more obvious, but it's not gone in yet.

You can see the logic the command-line vipsthumbnail program uses to parse geometry strings here:

https://github.com/jcupitt/libvips/blob/master/tools/vipsthumbnail.c#L303

jcupitt avatar Sep 07 '17 14:09 jcupitt