Jump to content

Page Image Manipulator | API for 1 & 2


horst

Recommended Posts

@horst, an excellent module for ProcessWire, I'm glad that I now have the opportunity to use it in a practical setting.

Though, I have encountered some issues, and though I have found workarounds, I thought I'd still make them known here, in case you did not know.

So, I've been creating a system that hooks after the pageSave event, applies a watermark to images in a specific field, and overwrite the original file with it. Along the way, I've noticed the following problems:

getPimVariations() does not include any images created and saved with Pim2 unless I reload the page:

$pim = $image->pim2Load('watermark');
print $pim->getPimVariations()->count; // prints '0', expected '0'

$pim->watermarkLogo($somePageimage, 'center', 5)->pimSave();

print $pim->getPimVariations()->count; // prints '0', expected '1'

This also happens:

// Call pim on image with existing pim variations
$pim = $image->pim2Load();

print $pim->getPimVariations()->count; // prints '1', expected '1'

$pim->removePimVariations();

print $pim->getPimVariations()->count; // prints '1', expected '0'

 

I tried to get around the above error by calling pim2Load() on the image again, after calling pimSave(), to see if that'd work, and it caused some kind of infinite loop, or whatever - I had to restart the severs:

$pim = $image->pim2Load();

print $pim->getPimVariations()->count; // prints '0', expected '0'

$pim->watermarkLogo($watermark, 'center', 5)->pimSave();

$pim = $image->pim2Load(); // Causes max_execution_time timeout

print $pim->getPimVariations()->count; // doesn't execute, expected '1'

 

Am I doing something wrong? If I am, I think it may be prudent for you to make clear the limitations of your module.

Link to comment
Share on other sites

Hi @CaelanStewart,

thanks for trying out and debug the PIM.

First thing, - in your code example above, you only have specified the mandatory $prefix in the first call of ->pim2load(). The other calls are completly empty, what may lead to the max_execution_time timeout. Please check, if you really call it without defining a prefix in your code at the webserver, or if you only forgott it in the example above. (?)

With the issue you find out about the cached variations array, please try to comment out the line 796 in ImageManipulator02.class.php and see, if this makes a difference, please.

Link to comment
Share on other sites

@horst, hi there.

I wrote the code I posted specifically for this thread - the prefix is present in the actual code.

Perhaps you could add an option to disable the caching of variations, or perhaps add a parameter to the getPimVariations() method that ignores the cache, like so?

$pim = $page->image->pim2Load('some-prefix');

$pim->getPimVariations(true); // Passing true skips the cache

 

  • Like 1
Link to comment
Share on other sites

@CaelanStewart: I have added it, but changed the default! So, everytime now you get a refreshed version. I think that way round it is more like the common expectations. Only if one have a more advanced usecase, he may be aware of the switch and has a need for it.

https://github.com/horst-n/PageImageManipulator/commit/ef8a6af6acc49e31d39fa63d6faa696428e30f83

 

  • Like 1
Link to comment
Share on other sites

1 minute ago, horst said:

@CaelanStewart: I have added it, but changed the default! So, everytime now you get a refreshed version. I think that way round it is more like the common expectations. Only if one have a more advanced usecase, he may be aware of the swotch and has a need for it.

https://github.com/horst-n/PageImageManipulator/commit/ef8a6af6acc49e31d39fa63d6faa696428e30f83

 

Wow, thanks! Good to see active and responsive development! :)

Though, unfortunately, I had avoided the need to get the variations. At least the next guy won't have an issue! :P

1 minute ago, horst said:

Holy ghost, @CaelanStewart, it feeled that you have liked my post just before I had posted it!?! :ph34r::lol::ph34r:

Yes, I'm secretly spying on everything you do! I knew!

  • Like 1
Link to comment
Share on other sites

@horst, I thought I'd share my implementation of an auto-watermarking module. This module depends on PIM, and also depends on some other stuff specific to the application the module was created for.

If anybody wants to use this module for their own uses, you'll have to change the names of fields and templates that you want to apply a watermark to.

We (PROGRESS: A Creative Agency) do plan to modify this module for more general use in future, and release it in the module directory. But we are really busy right now, and cannot do so.

See the GitHub gist here.

  • Like 3
Link to comment
Share on other sites

  • 4 weeks later...

Nice module, I am using it to add canvas to some images so they are all the same size. On the transparent regions they are being changed to black. Is there any way to stop this other than re-uploading a flattened image?

$options = array(
  'quality' => 100,
  'upscaling' => false,
);
$p->image_logo->pim2Load('logo')->setOptions($options)->width(180)->canvas(180, 180)->pimSave()->url

 

Link to comment
Share on other sites

It looks like some PNG files with transparency, the areas which are transparent are changed to black. I will do some more testing when I can. They might be 8 bit PNGs with matte set which could be confusing things. I was just wondering if this was a common occurrence. As it isn't I will dig some more.

Link to comment
Share on other sites

it uses GD-lib by default, but you also can enable the module for IMagick.

We inspect the images at first and when using GD-lib, we use different ways for modifying the images. Per default, we want to use all functions that are needed to produce the best possible quality. But for those known image formats, like png8 with transparency, we ommit some steps. (Those that would include GD-lib methods that are known to produce that black background instead keeping the transparency)

Link to comment
Share on other sites

I'm busy putting together my blog, and need to crop post feature images. I'm using Twig for my templates, and I have this code at the moment:

{# cropping is true by default, centers by default #}
{# upscaling is true by default #}
{% set image = post.post_image.pim2Load('cropped', true).setOptions({
    'quality': 80,
    'sharpening': 'none',
}).resize(700,300).smooth().pimSave() %}

The resulting image is not cropped to center as I'd expect it to. I could well do this with the built-in tools (as mentioned in your previous post about this), however this results in a severe loss of quality on my end, even if core quality property is set to 100 -- I assume this is a GD issue.

Can anything be done to implement auto-cropping? PIM is pretty much the only thing I can find that retains image quality. Thanks!

 

Update: I've decided to go ahead and use AvbImage - always been a fan of Intervention\Image, and it works quite well here. That said, this does still appear to be a bug to me, especially if the croppable property is present.

Link to comment
Share on other sites

For simple cropping I use Pia. No need to invoke Pim. Pia has the advantage that she lets you very simple define what you need and she uses all the available ImagerenderingEngines. She isn't bound to GD-lib only, as she only delegates your wishes to the core image engine. :)

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

This module is not working for me on PW 3.0.58. I'm trying to convert PNG to JPG. When I do 

$img->pim2Load('template')->setOptions($options)->width($img->width())->pimSave()

I get a fatal error: 

Quote

Exception: Method Pageimage::pim2Load does not exist or is not callable in this context

When I use pimLoad instead of pim2Load, there's no error and JPG's are generated. But since I'm on PW 3, I should use pim2Load. I'm calling the function in one of my templates. Any idea as to why pim2Load would not be working?

Link to comment
Share on other sites

On 8-4-2017 at 9:49 AM, horst said:

You need to manually install pim2 under site/modules/, and UN-install pim. Than you can use pim2load.

You can read about it in the very first post of this thread: https://processwire.com/talk/topic/4264-page-image-manipulator-1/  right at the top of that post.

 

I read that post but I was confused by it. But now understand: I need to open the site/modules/PageImageManipulator folder that was downloaded by PW and remove the module files that are not PageImageManipulator02 and then install it. Thanks for the module!

Link to comment
Share on other sites

  • 2 weeks later...

This is really great and can't wait to try it out.  Is it possible to adjust the watermark transparency, or rather let users adjust the watermark transparency?  Reason is, I have found that sometimes, depending on the photo, the watermark can be really hard to see, or too prominent.  Being able to adjust the opacity would be great. 

Thanks,

Ray

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...