A623216a5d2384489e012478c555d167

The built in image functions found in PHP manage to boggle my mind to a point where i can no longer understand anything of what's going on. I'll just end up messing around long enough to get things right.

For this reason i'm always trying to write up functions (which often end up horribly wrong) that would obfuscate all the mind boggling soup out of my sight.

Anything to make the following more bearable to live with is very, very welcome. Thanks.

/**
 * Scale an image according to input canvas dimensions. 
 * No cropping will occur, the scaled image is "centered" to canvas.
 */

function imageToCanvas ($_image, $_canvasWidth, $_canvasHeight)
{
    if (is_string($_image)) {
        if (is_file($_image)) {
            $_image = imagecreatefromjpeg($_image);
        } else {
            return "Incorrect imagepath >> No file here " . $_image;
        }
    } 
    
    $width = imagesx($_image);
    $height = imagesy($_image);
    $image_aspect_ratio = $width / $height;
    $canvas_aspect_ratio = $_canvasWidth / $_canvasHeight;
    
    // scale by height
    if ($image_aspect_ratio < $canvas_aspect_ratio) {
        $new_height = $_canvasHeight;
        $new_width = ($new_height/$height) * $width;
    } 
    // scale by width
    else {
        $new_width = $_canvasWidth;
        $new_height = ($new_width/$width) * $height;
    }
    
    # offset values (ie. center the resized image to canvas)
    $xoffset = ($_canvasWidth - $new_width) / 2;
    $yoffset = ($_canvasHeight - $new_height) / 2;
    
    $image_resized = imagecreatetruecolor($_canvasWidth, $_canvasHeight);
    
    # fill colour
    $white = imagecolorallocate($image_resized, 255, 255, 255);
    imagefill($image_resized, 0, 0, $white);
    imagecopyresampled($image_resized, $_image, $xoffset, $yoffset, 0, 0, $new_width, $new_height, $width, $height);
    
    imagedestroy($_image);
    return $image_resized;
}

Refactorings

No refactoring yet !

151e36cc7f789a4790c8ca437e3a1f60

Chris Dean

August 14, 2008, August 14, 2008 07:00, permalink

1 rating. Login to rate!

I don't see any real issues with the way this works, though it all applies to GD lib - unless you're doing a check to see if GD lib's available elsewhere maybe you'd want to include something like that?

A623216a5d2384489e012478c555d167

Juha Hollanti

August 14, 2008, August 14, 2008 08:11, permalink

No rating. Login to rate!

Checking for GD lib might actually be prudent. I once lost hours trying to get something like a picture upload working on a new server. For some reason nothing gave out any error messages.

B8536fef632f141a7e17d1253ae29edb

bruce

September 29, 2008, September 29, 2008 20:20, permalink

No rating. Login to rate!

#1 problem I can see is it only accepts jpegs

120f01e5de902fe9366b0341854d696d

Giulio Ardoino

November 18, 2010, November 18, 2010 08:55, permalink

No rating. Login to rate!

Great function, it works very well.

1d63fad185e4c5046d20d446b46f120c

James

April 14, 2011, April 14, 2011 16:15, permalink

No rating. Login to rate!

Hi, I know this code is over 3 years old but I needed to use it in something I wrote (although I adapted it to suit). Anyway, I wanted it so that it only scaled the image down if it was too big for the canvas - otherwise it stayed the same aspect ratio (and therefore didn't risk being blurred when scaled-up). So I modified your code as below.

Note: it can be switched to always force scale (as your original code) by setting the fourth param to true.

Cheers

James

/**
 * Scale an image according to input canvas dimensions. 
 * No cropping will occur, the scaled image is "centered" to canvas.
 */

function imageToCanvas ($_image, $_canvasWidth, $_canvasHeight, $forceScale=false)
{
    if (is_string($_image)) {
        if (is_file($_image)) {
            $_image = imagecreatefromjpeg($_image);
        } else {
            return "Incorrect imagepath >> No file here " . $_image;
        }
    } 
    
    $width = imagesx($_image);
    $height = imagesy($_image);
    $image_aspect_ratio = $width / $height;
    $canvas_aspect_ratio = $_canvasWidth / $_canvasHeight;
    
    if($forceScale)
    {
	// scale by height
	if ($image_aspect_ratio < $canvas_aspect_ratio) {
		$new_height = $_canvasHeight;
		$new_width = ($new_height/$height) * $width;
	} 
	// scale by width
	else {
		$new_width = $_canvasWidth;
		$new_height = ($new_width/$width) * $height;
	}
    }
    else
    {
	if($width > $_canvasWidth)
	{
		$new_width = $_canvasWidth;
		$new_height = ($new_width/$width) * $height;
	}
	elseif($height > $_canvasHeight)
	{
		$new_height = $_canvasHeight;
		$new_width = ($new_height/$height) * $width;
	}
	else
	{
		$new_height = $height;
		$new_width = $width;
	}	
    }
    
    # offset values (ie. center the resized image to canvas)
    $xoffset = ($_canvasWidth - $new_width) / 2;
    $yoffset = ($_canvasHeight - $new_height) / 2;
    
    $image_resized = imagecreatetruecolor($_canvasWidth, $_canvasHeight);
    
    # fill colour
    $white = imagecolorallocate($image_resized, 255, 255, 255);
    imagefill($image_resized, 0, 0, $white);
    imagecopyresampled($image_resized, $_image, $xoffset, $yoffset, 0, 0, $new_width, $new_height, $width, $height);
    
    imagedestroy($_image);
    return $image_resized;
}

Your refactoring





Format Copy from initial code

or Cancel