2

I'm trying to upload an image to our system. It is working well when I'm using it in localhost but when I'm testing in server, it's throwing an error Image source not readable. I'm trying to upload a 2.2MB photo since my max size of image upload is 3MB. Uploading images less than or equal to 2MB in server has no problem but in local, it accepts up to 3MB in which it is the expected behavior. I'm using docker for my local development and my server is CentOS7. Is there some configurations I should touch so that it can accept 3MB image size in my server? Or is there something I have to change in my code below in processing the image?

REQUEST

public function rules()
{
    return [
        'photo' => 'image|mimes:jpeg,png,jpg|max:3000',
    ];
}

Controller

$photo = $request->file('photo');

$server_dir = storage_path(config('const.upload_local_temp_path'));
FileHelper::addDirectory($server_dir, 0777);

$file_name = FileHelper::makeUniqFileName($photo->getClientOriginalExtension(), $server_dir);
$filepath = $server_dir . $file_name;
$img_path = FileHelper::storeResizeImg($photo->path(), $filepath, null, 300);
$img_url = FileHelper::getPublicPath($img_path);
return $img_url;

FILEHELPER

/*
* 
*/
public static function addDirectory($directory, $mod)
{

    if (!File::exists($directory)) {
        File::makeDirectory($directory, $mod, true);
    }
}

/**
* 
*/
public static function storeResizeImg($file, $filepath, $width, $height)
{
    $org_img = Image::make($file);
    $org_img->orientate();
    $org_img = $org_img->resize($width, $height, function ($constraint) {
        $constraint->aspectRatio();
    });
    $org_img->save($filepath);
    return $filepath;
}

public static function makeUniqFileName($ext, $path)
{
    $file_name = '';
    while (1) {
        $file_name = sha1(rand().microtime()).'.'.$ext;
        if (!File::exists($path. $file_name)) {
            break;
        }
    }
    return $file_name;
}

UPDATE

Error message that the image size is greater than 3MB is working in my local, but in server it says : The given data was invalid.

Please let me know of your thoughts.

Eem Jee
  • 1,239
  • 5
  • 30
  • 64

0 Answers0