...

View Full Version : Resolved php image upload and resize



anki
07-17-2012, 08:19 AM
i found this code and just trying to save the resized image to folder album but it is saving original image, how to save resized image ?


<?php
session_start();
require ("config.php");
error_reporting(5);
$id = $_SESSION['id'];

$pals = $_SESSION['myses'];
if ($pals!=yes)
{
header("location:login.php?msgLogin=Login to access this page");
exit;
}

// Check if file was uploaded ok
if( ! is_uploaded_file($_FILES['image']['tmp_name']) || $_FILES['image']['error'] !== UPLOAD_ERR_OK)
{

header("location:upload_photo.php?msg=Photo not uploaded. Possibly too large.");
exit;
}

// Create image from file
switch(strtolower($_FILES['image']['type']))
{
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['image']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['image']['tmp_name']);
break;
default:
//exit('Unsupported type: '.$_FILES['image']['type']);
header("location:upload_photo.php?msg=Unsupported image type");
exit;
}

// Target dimensions
$max_width = 800;
$max_height = 600;

// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);

// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);

// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);

// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);

// Resize old image into new
imagecopyresampled($new, $image,
0, 0, 0, 0,
$new_width, $new_height, $old_width, $old_height);

// Catch the imagedata
ob_start();
imagejpeg($new, NULL, 90);
$data = ob_get_clean();

// Destroy resources
imagedestroy($image);
imagedestroy($new);

// Set new content-type and status code
//header("Content-type: image/jpeg", true, 200);

// Output data
//echo $data;


$pic = basename($_FILES['image']['name']);
$arr2 = explode(".", $pic);
$pic_ext = $arr2[sizeof($arr2) -1 ];


$uploaddir = "album/";
$uploaddir = $uploaddir . time() . "." . $pic_ext;


if(move_uploaded_file( $_FILES['image']['tmp_name'], $uploaddir))
{

// saving image info in the database
$sql = "INSERT INTO album (img, user_id)VALUES('$uploaddir', '$id')";
$res = mysql_query($sql);

////////////////// redirecting sucessfull msg
header("location:upload_photo.php?msg=Photo sucessfully uploaded, upload another photo.");
exit;
}
else
{

header("location:upload_photo.php?msg=Error uploading your photo, please try again.");
exit();
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum