• 0

[PHP][GD]image resize script


Question

If possible I need a image resize function that I can recall many times that will resize a JPEG with aspect ratio into a width of 600 pixels. Using PHP/GD.

Hopefully it can be pretty simple something like:

function resizejpg ($file)
{

return $file
}


Called like:

<img src="<?=resizejpg ($image)?>">

It does not need to be stored, and for certain reasons I do not want to do it with HTML. I have looked online and I cannot find any that will do it simply and keep the aspect ratio.

Please help somebody. [thought]that sounds really desperate[/thought]

Link to comment
https://www.neowin.net/forum/topic/755450-phpgdimage-resize-script/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

My code I wrote a few weeks ago:

// Frumusanu Andrei - March 09
// Create a resized jpeg from the input image
function createResized($imagepath,$destination,$newX,$newY)
{
	$return = true;

	// Get information on the image
	$info = getimagesize($imagepath);
	$x = $info[0];
	$y = $info[1];
	$type = $info[2];

	// Check what type the image we're inputing is, and create a GD image out of it
	switch($type)
	{
		case IMAGETYPE_GIF :
			$source = imagecreatefromgif($imagepath);
		break;

		case IMAGETYPE_JPEG :
			$source = imagecreatefromjpeg($imagepath);
		break;

		case IMAGETYPE_PNG :
			$source = imagecreatefrompng($imagepath);
		break;

		default:
			// setError("Error: Not a valid image type!");
			return false;
	}

	// Calculate the image ratios and check how we'll resize the image
	$ratio1 = $x/$newX;
	$ratio2 = $y/$newY;
	if($ratio1 > $ratio2)
	{
		$nx = $newX;
		$ny = $y / $ratio1;
	} else
	{
		$nx = $x / $ratio2;
		$ny = $newY;
	}

	// Create a new image
	$blank = imagecreatetruecolor($nx, $ny);
	$return = !$blank ? false : $return;

	// Copy the source image into the new image while resizing it
	$return = imagecopyresampled($blank, $source, 0, 0, 0, 0, $nx, $ny, $x, $y) ? $return : false;

	// Convert the new image into a jpeg
	$return = imagejpeg($blank, $destination) ? $return : false;

	// Destroy the image ressources we were working on to free the memory
	imagedestroy($blank);
	imagedestroy($source);

	return $return;
}

I would recommend to save the resized image and just link to it rather than to output it directly, because I don't even think that works.

You just give it the parameters of 1. the image to be resized, 2. the destination path of the resized image, 3 and 4. the maximum allowed image sizes, in this case 600 for $newX and some stupid high number for $newY.

If you still want it to output the image via php, which I wouldn't recommend then you can remove the $destination in $return = imagejpeg($blank, $destination) ? $return : false; See the documentation.

Function returns true/false depending on success/failure of the generation of the resized image.

Supports JPEG/PNG/GIFs and creates JPEGs. To add other input formats, add them in the switch to convert them.

To add other outputs you'll need to create a new switch like the top one with "$return = imagejpeg($blank, $destination) ? $return : false;" with imagejpeg replaced with the needed function. Read the GD library functions.

Edited by Nebuchadnezzar
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.