<?php
// $Id$

/**
 * The purpose of this file, is to provide a skeleton for transforms,
 * so that there is an ease of use for developing them.
 *
 * Variables passed to this include are:
 * $filename, $options and $op.
 */

switch ($op) {
  // This tests for the supported extensions for this transformation
  case 'supported':
      // Required variables: $extensions
      
      // $extensions is an array of extensions which this transformation can handle
      $extensions = array(
        'png',
        'gif',
        'jpg',
      );
    break;

  // gets the description for the transform
  case 'description':
      // Required variables: $description
      
      $description = t('Allows scaling of images to specific boundaries.');
    break;

  // creates the form for the required values for the transform
  case 'form':
      // Required variables: $form
      
      // This uses the form API to handle the necessary settings for the transform.
      // Everything here is stored as a tree structure, whereas in the database that
      // tree structure is preserved as a serialized variable.
      $form['width'] = array(
        '#type' => 'textfield',
        '#title' => t('Width'),
        '#default_value' => (isset($options['width']) ? $options['width'] : '100%'),
        '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'),
      );

      $form['height'] = array(
        '#type' => 'textfield',
        '#title' => t('Height'),
        '#default_value' => (isset($options['height']) ? $options['height'] : '100%'),
        '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'),
      );
    break;

  // validate the above form
  case 'validate':
      // Required variables: none.
      
      // Allows for validating the form elements added in the 'form' section.
      // This is important for making sure that the settings inputted are valid.
      if (!preg_match("/^\d+(%|)$/", $options['height'])) {
        form_set_error('height', t('Height must be a number, or a percentage.'));
      }
      // check if number or percentage
      if (!preg_match("/^\d+(%|)$/", $options['width'])) {
        form_set_error('width', t('Width must be a number, or a percentage.'));
      }
      //
      if (!preg_match("/^(\d+|left|center|right)$/i", $options['xoffset'])) {
        form_set_error('xoffset', t('XOffset must be either a number, or left, center, or right.'));
      }
      //
      if (!preg_match("/^(\d+|top|center|bottom)$/i", $options['yoffset'])) {
        form_set_error('yoffset', t('YOffset must be either a number, or top, center, or bottom.'));
      }
    break;

  // called on submission of transform add/insert/edit form
  case 'submit':
      // Required variables: none
      
      // $options contains the details of everything that went into the database,
      // as well as the transform ID for which it is associated - set as $options['tid'].
      // This allows transforms, such as watermark, to upload files if need be.
    break;

  // workhorse of the transform
  case 'execute':
      // Required variable: $filename
      
      // This performs the transformation on the filename. The resulting filename must be set
      // back into $filename. On failure, drupal_set_message should be used.
      if ($option == null) {
        return;
      }

      // get the details, since we may need to change things
      $details = image_get_info($filename);

      // readjust for percentages
      $options['width'] = media_convert_percent($options['width'], $details['width']);
      $options['height'] = media_convert_percent($options['height'], $details['height']);

      // otherwise perform the action
      if (!image_crop($filename, $filename, $options['xoffset'], $options['yoffset'], $options['width'], $options['height'])) {
        drupal_set_message(t('Transform failed [%transform]', array('%transform' => 'crop')));
      }
    break;
}
