THE GOAL: To have a field where the user can upload their tiff image but have it displayed as a jpeg thumbnail.

I'm using imagecache and imagemagick for my image-handling and I'm using Drupal 6 right now because it doesn't look like the fieldfield in D7 can use imagecache presets (imagefield doesn't support tiffs). However if anyone knows how to get this to work in Drupal 7 that would be great too.

-Below are the relevant modules I have installed:
CCK 6.x-2.9
filefield 6.x-3.10
im_raw 6.x-1.2
imageapi 6.x-1.10
imagecache 6.x-2.0-beta12
imagecache_actions 6.x-1.8

If you already know how to do this please PM me. For now I can pay $50 which is why I wouldn't want anyone to spend too much time on this, but if you already know how then it could be lunch money for the week. Please let me know if I need to provide more information.

Thanks!

Comments

WorldFallz’s picture

If it were me, I'd have two separate fields (one filefield for the tiff, one imagefield for the jpg). You should be able to use the media_mover module to have a converted jpg added to the imagefield automatically (then use that for imagecache).

jfev’s picture

Would media_mover actually convert the tiff to a jpeg?... or are you just saying to use media_mover to attach it after it's converted? Basically the part I'm stuck at is the actual conversion process.

jvizcarrondo’s picture

- I think that you could convert current field widget to generic field.
- Install imagecache_actions.
- Add new preset to File Format switcher (jpeg file).
- Add preset to current field.

I don't know if imagecache_actions can convert tiff to jpeg but you also could create new action in imagecache
Hoping to have helped
Juan

nikit’s picture

imagecache_actions haven't tiff convertation.

jfev’s picture

I know ImageMagick itself can convert a tiff, but it looks like imagecache actions is still catching up. The ImageMagick Raw module looks like it would do the trick, except that I have no idea what to put in the command line.

jvizcarrondo’s picture

Unfortunately ImageMagick Raw Effect module has this limitation (see help in modulo):
"Do not add an input or output file. ImageAPI will add "convert" before and a "-quality" option after based on the configuration !settings_link"

so you could not put the output you expect.
Leave the bad taste how easy it would do this with Imagemagick:

convert your_file.tiff  your_file.jpeg

and ready. In reviewing the code of ImageCache is another difficulty arises: You can not change the file input and output in actions (by default), so set a new action with this structure, it would be a waste of time.

Now with a bit of time, in a solution a little dirty, I have changed a bit the code functions in imagecache imagecache_build_derivative and _imagecache_cache to allow this work (I do not know what the global consequences in presets, have to try long-term), which I place below:
imagecache.module

<?php

/**
 * handle request validation and responses to imagecache requests.
 */
function _imagecache_cache($presetname, $path) {
  if (!$preset = imagecache_preset_by_name($presetname)) {
    // Send a 404 if we don't know of a preset.
    header("HTTP/1.0 404 Not Found");
    exit;
  }

  // umm yeah deliver it early if it is there. especially useful
  // to prevent lock files from being created when delivering private files.
  $dst = imagecache_create_path($preset['presetname'], $path);
  if (is_file($dst)) {
    imagecache_transfer($dst);
  }

  // preserve path for watchdog.
  $src = $path;

  // Check if the path to the file exists.
  if (!is_file($src) && !is_file($src = file_create_path($src))) {
    watchdog('imagecache', '404: Unable to find %image ', array('%image' => $src), WATCHDOG_ERROR);
    header("HTTP/1.0 404 Not Found");
    exit;
  };

  // Bail if the requested file isn't an image you can't request .php files
  // etc...
  if (!getimagesize($src)) {
    watchdog('imagecache', '403: File is not an image %image ', array('%image' => $src), WATCHDOG_ERROR);
    header('HTTP/1.0 403 Forbidden');
    exit;
  }

  $lockfile = file_directory_temp() .'/'. $preset['presetname'] . basename($src);
  if (file_exists($lockfile)) {
    watchdog('imagecache', 'ImageCache already generating: %dst, Lock file: %tmp.', array('%dst' => $dst, '%tmp' => $lockfile), WATCHDOG_NOTICE);
    // 307 Temporary Redirect, to myself. Lets hope the image is done next time around.
    header('Location: '. request_uri(), TRUE, 307);
    exit;
  }
  touch($lockfile);
  // register the shtdown function to clean up lock files. by the time shutdown
  // functions are being called the cwd has changed from document root, to
  // server root so absolute paths must be used for files in shutdown functions.
  register_shutdown_function('file_delete', realpath($lockfile));

  // check if deriv exists... (file was created between apaches request handler and reaching this code)
  // otherwise try to create the derivative.
  if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, &$dst)) {
    imagecache_transfer($dst);
  }
  // Generate an error if image could not generate.
  watchdog('imagecache', 'Failed generating an image from %image using imagecache preset %preset.', array('%image' => $path, '%preset' => $preset['presetname']), WATCHDOG_ERROR);
  header("HTTP/1.0 500 Internal Server Error");
  exit;
}

/**
 * Create a new image based on an image preset.
 *
 * @param $preset
 *   An image preset array.
 * @param $source
 *   Path of the source file.
 * @param $destination
 *   Path of the destination file.
 * @return
 *   TRUE if an image derivative is generated, FALSE if no image
 *  derivative is generated. NULL if the derivative is being generated.
 */
function imagecache_build_derivative($actions, $src, $dst) {
  // get the folder for the final location of this preset...
  $dir = dirname($dst);
  // Build the destination folder tree if it doesn't already exists.
  if (!file_check_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
    watchdog('imagecache', 'Failed to create imagecache directory: %dir', array('%dir' => $dir), WATCHDOG_ERROR);
    return FALSE;
  }

  // Simply copy the file if there are no actions.
  if (empty($actions)) {
    return file_copy($src, $dst, FILE_EXISTS_REPLACE);
  }

  if (!$image = imageapi_image_open($src)) {
    return FALSE;
  }

  if (file_exists($dst)) {
    watchdog('imagecache', 'Cached image file %dst already exists but is being regenerated. There may be an issue with your rewrite configuration.', array('%dst' => $dst), WATCHDOG_WARNING);
  }

  foreach ($actions as $action) {
    if (!empty($action['data'])) {
      // Make sure the width and height are computed first so they can be used
      // in relative x/yoffsets like 'center' or 'bottom'.
      if (isset($action['data']['width'])) {
        $action['data']['width']   = _imagecache_percent_filter($action['data']['width'], $image->info['width']);
      }
      if (isset($action['data']['height'])) {
        $action['data']['height']  = _imagecache_percent_filter($action['data']['height'], $image->info['height']);
      }
      if (isset($action['data']['xoffset'])) {
        $action['data']['xoffset'] = _imagecache_keyword_filter($action['data']['xoffset'], $image->info['width'], $action['data']['width']);
      }
      if (isset($action['data']['yoffset'])) {
        $action['data']['yoffset'] = _imagecache_keyword_filter($action['data']['yoffset'], $image->info['height'], $action['data']['height']);
      }
    }
    if (!_imagecache_apply_action($action, $image)) {
      watchdog('imagecache', 'action(id:%id): %action failed for %src', array('%id' => $action['actionid'], '%action' => $action['action'], '%src' => $src), WATCHDOG_ERROR);
      return FALSE;
    }
    else {
      if ($action['action'] == 'imagecache_convertimages') {
        $current_path = pathinfo($image->source);
        $extension = $current_path['extension'];
        $dst1 = str_replace($extension, $action['data']['extension'], $image->source);
        $dst = str_replace($extension, $action['data']['extension'], $dst);
        $image = imageapi_image_open($dst1);
      }
    }
  }

  if (!imageapi_image_close($image, $dst)) {
    watchdog('imagecache', 'There was an error saving the new image file %dst.', array('%dst' => $dst), WATCHDOG_ERROR);
    return FALSE;
  }

  return TRUE;
}
?>

and now we can do new action in a new module called imagecache convert images (only works for imageMagick):
imagecache_convertimages.info

name = Imagecache Convert Images
description =  Imagecache Convert Images
dependencies[] = imagecache
package = ImageCache
core = 6.x

imagecache_convertimages.module

<?php

/**
 * Implementation of hook_actions()
 */
function imagecache_convertimages_imagecache_actions() {
  $actions = array(
    'imagecache_convertimages' => array(
      'name' => 'Convert',
      'description' => t('Convert images files to other extensions.'),
    ),
  );

  return $actions;
}

/**
 * Implementation of theme_form() for imagecache_ui.module
 * (select image extension)
 */ 
function imagecache_convertimages_form($action) {
  $types_allow = array(
    'jpeg' => 'Image Type JPEG',
    'png' => 'Image Type PNG',
  );
  $form['extension'] = array(
    '#type' => 'select',
    '#title' => t('Select imag extension'),
    '#options' => $types_allow,
    '#default_value' => $action['extension'],
  );
  return $form;
}

/**
 * Implementation of hook_theme()
 */
function imagecache_convertimages_theme() {
  return array(
    'imagecache_convertimages' => array(
      'arguments' => array('element' => NULL),
    )
  );
}

/**
 * Implementation of theme_hook() for imagecache_ui.module
 * (show extension)
 */ 
function theme_imagecache_convertimages($element) {
  return t('Convert Image Type to %ext', array('%ext'=> $element['#value']['extension']));
}

/**
 * Implementation of hook_image()
  only imagemagick
 */
function imagecache_convertimages_image(&$image, $data) {
  if ($image->info['mime_type'] && $image->toolkit == 'imageapi_imagemagick') {
    //get extension
    $current_path = pathinfo($image->source);
    $rsr = $image->source;
    $extension = $current_path['extension'];
    $destination = str_replace($extension, $data['extension'], $image->source);
    $command = escapeshellarg($rsr) .' '. escapeshellarg($destination);
    if (0 != _imageapi_imagemagick_convert_exec($command, $output, $errors)) {
      return FALSE;
    }
    return file_exists($destination);
  }
  return FALSE;
}

/**
 * Implementation of hook_enable()
 */
function imagecache_convertimages_enable() {
  if (function_exists('imagecache_action_definitions') ) imagecache_action_definitions(TRUE);
  cache_clear_all('imagecache_actions', 'cache');
}

/**
 * Implementation of hook_disable()
 */
function imagecache_convertimages_disable() {
  if (function_exists('imagecache_action_definitions') ) imagecache_action_definitions(TRUE);
  cache_clear_all('imagecache_actions', 'cache');
}
?>

This new action seems to work to convert TIFF to PNG and JPEG

Hoping to have helped
Juan

jfev’s picture

Wow, gonna take a minute to wrap my head around this and then give it try!

jfev’s picture

So it looks like this pretty much works with some minor issues...

1. First off, it does actually convert the file which is great!!!
2. I had to manually create the imagecache preset folder for the converted file to live.
3. For some reason, you have to create two folders... it seems like the module is trying to create the file in one spot, and look for it in another.
4. Finally, Imagecache won't display the converted file which is the most important part..(could be because 2 & 3).

Any thoughts?

jvizcarrondo’s picture

I have developed a new version of the module that does not require making modifications ImageCache. This version fixes the errors presented above:
imagecache_convertimages.module

<?php
define('IMAGECACHE_CONVERTIMAGES_STRING', '_ext1_');
define('IMAGECACHE_CONVERTIMAGES_NOEXT', 'noext');
/**
 * Implementation of hook_actions()
 */
function imagecache_convertimages_imagecache_actions() {
  $actions = array(
    'imagecache_convertimages' => array(
      'name' => 'Convert',
      'description' => t('Convert images files to other extensions.'),
    ),
  );

  return $actions;
}

/**
 * Implementation of theme_form() for imagecache_ui.module
 * (select image extension)
 */ 
function imagecache_convertimages_form($action) {
  $types_allow = array(
    'jpeg' => 'Image Type JPEG',
    'png' => 'Image Type PNG',
  );
  $form['extension'] = array(
    '#type' => 'select',
    '#title' => t('Select imag extension'),
    '#options' => $types_allow,
    '#default_value' => $action['extension'],
  );
  return $form;
}

/**
 * Implementation of theme_hook() for imagecache_ui.module
 * (show extension)
 */ 
function theme_imagecache_convertimages($element) {
  return t('Convert Image Type to %ext', array('%ext'=> $element['#value']['extension']));
}

/**
 * Implementation of hook_image()
  only imagemagick
 */
function imagecache_convertimages_image(&$image, $data) {
  if ($image->info['mime_type'] && $image->toolkit == 'imageapi_imagemagick') {
    //get extension
    $current_path = pathinfo($image->source);
    $rsr = $image->source;
    $current_path = $current_path['extension'];
    if ($current_path['extension']) {
      $destination = str_replace('.' . $current_path['extension'], IMAGECACHE_CONVERTIMAGES_STRING . $current_path['extension'], $image->source) . '.' . $data['extension'];
    }
    else {
      $destination = $image->source . IMAGECACHE_CONVERTIMAGES_STRING . IMAGECACHE_CONVERTIMAGES_NOEXT . '.' . $data['extension'];
    }
    $command = escapeshellarg($rsr) .' '. escapeshellarg($destination);

    if (0 != _imageapi_imagemagick_convert_exec($command, $output, $errors)) {
      return FALSE;
    }
    return file_exists($destination);
  }
  return FALSE;
}

/**
 * Implementation of hook_enable()
 */
function imagecache_convertimages_enable() {
  if (function_exists('imagecache_action_definitions') ) imagecache_action_definitions(TRUE);
  cache_clear_all('imagecache_actions', 'cache');
}

/**
 * Implementation of hook_disable()
 */
function imagecache_convertimages_disable() {
  if (function_exists('imagecache_action_definitions') ) imagecache_action_definitions(TRUE);
  cache_clear_all('imagecache_actions', 'cache');
}



/**
 * Implementation of hook_theme().
*/
function imagecache_convertimages_theme() {
  $imagecache_convertimages_path = drupal_get_path('module', 'imagecache_convertimages') . '/templates';
  return array(
    'imagecache' => array(
      'arguments' => array(
        'namespace' => NULL,
        'path' => NULL,
        'alt' => NULL,
        'title' => NULL,
      ),
      'function' => 'theme_imagecache_convertimages_imagecache',
    ),
  );
  $theme['imagecache_convertimages'] = array(
    'arguments' => array('element' => NULL),
  );

  foreach (imagecache_presets() as $preset) {
    $theme['imagecache_formatter_'. $preset['presetname'] .'_path'] = array(
      'arguments' => array('element' => NULL),
      'function' => 'theme_imagecache_convertimages_imagecache_formatter_path',
    );

    $theme['imagecache_formatter_'. $preset['presetname'] .'_url'] = array(
      'arguments' => array('element' => NULL),
      'function' => 'theme_imagecache_convertimages_imagecache_formatter_url',
    );
  }
  return $theme;
}

function theme_imagecache_convertimages_imagecache_formatter_path($element) {
  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return '';
  }

  // Extract the preset name from the formatter name.
  $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));
  $path = imagecache_create_path($presetname, $element['#item']['filepath']);

  if ($preset = imagecache_preset_by_name($presetname)) {
    foreach ($preset['actions'] as $action) {
      if ($action['action'] == 'imagecache_convertimages') {
        //GET FILE EXTENSION
        $current_path = pathinfo($path);
        $current_extension = $current_path['extension'];
        //change file extension
        if ($current_extension) {
          $path = str_replace('.' . $current_extension, IMAGECACHE_CONVERTIMAGES_STRING . $current_extension, $path) . '.' . $action['data']['extension'];
        }
        else {
          $path = $path . IMAGECACHE_CONVERTIMAGES_STRING . IMAGECACHE_CONVERTIMAGES_NOEXT . '.' . $action['data']['extension'];
        }
      }
    }
  }

  return $path;


}

/**
 * Create and image tag for an imagecache derivative
 *
 * @param $presetname
 *   String with the name of the preset used to generate the derivative image.
 * @param $path
 *   String path to the original image you wish to create a derivative image
 *   tag for.
 * @param $alt
 *   Optional string with alternate text for the img element.
 * @param $title
 *   Optional string with title for the img element.
 * @param $attributes
 *   Optional drupal_attributes() array. If $attributes is an array then the
 *   default imagecache classes will not be set automatically, you must do this
 *   manually.
 * @param $getsize
 *   If set to TRUE, the image's dimension are fetched and added as width/height
 *   attributes.
 * @return
 *   HTML img element string.
 */
function theme_imagecache_convertimages_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  // Check is_null() so people can intentionally pass an empty array of
  // to override the defaults completely.
  if (is_null($attributes)) {
    $attributes = array('class' => 'imagecache imagecache-'. $presetname);
  }
  if ($getsize && ($image = image_get_info(imagecache_create_path($presetname, $path)))) {
    $attributes['width'] = $image['width'];
    $attributes['height'] = $image['height'];
  }

  $attributes = drupal_attributes($attributes);
  $imagecache_url = imagecache_convertimages_create_url($presetname, $path);
  return '<img src="'. $imagecache_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}

/**
 * Return a URL that points to the location of a derivative of the
 * original image transformed with the given preset.
 *
 * Special care is taken to make this work with the possible combinations of
 * Clean URLs and public/private downloads. For example, when Clean URLs are not
 * available an URL with query should be returned, like
 * http://example.com/?q=files/imagecache/foo.jpg, so that imagecache is able
 * intercept the request for this file.
 *
 * This code is very similar to the Drupal core function file_create_url(), but
 * handles the case of Clean URLs and public downloads differently however.
 *
 * @param $presetname
 * @param $filepath
 *   String specifying the path to the image file.
 * @param $bypass_browser_cache
 *   A Boolean indicating that the URL for the image should be distinct so that
 *   the visitors browser will not be able to use a previously cached version.
 *   This is
 */
function imagecache_convertimages_create_url($presetname, $filepath, $bypass_browser_cache = FALSE) {
  $path = _imagecache_strip_file_directory($filepath);
  if (module_exists('transliteration')) {
    $path = transliteration_get($path);
  }
  if ($preset = imagecache_preset_by_name($presetname)) {
    foreach ($preset['actions'] as $action) {
      if ($action['action'] == 'imagecache_convertimages') {
        //GET FILE EXTENSION
        $current_path = pathinfo($path);
        $current_extension = $current_path['extension'];
        //change file extension
        if ($current_extension) {
          $path = str_replace('.' . $current_extension, IMAGECACHE_CONVERTIMAGES_STRING . $current_extension, $path) . '.' . $action['data']['extension'];
        }
        else {
          $path = $path . IMAGECACHE_CONVERTIMAGES_STRING . IMAGECACHE_CONVERTIMAGES_NOEXT . '.' . $action['data']['extension'];
        }
      }
    }
  }
  $args = array('absolute' => TRUE, 'query' => empty($bypass_browser_cache) ? NULL : time());
  switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
    case FILE_DOWNLOADS_PUBLIC:
      return url($GLOBALS['base_url'] . '/' . file_directory_path() .'/imagecache/'. $presetname .'/'. $path, $args);
    case FILE_DOWNLOADS_PRIVATE:
      return url('system/files/imagecache/'. $presetname .'/'. $path, $args);
  }
}

function theme_imagecache_convertimages_imagecache_formatter_url($element) {
  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return '';
  }

  // Extract the preset name from the formatter name.
  $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));

  return imagecache_convertimages_create_url($presetname, $element['#item']['filepath']);
}

/**
 * Implementation of hook_menu_alter()
 */
function imagecache_convertimages_menu_alter(&$items) {
  //change imagecache default callback
  $items[file_directory_path() .'/imagecache']['page callback'] = 'imagecache_convertimages_cache';
  //$items['system/files/imagecache']['page callback'] = 'imagecache_convertimages_cache_private';
}
/**
 * callback for handling public files imagecache_convertimages requests.
 */
function imagecache_convertimages_cache() {
  $args = func_get_args();
  $preset = check_plain(array_shift($args));
  $path = implode('/', $args);
  _imagecache_convertimages_cache($preset, $path);
}

/**
 * handle request validation and responses to imagecache requests.
 */
function _imagecache_convertimages_cache($presetname, $path) {
  if (!$preset = imagecache_preset_by_name($presetname)) {
    // Send a 404 if we don't know of a preset.
    header("HTTP/1.0 404 Not Found");
    exit;
  }

  // umm yeah deliver it early if it is there. especially useful
  // to prevent lock files from being created when delivering private files.

  $dst = imagecache_create_path($preset['presetname'], $path);

  if (is_file($dst)) {
    imagecache_transfer($dst);
  }
  //get original image path
  $ext_file = explode(IMAGECACHE_CONVERTIMAGES_STRING, $path);
  $remplace_string = $ext_file[0];
  if ($ext_file[1] && $ext_file[1] != IMAGECACHE_CONVERTIMAGES_NOEXT) {
    $ext1 = explode('.', $ext_file[1]);
    $remplace_string .= '.' . $ext1[0];
  }
  $path = $remplace_string;
  // preserve path for watchdog.
  $src = $path;

  // Check if the path to the file exists.
  if (!is_file($src) && !is_file($src = file_create_path($src))) {
    watchdog('imagecache', '404: Unable to find %image ', array('%image' => $src), WATCHDOG_ERROR);
    header("HTTP/1.0 404 Not Found");
    exit;
  };

  // Bail if the requested file isn't an image you can't request .php files
  // etc...
  if (!getimagesize($src)) {
    watchdog('imagecache', '403: File is not an image %image ', array('%image' => $src), WATCHDOG_ERROR);
    header('HTTP/1.0 403 Forbidden');
    exit;
  }

  $lockfile = file_directory_temp() .'/'. $preset['presetname'] . basename($src);
  if (file_exists($lockfile)) {
    watchdog('imagecache', 'ImageCache already generating: %dst, Lock file: %tmp.', array('%dst' => $dst, '%tmp' => $lockfile), WATCHDOG_NOTICE);
    // 307 Temporary Redirect, to myself. Lets hope the image is done next time around.
    header('Location: '. request_uri(), TRUE, 307);
    exit;
  }
  touch($lockfile);
  // register the shtdown function to clean up lock files. by the time shutdown
  // functions are being called the cwd has changed from document root, to
  // server root so absolute paths must be used for files in shutdown functions.
  register_shutdown_function('file_delete', realpath($lockfile));

  // check if deriv exists... (file was created between apaches request handler and reaching this code)
  // otherwise try to create the derivative.
  if (file_exists($dst) || imagecache_convertimages_cache_build_derivative($preset['actions'], $src, &$dst)) {
    imagecache_transfer($dst);
  }
  // Generate an error if image could not generate.
  watchdog('imagecache', 'Failed generating an image from %image using imagecache preset %preset.', array('%image' => $path, '%preset' => $preset['presetname']), WATCHDOG_ERROR);
  header("HTTP/1.0 500 Internal Server Error");
  exit;
}

/**
 * Create a new image based on an image preset.
 *
 * @param $preset
 *   An image preset array.
 * @param $source
 *   Path of the source file.
 * @param $destination
 *   Path of the destination file.
 * @return
 *   TRUE if an image derivative is generated, FALSE if no image
 *  derivative is generated. NULL if the derivative is being generated.
 */

function imagecache_convertimages_cache_build_derivative($actions, $src, $dst) {

  // get the folder for the final location of this preset...
  $dir = dirname($dst);
  // Build the destination folder tree if it doesn't already exists.
  if (!file_check_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
    watchdog('imagecache', 'Failed to create imagecache directory: %dir', array('%dir' => $dir), WATCHDOG_ERROR);
    return FALSE;
  }

  // Simply copy the file if there are no actions.
  if (empty($actions)) {
    return file_copy($src, $dst, FILE_EXISTS_REPLACE);
  }

  if (!$image = imageapi_image_open($src)) {
    return FALSE;
  }

  if (file_exists($dst)) {
    watchdog('imagecache', 'Cached image file %dst already exists but is being regenerated. There may be an issue with your rewrite configuration.', array('%dst' => $dst), WATCHDOG_WARNING);
  }

  foreach ($actions as $action) {
    if (!empty($action['data'])) {
      // Make sure the width and height are computed first so they can be used
      // in relative x/yoffsets like 'center' or 'bottom'.
      if (isset($action['data']['width'])) {
        $action['data']['width']   = _imagecache_percent_filter($action['data']['width'], $image->info['width']);
      }
      if (isset($action['data']['height'])) {
        $action['data']['height']  = _imagecache_percent_filter($action['data']['height'], $image->info['height']);
      }
      if (isset($action['data']['xoffset'])) {
        $action['data']['xoffset'] = _imagecache_keyword_filter($action['data']['xoffset'], $image->info['width'], $action['data']['width']);
      }
      if (isset($action['data']['yoffset'])) {
        $action['data']['yoffset'] = _imagecache_keyword_filter($action['data']['yoffset'], $image->info['height'], $action['data']['height']);
      }
    }
    if (!_imagecache_apply_action($action, $image)) {
      watchdog('imagecache', 'action(id:%id): %action failed for %src', array('%id' => $action['actionid'], '%action' => $action['action'], '%src' => $src), WATCHDOG_ERROR);
      return FALSE;
    }
    else {
      if ($action['action'] == 'imagecache_convertimages') {
        $current_path = pathinfo($image->source);
        $extension = $current_path['extension'];
        if ($current_path['extension']) {
          $dst1 = str_replace('.' . $current_path['extension'], IMAGECACHE_CONVERTIMAGES_STRING . $current_path['extension'], $image->source) . '.' . $action['data']['extension'];
          $dst = str_replace('.' . $current_path['extension'], IMAGECACHE_CONVERTIMAGES_STRING . $current_path['extension'], $dst) . '.' . $action['data']['extension'];
        }
        else {
          $dst1 = $image->source . IMAGECACHE_CONVERTIMAGES_STRING . IMAGECACHE_CONVERTIMAGES_NOEXT . '.' . $action['data']['extension'];
          $dst = $dst . IMAGECACHE_CONVERTIMAGES_STRING . IMAGECACHE_CONVERTIMAGES_NOEXT . '.' . $action['data']['extension'];
        }
        $image = imageapi_image_open($dst1);
        //register_shutdown_function('file_delete', realpath($dst1));
      }
    }
  }

  if (!imageapi_image_close($image, $dst)) {
    watchdog('imagecache', 'There was an error saving the new image file %dst.', array('%dst' => $dst), WATCHDOG_ERROR);
    return FALSE;
  }

  return TRUE;
}

this module does not work with private directory
Juan

jfev’s picture

Absolutely fantastic work! Thanks.

sébastien FAURE’s picture

Hi jvizcarrondo
this is a really nice module. I tried to use it but I am facing a problem.
If I place the convert action first, it convert the image as I want. But I would like to implement this action after several other actions. In that case the convert action does not work.

for example :

- first action scale 130x100 with the scale action
- second action convert to jpeg with your action

in that case the first action is not taken into account.
If I change the order it is working fine. But for what I want to do, I need to convert the image format at the end of a list of differents actions.

So my question is : How can I use your action after other imagecache actions ?

Regards

sébastien FAURE’s picture

I am using drupal 6