Hi,

I'm sorry if I am in the wrong forum, but I did not find a better place.

I often want to scale images in an other way, than the drupal core image resize function does. I want the border-length to always be same. For example the profile images should always be 160x250 and not only in the best case. If I use Drupals resize function, it might also come to a 30x250 image. I could just resize the image to the given size without watching the ratio, but this way, images will look stupid.
This is why I think the following function might be good to be added to the core-image functionality. It downscales an image to match best into a given imagesize and fills the rest of the new image with a color of choice. It is somehow like Ebay does with the userimages.

If there is already such a functionality or I am the only one, who needs it or it is the wrong area to post this code, I'm sorry and I ask you, to kindly move the code where it belongs.

Markus

/**
 * Scale an image to the specified size using GD adding colored edges to exactly match the given size.
 */
function image_gd_resize_to_fill_space($source, $destination, $totalwidth, $totalheight, $color=array(255, 255, 255)) {
  if (!file_exists($source)) {
    return false;
  }
  $base = image_get_info($source);
  if (!$base) {
    return false;
  }

  $im = image_gd_open($source, $base['extension']);
  if (!$im) {
    return false;
  }
 
  $base['ratio']=$base['height']/$base['width'];  
  $target['ratio']=$totalheight/$totalwidth;
  
  
  if($totalwidth<$base['width'] || $totalheight<$base['height']) {	//a side is larger than maxlength, so the image needs to be scaled
     if($base['ratio']>$target['ratio']){	//height is larger than maxheight
        $target['height']=$totalheight;
        $target['width']=$totalheight*$totalwidth/$base['height'];
     } elseif($base['ratio']<$target['ratio']) {	//width is larger than maxwidth
        $target['width']=$totalwidth;
        $target['height']=$totalheight*$totalwidth/$base['width'];	
     } else {		//same, just scale down
        $target['width']=$totalwidth;
        $target['height']=$totalheight;	
     }
  } else {	//the image is smaller, so dimensions should stay
       $target['width']=$base['width'];
       $target['height']=$base['height'];	
  }
  
  //setting the position of the resampled image in the new image
  $target['x']=($totalwidth-$target['width'])/2;
  $target['y']=($totalheight-$target['height'])/2; 

  $res = imageCreateTrueColor($totalwidth, $totalheight);
  if ($info['extension'] == 'png') {
    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
    imagealphablending($res, false);
    imagefilledrectangle($res, 0, 0, $totalwidth, $totalheight, $transparency);
    imagealphablending($res, true);
    imagesavealpha($res, true);
  }
  
  //filling with the wanted backgroundcolor;
  $bg=imageColorAllocate($res, $color[0], $color[1], $color[2]);
  imageFill($res, 0, 0, $bg);

  //building the image
  imageCopyResampled($res, $im, $target['x'], $target['y'], 0, 0, $target['width'], $target['height'], $base['width'], $base['height']);
  $result = image_gd_close($res, $destination, $base['extension']);

  imageDestroy($res);
  imageDestroy($im);

  return $result;
}

Comments

benbruscella’s picture

Is this a CCK type? If so, it sounds like imagecache would do this. I havent tried it yet, so not sure...

NewToDruballer’s picture

>>Is this a CCK type?

Sorry I'm still relatively new to Drupal and though I read often about CCK, I still do not know, what it exactly does.
I took code from the original image-functions and modified it, so it can fit extended scaling needs.

>>If so, it sounds like imagecache would do this.
I'm not sure. I just looked at imagecache and it seems, as if it does not provide this functionality. It seems to be the functions as the built-in code has - just with caching extensions. Correct me, if I am wrong.