horst

Module
Page Image Manipulator 1

225 posts in this topic

@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.

Share this post


Link to post
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.

Share this post


Link to post
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

 

1 person likes this

Share this post


Link to post
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

 

1 person likes this

Share this post


Link to post
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!

1 person likes this

Share this post


Link to post
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.

3 people like this

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

More infos please! which transparent regions? what source format? which outputformat? how do you have defined the background?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Ah, ok, 8bit transparent pngs are not handled correctly by the GD-library!

There is no chance. Only solution is to use 24bit PNGs with transparency.

2 people like this

Share this post


Link to post
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)

Share this post


Link to post
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.

Share this post


Link to post
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. :)

2 people like this

Share this post


Link to post
Share on other sites

@horst - I knew there was another one, and I have seen this before! Haha! Anyways, I've already implemented AvbImage, and so I'll stick to that for my blog. I'll be sure to give Pia a try on one of my upcoming sites that doesn't need many manipulation features. ?

1 person likes this

Share this post


Link to post
Share on other sites

Sorry, AFAIK, GD doesn't support TIF.

1 person likes this

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Ok, no, that wasn't exactly what I suggested. But if this works too, it is a good alternative. :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

@rayorg: currently only the method ->watermarkText() has a param opacity (0-100%). For overlaying images, there isn't a configurable option currently.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By matjazp
      Module: Auto Smush PDF
      https://github.com/matjazpotocnik/AutoSmushPDF 
      Compress PDF files automatically on upload,  manually by clicking the link for each file and in bulk mode for all PDF files.
      In Automatic mode PDF files that are uploaded are automatically compressed.
      In Manual mode "Compress" link will be present. This allows manual compression of the individual PDF file.
      In Bulk mode all PDF files can be compressed in one click. Will process PDF files sitewide, use with caution! 
      Using https://labstack.com/, free (at the moment) online web service that provides compressing of PDF files. There is no limit in file size and no limit on number of uploaded PDF's. No privacy policy available. 
       
      EDIT April 30 2017: This module is not working anymore as Labstack removed support for pdf compression.
    • By abdus
      After this tutorial you'll have learned how to:
      Build a Process module Make an AJAX request to backend Serve JSON as response Let's say you want to display the latest orders in a dashboard that you can access from admin panel. And you want it to refresh its content with a button click. Most straightforward and proper way (that I know of) is to create a Process module, as they're built for this purpose.
      First, create a directory under /site/modules/, call it ProcessDashboard, and create a file named ProcessDashboard.module under that directory. Following is about the least amount of code you need to create a Process module.
      <?php namespace ProcessWire; class ProcessDashboard extends Process implements Module { public static function getModuleInfo() { return [ 'title' => 'Orders Dashboard', 'summary' => 'Shows latest orders', 'version' => '0.0.1', 'author' => 'abdus', 'autoload' => true, // to automatically create process page 'page' => [ 'name' => 'order-dashboard', 'title' => 'Orders', 'template' => 'admin' ] ]; } public function install() { parent::install(); // to create process page } public function uninstall() { parent::uninstall(); // to the remove process page when uninstalling } public function ___execute() { return 'hello'; } } Once you refresh module cache from Modules > Refresh, you'll see your module. Install it.

      It will create an admin page under admin (/processwire/) and will show up as a new item in top menu, and when you click on it, it will show the markup we've built in execute() function.

       
      All right, now let's make it do something useful. Let's add create a data list to display latest orders. We'll change execute() function to render a data table.
      public function ___execute() { /* @var $table MarkupAdminDataTable */ $table = $this->modules->MarkupAdminDataTable; $table->setID($this->className . 'Table'); // "#ProcessDashboardTable" $table->headerRow([ 'Product', 'Date', 'Total' ]); // fill the table foreach ($this->getLatest(10) as $order) { $table->row([ $order['title'], $order['date'], $order['total'] ]); } // to refresh items $refreshButton = $this->modules->InputfieldSubmit; $refreshButton->name = 'refresh'; $refreshButton->id = $this->className . 'Refresh'; // "#ProcessDashboardRefresh" $refreshButton->value = 'Refresh'; // label of the button return $table->render() . $refreshButton->render(); } where getLatest() function finds and returns the latest orders (with only title, date and total fields)
      protected function getLatest($limit = 5, $start = 0) { // find last $limit orders, starting from $start $orders = $this->pages->find("template=order, sort=-created, limit=$limit, start=$start"); // Only return what's necessary return $orders->explode(function ($order) { return [ 'title' => $order->title, 'date' => date('Y-m-d h:i:s', $order->created), 'total' => $order->total ]; }); } When you refresh the page, you should see a table like this

      Now we'll make that Refresh button work. When the button is clicked, it will make an AJAX request to ./latest endpoint, which will return a JSON of latest orders. We need some JS to make AJAX request and render new values. Create a JS file ./assets/dashboard.js inside the module directory.
      window.addEventListener('DOMContentLoaded', function () { let refresh = document.querySelector('#ProcessDashboardRefresh'); let table = document.querySelector('#ProcessDashboardTable'); refresh.addEventListener('click', function (e) { // https://developer.mozilla.org/en/docs/Web/API/Event/preventDefault e.preventDefault(); // Send a GET request to ./latest // http://api.jquery.com/jquery.getjson/ $.getJSON('./latest', { limit: 10 }, function (data) { // check if data is how we want it // if (data.length) {} etc // it's good to go, update the table updateTable(data); }); }); function renderRow(row) { return `<tr> <td>${row.title}</td> <td>${row.date}</td> <td>${row.total}</td> </tr>`; } function updateTable(rows) { table.tBodies[0].innerHTML = rows.map(renderRow).join(''); } }); And we'll add this to list of JS that runs on backend inside init() function
      public function init() { $scriptUrl = $this->urls->$this . 'assets/dashboard.js'; $this->config->scripts->add($scriptUrl); } Requests to ./latest will be handled by ___executeLatest() function inside the module, just creating the function is enough, PW will do the routing. Here you should notice how we're getting query parameters that are sent with the request.
      // handles ./latest endpoint public function ___executeLatest() { // get limit from request, if not provided, default to 10 $limit = $this->sanitizer->int($this->input->get->limit) ?? 10; return json_encode($this->getRandom($limit)); } Here getRandom() returns random orders to make it look like there's new orders coming in. 
      protected function getRandom($limit = 5) { $orders = $this->pages->find("template=order, sort=random, limit=$limit"); return $orders->explode(function ($order) { return [ 'title' => $order->title, 'date' => date('Y-m-d h:i:s', $order->created), 'total' => $order->total ]; }); } And we're done. When refresh button is clicked, the table is refreshed with new data.
      Here it is in action: 
      2017-04-29_19-01-40.mp4 (227KB MP4, 0m4sec)
      Here's the source code:
      https://gist.github.com/abdusco/2bb649cd2fc181734a132b0e660f64a2
    • By justb3a
      Little admin helper module: Using this module you can view all template cache settings at once. E.g. cache status, cache time (configurable). Furthermore it adds the functionality to clear the entire template cache or just the template cache for a given template. Note that this may cause a temporary delay for one or more requests while pages are re-cached.

      GitHub: https://github.com/justb3a/processwire-templatecacheoverview
    • By blad
       
      Hi guys!
      I'm creating a module to manage, edit, publish ... pages. It is very similar to ProcessBatcher.
       
       
      Changelog:
      In progress 
      Features:



      Select pages with your mouse like a desktop enviroment. If you press "edit" the selected pages will appear in a lightbox.


      Drag pages to do actions.
      Any feedback is welcome
       
       
    • By abdus
      I dont know how they work internally, whether they depend on each other, but I'd love to be able to test and buy RepeaterMatrix or Multiplier or Table (or any other) module individually. Whole pack of ProFields modules seems awesome to have, however, being able to buy one for ~$30 --instead of all for $130-- sounds much more affordable and this way I could buy the whole pack part by part in several months and wouldn't think twice. But $130 at once, I can't really justify it (and still havent been able to) just as easily with low (to none) disposable student income.  Or instead, having sales one - twice a year would be great as well.
      One advantage of this change would be allowing more people to buy -> support the development & community -> give feedback -> improve these products. This could also potentially pose a disadvantage where requests from people would increase the headache and time required to respond to all these requests, but this community is more than able to handle that.
      I'm sure there's a reason to why these modules are sold together, and I'd love to hear your side as well, @ryan, (and all other maintainers).
      Regards,
      Abdus.