Duplicate and Resize Image During Upload

Hi all. I have this code for uploading an image, not for anything particular at the moment I’m just trying to learn things.

Basically at the moment this code uploads a file successfully if it is the right file type and applies an MD5 hash filename to it (not for any real reason, I have a method to just remove unsavoury characters instead but the MD5 was the last step I used and I haven’t undone it.

Anyway, what I am after is a tutorial on creating a function or just series of code to add to the script below that will create a second, resized copy of whatever image I upload and append ‘_thumb’ to it. The name part isn’t hard but I am struggling to find a tutorial that has decent description of what is going on, that I can apply to what I currently have or that isn’t just a script to download.

I’m more interested in a tutorial so I can understand what is happening as I move through the script. If anyone can provide such a tutorial I’d be much appreciative.

P.S. I would like to do this purely in Server Side if possible and avoid javascript.

Code I have so far:

<?php

function check_file($file) {
	$valid_types = array("image/pjpeg","image/jpg","image/jpeg","image/gif","image/png");
	
	if(in_array($file['type'],$valid_types)) 
		return 1;
		return 0;
}
$special = array('/','!','&','*',' ','-');

define('TARGET_PATH', 'uploadimages/');
$image = $_FILES['image'];
$image['name'] = stripslashes($image['name']);
$file_basename = substr($image['name'], 0, strripos($image['name'], '.')); // strip extention
$file_ext = substr($image['name'], strripos($image['name'], '.')); // strip name
$image['name'] = md5($file_basename).$file_ext;

if(check_file($image)){

	if ($image['error'] > 0)
		{
		echo "Error: " . $image["error"] . "<br/>";
		}
	else
		{
		if (file_exists(TARGET_PATH . $image["name"]))
			{
			echo $image["name"] . " already exists.";
			}
		else
			{
			move_uploaded_file($image["tmp_name"], TARGET_PATH. $image["name"]);
			echo "Stored in: " . TARGET_PATH . $image["name"] . "<br />";
			echo "Upload " . $image["name"] . "<br />";
			echo "Type " . $image["type"] . "<br />";
			echo "Size " . $image["size"] / 1024 . "Kb <br />";
			echo "<img src=" . TARGET_PATH . $image['name'] ." />";
			}
		}

	}
else
	{
	echo "Invalid file";
	}
?>

I know this goes against what you asked for, But IMHO things such as resizing an image are so trivial nowadays that there’s not a whole lot of point trying to write your own resizing script/class when there’s a lot of very decent solutions already out there.

I like http://phpthumb.gxdlabs.com/ which has a class for creating new thumbs, applying different resize methods etc. all with a few lines of code.

By looking at the code of classes like this, you’ll see other techniques such as method chaining, exceptions etc which will serve you in the long run to becoming a better developer, rather than simply knowing how to resize an image. If the script is too hard to follow, you could look at getting a debugger to step through the code, always helps me.

Yes, using an external library is most likely the way to go, so you don’t have to bother with special cases and possible bugs (while the math for resizing is easy, it isn’t trivial).

That being said, the basic code looks something along this:

// load the image
$im = imagecreatefromjpeg('big image.jpg');

// get width and height
$width = imagesx($img);
$height = imagesy($img);

// calculate the ratio
$ratio = $width / $height;
// do some calculations with it
// explained in more detail on http://stackoverflow.com/questions/1729887/little-math-help-for-image-resize-needed

// resize the image to $new_width, $new_height
$new_img = imagecopyresampled($img, ...); // better quality, works fine with alpha channel
$new_img = imagecopyresized($img, ...); // not so smooth, appropriate for 8-bit transparent gifs only

// save to a file
imagejpeg($new_img, 'resized.jpg');

In contrast, all the code you need, if you use WideImage, is:


include 'WideImage/WideImage.php';
 
WideImage::load('image')->resize(200, 200)->
  saveToFile(TARGET_PATH . $file_basename . '_thumb' . $file_ext);

Hi all,

I am also looking for a script that will allow you to upload a image (any image type) jpg,gif,png etc and create a thumbnail as well.

the script needs to store the thumbnails in the thumbnails folder and the original image in the images dir.

I am a newbie and have searched google and tried some samples ,but all i found was image galleries.

I want to create my own image gallery ,but i just need the script for the above.
if the script inserts the images into a database it is also or not.

Thanks in advance.

Nathan