Codicus
11-6-01, 01:02 PM
Here's a neat bit of PHP code that I wrote for my web site to automatically generate thumbnails for uploaded images. The thumbnail filename will have a t_ prepended. The $path variable should be set to the path where your files are to be stored.
//only accept jpg files
if(strtolower(substr($imagefile_name, -4)) == ".jpg")
{
$filename = $path . $imagefile_name;
$thumbfile = $path . "t_" . $imagefile_name;
//Copy the uploaded file
if(move_uploaded_file($imagefile, $filename))
{
//Create an image for the thumbnail of the size I want
$dest = ImageCreate(150, 113);
//Load the full size image
$src = ImageCreateFromJPEG($filename);
//Copy the full size image to the thumbnail and resize it
ImageCopyResized($dest, $src, 0, 0, 0, 0, 150, 113, ImageSX($src), ImageSY($src));
//Write out the thumbnail
ImageJPEG($dest, $thumbfile);
//Set the file permissions
chmod($filename, 0644);
chmod($thumbfile, 0644);
}
}
If you're not familiar with file upload forms the html code to do so is below. The code above would be in the uploadimage.php action below.
<form enctype=multipart/form-data action=uploadimage.php method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
Image Filename: <INPUT NAME=imagefile TYPE=file>
<input type=submit value="Upload Image">
</form>
9
//only accept jpg files
if(strtolower(substr($imagefile_name, -4)) == ".jpg")
{
$filename = $path . $imagefile_name;
$thumbfile = $path . "t_" . $imagefile_name;
//Copy the uploaded file
if(move_uploaded_file($imagefile, $filename))
{
//Create an image for the thumbnail of the size I want
$dest = ImageCreate(150, 113);
//Load the full size image
$src = ImageCreateFromJPEG($filename);
//Copy the full size image to the thumbnail and resize it
ImageCopyResized($dest, $src, 0, 0, 0, 0, 150, 113, ImageSX($src), ImageSY($src));
//Write out the thumbnail
ImageJPEG($dest, $thumbfile);
//Set the file permissions
chmod($filename, 0644);
chmod($thumbfile, 0644);
}
}
If you're not familiar with file upload forms the html code to do so is below. The code above would be in the uploadimage.php action below.
<form enctype=multipart/form-data action=uploadimage.php method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
Image Filename: <INPUT NAME=imagefile TYPE=file>
<input type=submit value="Upload Image">
</form>
9