Hayling Island Internet Marketing and Technology Blog

£300CMS

A Content Management System can give you the power to control the content on your web site. Our CMS is a fully featured mature piece of professional software it also comes with a remarkable price tag.

  • A custom designed template
  • Edit your content
  • A web form

 

Contact us now to find out more.
Sep 30, 2009

PHP Image Upload and Resize


I create Web Apps in PHP and these usually require an image upload of some kind, unfortunately clients are likely to upload images that are simply too big to be put on a web site, so I felt I needed a PHP script to resize an image if it exceeds a predefined limit.

Looking around the internet for a script that did this I was stumped, I had seen Php Thumb 2.0 which is awesome but for the project I was working on I needed something to plug in very quickly.

 

Below is some basic code to upload an image to predefined location.

 

$path = "/images/";
$maxwidth = 1000;
$maxheight= 1000;
if ($_FILES['image']['error'] != 4)
{
	if ($_FILES['image']['type'] != 'image/jpeg' && $_FILES['image']['type'] != 'image/pjpeg')
	{
		$error = 'Floor Plan is not a Jpg'; 
	} else {
		$uniqueName = md5(date('l jS \of F Y h:i:s A') . $_FILES['image']["name"]);
		$uniqueName .= '.jpg';
		// lets move the file with our unique name and store that value in the $params
		
		move_uploaded_file($_FILES['image']["tmp_name"], $path . $uniqueName);
		
		$params['image'] = $uniqueName;
		
		$filename = $path . $uniqueName;		
		
	}
}

 

I set the path variable at the top, checked for the existence of the image, then tested to that the image was a jpg or jpeg, obviously you could include gif or png if you wanted.

 

For the unique name I use the name of the image and a date time stamp in a md5 hash, it is not pretty, but works well, then I just move the file to the correct destination and voila our image is uploaded.

 

This is great so far, but if a client chooses to upload a huge file this could cause problems on the frontend of our web site, so I wrote a simple little script to see if the height and width exceed a certain limit and then resize accordingly, depending on whether it is a landscape image or a portrait image.

 

$maxwidth = 1000;
$maxheight= 1000;

// Get new sizes
list($mywidth, $myheight) = getimagesize($filename);

if ($mywidth > $maxwidth || $myheight > $maxheight)
{
	if ($mywidth > $myheight)
	{
		//portrait
		$mynewwidth = $maxwidth;
		$mynewheight = round($myheight * ($maxwidth / $mywidth));
	} else {
		//landscape
		$mynewheight = $maxheight;
		$mynewwidth = round($mywidth * ($maxheight / $myheight));
	}
	
	$image_p = imagecreatetruecolor($mynewwidth, $mynewheight);
	$image = imagecreatefromjpeg($filename);

	// Resize
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $mynewwidth, $mynewheight, $mywidth, $myheight);
	
	unlink($filename);
	
	imagejpeg($image_p, $filename);   // Saving The Image
	imagedestroy($image_p);   // Destroying The Temporary Image
	imagedestroy($image);   // Destroying The Other Temporary Image 
}		

 

First I get the size of our uploaded image and test to see if the max height or max width has been exceeded. If this is true I work out which way round the image goes either landscape or portrait, if it is portrait I set the height to my default which is 1000 and then resize the width in scale with the resize.

 

After this we just do the standard PHP image resizing, destroy the original image and write our new image to the server.

 

Full code below:

$path = "/images/";
$maxwidth = 1000;
$maxheight= 1000;
if ($_FILES['image']['error'] != 4)
{
	if ($_FILES['image']['type'] != 'image/jpeg' && $_FILES['image']['type'] != 'image/pjpeg')
	{
		$error = 'Floor Plan is not a Jpg'; 
} else { $uniqueName = md5(date('l jS \of F Y h:i:s A') . $_FILES['image']["name"]); $uniqueName .= '.jpg'; // lets move the file with our unique name and store that value in the $params move_uploaded_file($_FILES['image']["tmp_name"], $path . $uniqueName); $params['image'] = $uniqueName; $filename = $path . $uniqueName; // Get new sizes list($mywidth, $myheight) = getimagesize($filename); if ($mywidth > $maxwidth || $myheight > $maxheight) { if ($mywidth > $myheight) { //portrait $mynewwidth = $maxwidth; $mynewheight = round($myheight * ($maxwidth / $mywidth)); } else { //landscape $mynewheight = $maxheight; $mynewwidth = round($mywidth * ($maxheight / $myheight)); } $image_p = imagecreatetruecolor($mynewwidth, $mynewheight); $image = imagecreatefromjpeg($filename); // Resize imagecopyresampled($image_p, $image, 0, 0, 0, 0, $mynewwidth, $mynewheight, $mywidth, $myheight); unlink($filename); imagejpeg($image_p, $filename); // Saving The Image imagedestroy($image_p); // Destroying The Temporary Image imagedestroy($image); // Destroying The Other Temporary Image } } }

 

I am sure many of you can see that this should be in a function or a class, but it was a quick fix to my problem, perhaps next time I will organize this into something a little more usable.