\n"; }
else {
// check if it's an image
$image_ext = array("image/gif", "image/jpg", "image/jpeg", "image/png");
if (in_array($_FILES['uploadedfile']['type'], $image_ext))
{
$root_path = $_POST['basedir'];
$target_path = $root_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded.
\n";
}
else
{
echo "There was an error uploading the file, please try again!
\n";
}
}
else
{
echo "File is not an image!
Allowed extensions: .jpg, .gif, .png
\n";
}
}
echo "Memory Usage: " . memory_get_usage() . "
\n";
ini_set('memory_limit', '50M');
// Create thumbnails
$thumbMax = 140; // max dimension for thumnails
$fname = basename( $_FILES['uploadedfile']['name']);
$info = pathinfo($target_path);
echo "Creating thumbnail for {$fname}({$target_path})...";
// load image and get image size
if ( strtolower($info['extension']) == 'jpg' )
{ $img = imagecreatefromjpeg( "{$target_path}" ); }
if ( strtolower($info['extension']) == 'png' )
{ $img = imagecreatefrompng( "{$target_path}" ); }
if ( strtolower($info['extension']) == 'gif' )
{ $img = imagecreatefromgif( "{$target_path}" ); }
if (!isset($img))
{ echo "bad filename.
\n"; } else {
// calculate thumbnail size
$width = imagesx( $img );
$height = imagesy( $img );
$rel = $width/$height;
if($rel>1){
$new_width = $thumbMax;
$new_height = floor( $height * ( $thumbMax / $width ) );
} else {
$new_height = $thumbMax;
$new_width = floor( $width * ( $thumbMax / $height ) );
}
// create a new temporary image
$tmp_img = imagecreatetruecolor( $thumbMax, $thumbMax );
imagefill($tmp_img, 0, 0, imagecolorallocate($tmp_img, 255, 255, 255));
// copy and resize old image into new image
$offx = ($thumbMax - $new_width)/2;
$offy = ($thumbMax - $new_height)/2;
imagecopyresized( $tmp_img, $img, $offx, $offy, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
if ( strtolower($info['extension']) == 'jpg' )
{ imagejpeg( $tmp_img, "{$root_path}thumb-{$fname}" ); }
if ( strtolower($info['extension']) == 'png' )
{ imagepng( $tmp_img, "{$root_path}thumb-{$fname}" ); }
if ( strtolower($info['extension']) == 'gif' )
{ imagegif( $tmp_img, "{$root_path}thumb-{$fname}" ); }
echo "good.
\n";
} // End thumbnail creation
unset($img); unset($tmp_img); // free memory
// Resize Image
$imgMax = 1024; // max dimension for image
$imginfo = getimagesize($target_path);
if ($imginfo[0]>$imgMax||$imginfo[1]>$imgMax)
{
$fname = basename( $_FILES['uploadedfile']['name']);
$info = pathinfo($target_path);
echo "Resizing {$fname}...";
// load image and get image size
if ( strtolower($info['extension']) == 'jpg' )
{ $img = imagecreatefromjpeg( "{$target_path}" ); }
if ( strtolower($info['extension']) == 'png' )
{ $img = imagecreatefrompng( "{$target_path}" ); }
if ( strtolower($info['extension']) == 'gif' )
{ $img = imagecreatefromgif( "{$target_path}" ); }
if (!isset($img))
{ echo "bad filename.
\n"; } else {
// calculate image size
$width = imagesx( $img );
$height = imagesy( $img );
$rel = $width/$height;
if($rel>1){
$new_width = $imgMax;
$new_height = floor( $height * ( $imgMax / $width ) );
} else {
$new_height = $imgMax;
$new_width = floor( $width * ( $imgMax / $height ) );
}
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
if ( strtolower($info['extension']) == 'jpg' )
{ imagejpeg( $tmp_img, "{$target_path}" ); }
if ( strtolower($info['extension']) == 'png' )
{ imagepng( $tmp_img, "{$target_path}" ); }
if ( strtolower($info['extension']) == 'gif' )
{ imagegif( $tmp_img, "{$target_path}" ); }
echo "good.
\n";
} } // End Image Resizeing
unset($img); unset($tmp_img); // free memory
echo '';
?>