Hi,

I'm trying to scale an image down, but I'd like to set a minimum height. I've got this code working on non-drupal sites, I'm just have a bit of trouble figuring out how to get it to work with this module. I know that I need to rebuild the $image variable before returning it, and I think I may be doing that incorrectly. Maybe someone can help me out and let me know what's wrong with this?

$min_height = 222; // set the minimum height	
$n_width= 150; // set new image width

$reductionRatio = $width / $n_width; // determine ratio of original width to new width
$n_height = $height / $reductionRatio; // scale down the height of the image based on ratio	

if ($n_height < $min_height){ //if new height is less than $min_height, set height to $min_height and then calculate width
	$n_height = $min_height;
	$reductionRatio = $height / $min_height; // determine ratio of original height to new height
	$n_width= $width / $reductionRatio; // scale down the width of the image based on ratio	
}

$newimage= imagecreatetruecolor($n_width,$n_height);
imageCopyResized($image->resource, $newimage, 0, 0, 0, 0, $n_width, $n_height, $width, $height);

return $image;

Comments

dman’s picture

Well, the key step you are missing from the above code is
<?
$image->resource = $newimage;
?>
.. and then you's also update the dimensions in $image->info if you wanted to be tidy

however...

You can also just pass the action on to the funcs that do it for you

imageapi_image_scale($image, $width, $height);

This updates the $image object by reference.
Use that instead of imagecreatetruecolor, imageCopyResized

fietserwin’s picture

Status: Active » Closed (works as designed)

I guess that answered the question.