Hello, kindly ask your help regarding a function to upload images in two folders, one original image and the "Thumb" folder for thumbnails. Well, I entered the rand code to rename the file name. Loading is performed properly in the root folder (Original image) but not in the thumb folder.
I tried some changes but with little success.
This is the code:
function Upload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){
//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;
//file name setup
$filename_err = explode(".",$_FILES[$field_name]['name']);
$filename_err_count = count($filename_err);
$file_ext = $filename_err[$filename_err_count-1];
$userpic = rand(1000,1000000).'.'.$file_ext;
if($file_name != '')
{
$fileName = $file_name.'.'.$file_ext;
}
else
{
$fileName = $_FILES[$field_name]['name'];
}
//upload image
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$target_path.$userpic))
{
//thumbnail creation
if($thumb == TRUE)
{
$thumbnail = $thumb_path.$fileName;
list($width,$height) = getimagesize($userpic);
$thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
switch($file_ext){
case 'jpg':
$source = imagecreatefromjpeg($userpic);
break;
case 'jpeg':
$source = imagecreatefromjpeg($userpic);
break;
case 'png':
$source = imagecreatefrompng($userpic);
break;
case 'gif':
$source = imagecreatefromgif($userpic);
break;
default:
$source = imagecreatefromjpeg($userpic);
}
imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
switch($file_ext){
case 'jpg' || 'jpeg':
imagejpeg($thumb_create,$thumbnail,100);
break;
case 'png':
imagepng($thumb_create,$thumbnail,100);
break;
case 'gif':
imagegif($thumb_create,$thumbnail,100);
break;
default:
imagejpeg($thumb_create,$thumbnail,100);
}
}
return $fileName;
}
else
{
return false;
}
}
if(!empty($_FILES['image']['name'])){
//call thumbnail creation function and store thumbnail name
$thumb_img = Upload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');
//full path of the thumbnail image
$thumb_src = 'uploads/thumbs/'.$thumb_img;
//set success and error messages
$message = $thumb_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";
}else{
//if form is not submitted, below variable should be blank
$thumb_src = '';
$message = '';
}