Image Upload and resizing code using php and Mysql

05-24-09, 02:39 PM
|
|
Newbie Coder
|
|
Join Date: May 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Image Upload and resizing code using php and Mysql
Hello Guys i'm in need of some major assistance.
I'm busy with a website and will need to bring in a Image upload section where people can upload photos to. (max of 6 )
I have very very limited coding knowledge on this. i can program basic functionality and sections of a website.
I need a piece of code that can validate a image upload ( max size 2meg/ 2000pixels at the longest size )
then resize the photo to a maximum of 500 pixels with decent quality but not too big to make downloading too long
also to create a thumbnail of a maximum of 80 pixels
and then read it into a table field seperated by a comma
Can somebody please assist me?
Thank you very much for your time!
|

05-24-09, 09:05 PM
|
|
Aspiring Coder
|
|
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
|
|
I have a image upload script you can use. You are more than welcome to dig through it, and fix it the way you want it.
It's as good of a time to learn something anyway, right
script validates .gif, .jpeg, .png from both firefox and IE.
stores images in a folder named upload, same directory.
PHP Code:
if ($_FILES["file"]["name"]=="") {$content .= "You must choose a file to upload!";}
else if ($_POST["text"]=="") {$content .= "You must enter a description of the file!";}
else
{
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/x-png"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
$content .= "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$content .= "Upload: " . $_FILES["file"]["name"] . "<br />";
$content .= "Type: " . $_FILES["file"]["type"] . "<br />";
$content .= "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
$content .= "<br/><br/>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
$content .= $_FILES["file"]["name"] . " already exists. Upload cancelled!";
}
else
{
$uploadedfile = $_FILES["file"]["tmp_name"];
$image = str_replace(' ','_',$_FILES["file"]["name"]);
$size = getimagesize($uploadedfile);
$type = $size['mime'];
$width = $size[0];
$height = $size[1];
if($height > '600' || $width > '800')
{
$newwidth=800;
$newheight=($height/$width)*800;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$filename = "upload/$image";
if($size[2] == IMAGETYPE_GIF)
{
$src = imagecreatefromgif($uploadedfile);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagegif($tmp,$filename,100);
}
elseif($size[2] == IMAGETYPE_JPEG)
{
$src = imagecreatefromjpeg($uploadedfile);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($tmp,$filename,100);
}
elseif($size[2] == IMAGETYPE_PNG)
{
$src = imagecreatefrompng($uploadedfile);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagepng($tmp,$filename,9);
}
$content .= "Image has been resized to $newwidth X $newheight<br/>";
imagedestroy($src);
imagedestroy($tmp);
}
else
{
move_uploaded_file($uploadedfile, "upload/$image");
$content .= "Stored in: " . "upload/$image";
}
}
}
}
else { $content .= "Invalid file"; }
}
echo $content;
|

05-26-09, 06:28 PM
|
|
Newbie Coder
|
|
Join Date: May 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Jcbones
I'm going to look at it and work on it
Thanks alot
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|