This is my function that uploads images and you can resize them. All you have to do is include this in your script and pass the $_FILE[''] value (array) to it. Script will do the rest. Very copy/paste friendly.
PHP Code:<?php
/**
* -----------------------------------------------------------------------------
* Image Uploading Script
* by Steve Moss (Dr.Qmal@gmail.com)
* -----------------------------------------------------------------------------
* @imgUpload Function takes parameters, then renames the original file to
* random generated name (32 chars long), resizes it if one of parameters are
* set and then uploads it into desired directory.
* -----------------------------------------------------------------------------
*
* @param <array> $img - Our file array
* @param <string> $upload_dir - Directory where we want to upload our image
* @param <int> $newWidth - New width of image, null by default
* @param <int> $newHeight - New height of image, null by default
* @return <string> - Eather it's error type or uploaded image link
*/
function imgUpload($img, $upload_dir, $newWidth = null, $newHeight = null)
{
//First we will check if there are no errors
if ($img['error'] == 0)
{
//We will check if it is an image (pjpeg, x-png needed SPECIALLY for IE -.-)
if ( $img['type'] == "image/gif"
|| $img['type'] == "image/jpeg"
|| $img['type'] == "image/pjpeg"
|| $img['type'] == "image/png"
|| $img['type'] == "image/x-png")
{
//We need to check if image is not too big (6291456b or 6mb is default value)
if ($img['size'] < 6291456)
{
//Setting up file name
$imgName = $img['name'];
//Setting up the extension
$imgExtension = end(explode(".", $imgName));
//Creating random file name by using MD5 HASH from Unique ID generator (32 char long)
$imgNewName = md5(uniqid()) . "." . $imgExtension;
//Checking if file with this name already exists, if it does we generate new name
while (file_exists($upload_dir . $imgNewName) == true)
$imgNewName = md5(uniqid()) . "." . $imgExtension;
//Checking if we want to resize the image
if ($newWidth != null || $newHeight != null)
{
//First we get the old Width and Height in case one of parameters are "null"
list($oldWidth, $oldHeight) = getimagesize($img['tmp_name']);
//If $newWidth or $newHeight is "null" we will change that to original Height/Width
if ($newWidth == null)
$newWidth = $oldWidth;
if ($newHeight == null)
$newHeight = $oldHeight;
//Creating image from old one depending on its extension
if ($imgExtension == "jpeg" || $imgExtension == "jpg")
$source = imagecreatefromjpeg($img['tmp_name']);
if ($imgExtension == "png")
$source = imagecreatefrompng($img['tmp_name']);
if ($imgExtension == "gif")
$source = imagecreatefromgif($img['tmp_name']);
//Creating new image with desiered dimension
$newImage = imagecreatetruecolor($newWidth, $newHeight);
//Resizing our image with some manual corrections for optimal transition
imagecopyresized($newImage, $source, 0, 0, 0, 0, $newWidth, $newHeight + 1, $oldWidth - 2, $oldHeight);
//Moving our image to designated folder
imagejpeg($newImage, "$upload_dir/$imgNewName");
}
else
{
//If we don't want to resize our image just upload it
move_uploaded_file($img['tmp_name'], "$upload_dir/$imgNewName");
}
return "$upload_dir/$imgNewName";
}
else
return "sizeError";
}
else
return "fileError";
}
else
return "Error: " . $img['error'];
}

