Jump to content

GD Image Resize


Jonob

Recommended Posts

Hi all,

 

I am trying to resize an image using the following function. Everything appears to be working fine - I have verified that the correct size is obtained and that the correct $type is selected each time. The problem is that the resized_ image is not created at all.

 

I'm probably missing something really obvious here....but I cant quite figure out whats going wrong.

 

function resize_image($file)
{
$max_width = 350;
$max_height = 100;

//use php getimagesize function
list($width, $height, $type) = getimagesize($file);
if ($width > $max_width || $height > $max_height)
{
	if($type == 1)
	{
		$image = imagecreatefromgif($file);
	}
	elseif ($type == 2)
	{
		$image = imagecreatefromjpeg($file);
	}

	elseif ($type == 3)
	{
		$image = imagecreatefrompng($file);
	}
	$resized_width = $width;
	$resized_height = $height;

	if ($resized_width > $max_width)
	{
		$aspect_ratio = $max_width / $resized_width;
		$resized_width = round($aspect_ratio * $resized_width);
		$resized_height = round($aspect_ratio * $resized_height);
	}

	if ($resized_height > $max_height)
	{
		$aspect_ratio = $max_height / $resized_height;
		$resized_width = round($aspect_ratio * $resized_width);
		$resized_height = round($aspect_ratio * $resized_height);
	}
	$resized_img = imagecreatetruecolor($resized_width, $resized_height);

	//resize the image
	imagecopyresized($resized_img, $image, 0, 0, 0, 0, $resized_width, $resized_height, $width, $height);

	if($type == 1)
	{
		imagegif ($resized_img, "resized_" . $file);
	}
	elseif ($type == 2)
	{
		imagejpeg ($resized_img, "resized_" . $file);
	}
	elseif ($type == 3)
	{
		imagepng ($resized_img, "resized_" . $file);
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/191645-gd-image-resize/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.