LemonStand Documentation

core:onProcessImage event

The core:onProcessImage event allows to process product and other images using third-party image manipulation tools. This event is triggered every time when you call the Db_File::getThumbnailPath() method (and hence - every time when you call the Shop_Product::image_url() and Shop_Category::image_url() methods). This, the event allows to use the usual programming interface with third-party image processing modules.

The event handler should accept 5 parameters:

  • File object - a reference to the Db_File class instance, which represents the original image.
  • Image width - the image width requested in the Db_File::getThumbnailPath() method call.
  • $height - the image height requested in the Db_File::getThumbnailPath() method call.
  • $returnJpeg - the $returnJpeg parameter value specified in the Db_File::getThumbnailPath() method call.
  • $params - the $params parameter value specified in the Db_File::getThumbnailPath() method call. You can use this parameter for passing image library specific parameters from the Db_File::getThumbnailPath() call to the event handler.

The event handler should return a path to the generated image. The path should be relative to the LemonStand root directory. In the event handler you should check whether the thumbnail is not generated yet. Example of the event handler:

public function subscribeEvents()
{
  Backend::$events->addEvent('core:onProcessImage', $this, 'process_image');
}

public function process_image($file_obj, $width, $height, $returnJpeg, $params)
{
  /*
   * This code just copies the original image to the uploaded/thumbnails directory
   */
  
  // Generate the thumbnail file name and check whether it does not exist yet  
  $ext = $returnJpeg ? 'jpg' : 'png';
  $thumbnail_path = '/uploaded/thumbnails/db_file_img_'.$file_obj->id.'_'.$width.'x'.$height.'.'.$ext;

  // Return the thumbnail path if it does exist
  if (file_exists($thumbnail_path))
    return $thumbnail_path;
      
  // Process image with a third-party image library and save it to the
  // uploaded/thumbnails directory. Please note - to get an absolute path
  // to a file, we prepend the PATH_APP constant.
  
  copy(PATH_APP.$file_obj->getPath(), PATH_APP.$thumbnail_path);

  // Return the relative path to the thumbnail
  return $thumbnail_path;
}

Next: core:onBeforeDatabaseQuery event
Previous: shop:onExtendOrdersToolbar event
Return to Handling LemonStand events