How to resample images – a simple algorithm

When working with images I sooner or later usually run into the problem of creating thumbnails. Resampling images is not really difficult in the major programming languages however I have not found built-in support for caclulating thumbnail dimensions neither in .NET nor in PHP. And it seems that this made me re-invent the wheel over and over again creating codes with various lengths just for this simple task.

In Sense/Net 6.0, the open source ECMS I am working on I came across the exact same problem: I wanted to resize images, this time on the fly. This time however I decided to do it a bit less complicated as in previous cases and create a clean and simple solution. After a good deal of googling, tutorial reading and planning I came up with a fairly simple and good code snipplet.

The way I intended the thumbnail generation to work is to always fit the thumbnail into the target dimensions. So if there was a 600×400 picture and the target thumbnail size was 200×200 the generated picture would be 200×133. Similarly if the image size was 100×500 and the target thumbnail size 200×200 the generated picture’s width and height would be 40×200.

The idea behind the algorithm was to first determine whid dimension (width or height) is the “more critical” dimension. In the above examples at the 600×400 picture this dimension is the first one, width. In the second case (100×500) it is the second dimension, the height.

In the algorithm the first step is to determine this “critical” dimension by comapring current size/target size splits. Which ever is larger will be the dimension that will be resized to the thumbnails same dimension size and the other dimension will be shrinked accordingly. This might sound a little confusing so let the code speak for itself.

The C# code:

private static void GetRealXY(double imgX, double imgY, double targetX, double targetY, out double realX, out double realY)
{
	double xScale = imgX / targetX;
	double yScale = imgY / targetY;
 
	// Do not enlarge image
	if (yScale < 1)
		yScale = 1;
	if (xScale < 1)
		xScale = 1;
 
	if (yScale > xScale) // Image has to be shrinked based on height
	{
		realX = imgX * 1 / yScale;
		realY = imgY * 1 / yScale;
	}
	else // xScale > yScale // Image has to be shrinked based on width
	{
		realX = imgX * 1 / xScale;
		realY = imgY * 1 / xScale;
	}
}

And here’s how the same code in PHP would look like:

function getRealXY($imgX,$imgY,$targetX,$targetY)
{
	$xScale = $imgX / $targetX;
	$yScale = $imgY / $targetY;
 
	// Do not enlarge image
	if ($yScale < 1)
		$yScale = 1;
	if ($xScale < 1)
		$xScale = 1;
 
	if ($yScale > $xScale) // Image has to be shrinked based on height
	{
		$realX = $imgX * 1 / $yScale;
		$realY = $imgY * 1 / $yScale;
	}
	else // $xScale > $yScale // Image has to be shrinked based on width
	{
		$realX = $imgX * 1 / $xScale;
		$realY = $imgY * 1 / $xScale;
	}
	return array("x"=>$realX,"y"=>$realY);
}

Hope this snipplet helps. If you’re interested for the whole image resampling process you will be able to access it in the Sense/Net 6.0 Beta 3 source code which will be coming out in a few days.

One Response to How to resample images – a simple algorithm
  1. Nitin

    Cool! gr8 algo! :)