0

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 = '';
}
2
Contributors
1
Reply
16
Views
1 Year
Discussion Span
Last Post by AleMonteiro
0

Don't know if it's only this, but the uploaded image is at $target_path.$userpic and your are creating the thumb only from $userpic;

I think it should be this: $source = imagecreatefromjpeg($target_path.$userpic);

And the same to get the size: list($width,$height) = getimagesize($target_path.$userpic);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.