Jump to content

Am I reinventing the wheel? Creating multiple version of an image when uploading


Jay D
 Share

Recommended Posts

Hi all! 

I want to check my sanity on this one, I want to create multiple versions of an image when uploading to PW Admin, think Hero and Thumbnail. I am not finding any recent modules, so I decided to write one. Now I am having an issue where I am not able to access Image method. Does anyone have an example of how to manipulate images once it is processed? And is there a good document on how to debug the API calls? 

I want to avoid having the images processed every time a page is loaded, or when the cache is cleared. 

Thanks,

Jay

Link to comment
Share on other sites

You can also generate desired image variations when you upload an image in the field:

/**
 * // in ready.php
 * Image variations
 * Generate image variations on upload
 * Pageimage api ref: https://processwire.com/api/ref/pageimage/
 */
$this->wire()->addHookAfter('InputfieldImage::fileAdded', function ($event) {
	$inputfield = $event->object;
	$image = $event->argumentsByName("pagefile");

	// `image` field on `document` template
	if ($inputfield->hasField == 'image'
		&& $inputfield->hasPage
		&& $inputfield->hasPage->template->name == 'document') {

      // some custom options
      $defaultOptions = array(
        'upscaling' => false,
        'cropping'  => false,
        'quality'   => 90,
      );

	  // generate image variations
	  // See wire/config.php for $config->sizeName: https://github.com/processwire/processwire/blob/master/wire/config.php#L726-L789
	  $image->size('landscape'); // 16/9 defined size `landscape` from $config->sizeName
	  $image->size(200, 250);    // 4/3	thumbnails	
	}
});

 

For debugging => https://processwire.com/modules/tracy-debugger/ 

  • Like 3
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...