Web Hosting Talk







View Full Version : PHP image upload and thumb create script


Shib
11-23-2009, 04:16 PM
I have been looking around for a script that will allow me to upload images to a specific folder and then create a thumb nail of that image in an another folder. I have found a script similar to this but it only supports the .jpg extension and I need the .gif, .bmp and .png extensions

Does anybody have something that they could share with me?

I don't need anything over complicated because I will be intergrating it with my existing website.

Thank you :)

HostBill
11-24-2009, 05:21 PM
im using bit modificated http://phpthumb.sourceforge.net/ - its powerfull class when it comes to thumbnail generation.

Shib
11-25-2009, 11:34 AM
im using bit modificated http://phpthumb.sourceforge.net/ - its powerfull class when it comes to thumbnail generation.


Thank you :)

I however have found another script (mentioned above), which I have modified to use the other image extensions.

Thank you for trying to help though!

include
11-25-2009, 11:52 AM
Care to share your script? Always looking out for simplistic upload scripts :-)

Shib
11-26-2009, 12:17 PM
Yeah sure
save it as image_upload.php
create a new folder called images and then a new folder inside of it called thumbs.
This will allow you to upload .jpg, .gif and .png aslong as they are under 2mb. Once the files are uploaded they will be resized into an image with a width of 200px.

I didn't write all of this script, I just added the .gif and .png extensions and the size limitation.


<form action="image_upload.php" method="post" enctype="multipart/form-data" id="something" class="uniForm">
<input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
<button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
<?php
$stop = '0';
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = time().$_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$file_size = $_FILES['new_image']['size'];
$size_limit = '2000000';
$target = "images/".$imagename;
$file_type = $_FILES['new_image']['type'];

if($file_size >= $size_limit) :
echo 'You image is to large!';
else :
if($_FILES['new_image']['type'] == 'image/pjpeg'):
move_uploaded_file($source, $target);
elseif($_FILES['new_image']['type'] == 'image/x-png'):
move_uploaded_file($source, $target);
elseif($_FILES['new_image']['type'] == 'image/gif'):
move_uploaded_file($source, $target);
endif;
endif;


$imagepath = $imagename;
$save = "images/" . $imagepath; //This is the new file you saving
$file = "images/" . $imagepath; //This is the original file
$x = @getimagesize($file);
switch($x[2]) {
case 1:
$image = imagecreatefromgif($file);
break;
case 2:
$image = imagecreatefromjpeg($file);
break;
case 3:
$image = imagecreatefrompng($file);
break;
default:
echo "file is not a valid image file.<br>";
$stop = '1';
break;
}
if($stop != 1) {
list($width, $height) = getimagesize($file) ;

$modwidth = $width;

$diff = $width / $modwidth;

$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
/*
$file_type = $_FILES['new_image']['type'];
if($file_type == "image/jpeg" || $file_type == "image/jpg") :
$image = imagecreatefromjpeg($file);
elseif($file_type == "image/x-png" || $file_type == "image/png") :
$image = imagecreatefrompng($file);
elseif($file_type == "image/gif") :
$image = imagecreatefromgif($file);
else :
echo 'Invalid type';
endif;*/

imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;

$save = "images/thumbs/sml_" . $imagepath; //This is the new file you saving
$file = "images/" . $imagepath; //This is the original file

list($width, $height) = getimagesize($file) ;

$modwidth = 200;

$diff = $width / $modwidth;

$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$x = @getimagesize($file);
switch($x[2]) {
case 1:
$image = imagecreatefromgif($file);
break;
case 2:
$image = imagecreatefromjpeg($file);
break;
case 3:
$image = imagecreatefrompng($file);
break;
default:
echo "file is not a valid image file.";
}
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;
echo "Large image: <img src='images/".$imagepath."'><br>";
echo "Thumbnail: <img src='images/thumbs/sml_".$imagepath."'>";

}
}
echo $_FILES['new_image']['type'],'<br>';
echo $_FILES['new_image']['name'],'<br>';
}
?>

include
11-26-2009, 02:45 PM
Thanks for that :-)

Shib
11-26-2009, 07:00 PM
Yeah no problem

However I am having trouble getting this to run on firefox

any ideas?