libvips/php-vips

gifload: no property named dpi

ethaniel opened this issue · 4 comments

Hello and thank you for the amazing binding.

I am trying to make a "universal" loader which accepts all kinds of images, but for example I would like to set those that have a property named dpi to a value of 300.

If I add an option called dpi to the Vips\Image::newFromFile options, and I am loading a normal gif image, I get a gifload: no property named dpi error.

I already use Vips\Image::findLoad to detect the kind of image I'm loading before I set the options. But I was wondering if it's possible to check if a dpi option exists for a specific loader (so I wouldn't have to hardcode the option exceptions for gif/jpg/etc)?

Hi, it's not too well documented, but you can use Introspect to find the arguments for operations:

https://libvips.github.io/php-vips/classes/Jcupitt-Vips-Introspect.html

The source might be helpful:

https://github.com/libvips/php-vips/blob/master/src/Introspect.php

It's used here (might help too):

https://github.com/libvips/php-vips/blob/master/src/Introspect.php

Are you trying to detect things like memory bombs? You can do this after opening, something like:

$image = Vips\Image::newFromFile($filename);
if ($image->width * $image->height * $image->bands > $size_threshold) {
    error("image too big!!");
}

newFromFile will read the image metadata, but won't do any decoding (that happens the first time you read pixel values), so it can't trigger out of memory.

In some cases, my users upload PDFs or TIFFs. I want these files to be loaded with a dpi of 300. So I want to know beforehand, if I should apply the dpi option to the loader or not. Thank you for the Introspect, I will look into it.

Ah OK. I would sniff the type and then have a special path for pdf, eg. (untested):

$loader = Vips\Image::findLoad($filename);
if ($loader == "VipsForeignLoadPdfFile") {
    $image = Vips/Image::newFromFile($filename, ["dpi" => 300]);
}
else {
    $image = Vips/Image::newFromFile($filename);
}