thumbnailing AND setting limit on size of fullsize pic
Posted: Thu Jan 20, 2005 12:55 am
Hello,
I'm using the following snippet of code to accept an array (of pictures) from a form which automatically creates proportional thumbnails and automatically links the thumbs to the fullsize pics. Everything below works fine. You can see near the bottom how the prefix 'tb_' is automatically appended to the thumbs.
What I am having a brain fart about is how to proportionately resize the fullsize picture as well to set a limit on how big each picture will be.
Thanks for any help
I'm using the following snippet of code to accept an array (of pictures) from a form which automatically creates proportional thumbnails and automatically links the thumbs to the fullsize pics. Everything below works fine. You can see near the bottom how the prefix 'tb_' is automatically appended to the thumbs.
What I am having a brain fart about is how to proportionately resize the fullsize picture as well to set a limit on how big each picture will be.
Code: Select all
if($_REQUEST['action'] == 'photos_uploaded')
{
// INITIALIZATION
$result_final = '';
$counter = 0;
// LIST OF OUR KNOWN PHOTO TYPES
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
// GD LIBRARY FUNCTION LIST
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
// FETCH THE PHOTO ARRAY SENT BY PREUPLOAD.PHP
$photos_uploaded = $_FILES['photo_filename'];
while($counter <= count($photos_uploaded))
{
if($photos_uploaded['size'][$counter] > 0)
{
if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
{
$result_final .= 'File '.($counter+1).' is not a photo<br />';
}
else
{
mysql_query(\"INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0','\".addslashes($photo_caption[$counter]).\"', '\".addslashes($_REQUEST['category']).\"')\");
$new_id = mysql_insert_id();
$filetype = $photos_uploaded['type'][$counter];
$extention = $known_photo_types[$filetype];
$filename = $new_id.'.'.$extention;
mysql_query(\"UPDATE gallery_photos SET photo_filename='\".addslashes($filename).\"' WHERE photo_id='\".addslashes($new_id).\"'\");
// STORE THE ORIGNAL FILE
copy($photos_uploaded['tmp_name'][$counter], $images_dir.'/'.$filename);
// GET THE THUMBNAIL SIZE
$size = GetImageSize($images_dir.'/'.$filename);
if($size[0] > $size[1])
{
$thumbnail_width = 100;
$thumbnail_height = (int)(100 * $size[1] / $size[0]);
}
else
{
$thumbnail_width = (int)(100 * $size[0] / $size[1]);
$thumbnail_height = 100;
}
// BUILD THUMBNAIL WITH GD
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = 'ImageCreateFrom'.$function_suffix;
$function_to_write = 'Image'.$function_suffix;
// READ THE SOURCE FILE
$source_handle = $function_to_read($images_dir.'/'.$filename);
if($source_handle)
{
// CREATE AN BLANK IMAGE FOR THE THUMBNAIL
$destination_handle = ImageCreateTrueColor($thumbnail_width, $thumbnail_height);
// NOW WE RESIZE IT
//ImageCopyResized($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);
ImageCopyResampled($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);
}
// SAVE THE THUMBNAIL
$function_to_write($destination_handle, $images_dir.'/tb_'.$filename);
ImageDestroy($destination_handle);
$result_final .= \"<img src='\".$images_dir. \"/tb_\".$filename.\"' /> File \".($counter+1).\" Added<br />\";
}
}
$counter++;
}Thanks for any help