I just noticed that I have an image that is already 460px wide and I print it out through preset that only has "scale: width 460" action on it, the image gets compressed again - not just copied - thus reducing the initial quality.

Original image:
http://satumaa.yle.fi/sites/satumaa.yle.fi/files/images/suvi-ja-marko-bl...
Same image through imagecache preset:
http://satumaa.yle.fi/sites/satumaa.yle.fi/files/imagecache/6col/images/...

Is there a way to avoid this unnecessary action? IE. if there image is already in right size, just copy the original to a new path?

Comments

tormu’s picture

Title: Unnecessary action taken on right sized image » Remove unnecessary scaling for right sized image
Category: support » feature

This is how we handled it. When the image is displayed, we check if the preset only has one action and if it's scale. If that's true, then the image size is compared to the scale settings to see if there's a need for a scale or not. If not, then the original image is used.

And yeah, that code isn't as elegant as I'd hope for it to be, but at least it seems working. I hope this feature gets into the module in some way, though I think it would be better to stick it into the preset setting page..

function themename_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  // Check if original picture can be shown instead of the imagecache version
  $original_image = image_get_info($path);
  $preset = imagecache_preset_by_name($presetname);
  $use_original = false;
  //If there's only one action and that is scaling, then check image dimensions to see if the original image can be used
  if(count($preset['actions']) == 1  &&  $preset['actions'][0]['action'] == 'imagecache_scale') {
  	$data = $preset['actions'][0]['data'];
  	$upscale = $data['upscale'];
  	$dimensions = array();
  	if(is_numeric($data['height'])) {
  		$dimensions['height'] = $data['height'];
  	}
    if(is_numeric($data['width'])) {
      $dimensions['width'] = $data['width'];
    }

  	$use_original = true;
  	foreach($dimensions as $dimension => $value) {
  		if($value < $original_image[$dimension] || (($value > $original_image[$dimension]) && $upscale)) {
        $use_original = false;
  		}
  	}
  	// Special case where both dimensions are defined, upscale is on and one (or both) of the dimensions is equal to the preset's dimension = NO SCALING NEEDED
  	if($dimensions['height'] && $dimensions['width'] && $upscale &&
        (
         ($dimensions['height'] == $original_image['height'] && $dimensions['width'] >= $original_image['width'])
         ||
         ($dimensions['width'] == $original_image['width'] && $dimensions['height'] >= $original_image['height'])
        )
  	  ) {
      $use_original = true;
  	}
  }
  $imagepath = ($use_original) ? $path : imagecache_create_path($presetname, $path);

  // Check is_null() so people can intentionally pass an empty array
  // to override the defaults completely.
  if(is_null($attributes)) {
    $attributes = array('class' => 'imagecache imagecache-'. $presetname);
  }
  if($getsize && ($image = image_get_info($imagepath))) {
    $attributes['width'] = $image['width'];
    $attributes['height'] = $image['height'];
  }
  $attributes = drupal_attributes($attributes);

  $url = ($use_original) ? url($path) : imagecache_create_url($presetname, $path);
  return '<img src="'. $url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}
fizk’s picture

Status: Active » Closed (fixed)

Please reopen if this is still an issue with ImageCache 6.x-2.0-rc1.