JamesHeinrich / phpThumb

phpThumb() - The PHP thumbnail generator
Other
315 stars 97 forks source link

Support for imagecropauto #158

Open hempalex opened 4 years ago

hempalex commented 4 years ago

How can i use imagecropauto function for removing white/black/transparent borders before thumbnail creation. I can't see any "prefilter" interfaces which I could use.

JamesHeinrich commented 4 years ago

I have added a new parameter "ica" in https://github.com/JamesHeinrich/phpThumb/commit/1766fa7e83810193c34208c1b009d2f6a69d7402 The value can be an integer 0-4 for the first 4 modes of imagecropauto, or 5|<threshold>|<hexcolor> where threshold is a value between 0-1 and hexcolor is a 6-char hexadecimal color string.

hempalex commented 4 years ago

Thank you!

Is it possible to implement my own "pre-filters" without patching core of the library? I'd like to implement more sophisticated auto cropping involving several calls to imagecropauto with different params depending on image content.

JamesHeinrich commented 4 years ago

I'm not opposed to that, I'm just trying to think of a good way to make that work. If you have any implementation suggestions I'm interested to hear them. I assume you'd be working in object mode rather than URL parameters?

One possibility to consider with the existing code is to use setSourceData instead of setSourceFilename, which would let you pre-process how you wish and then pass in the pre-processed image data. Or, of course, you could pre-process and write to disk and proceed with setSourceFilename, wouldn't be a big difference between the two approached (and both are not ideal, I understand that).

If you have suggestions for how to implement the possibility of passing in arbitrary pre-filtering code without causing security concerns I'm interested in your suggested approach.

hempalex commented 4 years ago

I can preprocess image myself, but it causes a lot of boilerplate code, like choosing function to read image from jpg/png/gif but the best code is not written code ))

In this particular case I'd suggest to use a similar to fltr[] approach, to allow to pass multiple cropping attempts. Now it's not possible to crop transparent border and white borders with the same code. But thumbnails are generated from many images, and it's not possible to know which cropping algo would be the best.

But in general I'd like to use some prefilters - like

$phpThumb->addPrefilter(function($gdImage) {   
    $cropped = imagecropauto($gdimage, IMG_CROP_TRANSPARENT);
    if ($cropped === false)  return $gdImage;

   $cropped =  imagecropauto($gdimage , IMG_CROP_THRESHOLD, 0.33, 0xFFFFFF);
    if ($cropped === false)  return $gdImage;

  return $cropped;
} );

and it could be the same with "postfilters", also you could recognise special keywords and implement built-in filters into pipeline.

But it can turn out to complete different library ))

JamesHeinrich commented 4 years ago

Would you be interested in writing said addPreFilter (and addPostFilter) changes for phpThumb.class and submitting a pull request?

arturmamedov commented 4 years ago

Are this repo elligible for hacktoberfest?