【问题标题】:laravel with intervention package maximum image upload validationlaravel 带干预包最大图片上传验证
【发布时间】:2014-12-06 20:34:20
【问题描述】:

所以我使用干预包来保存并调整我的图像,如果它需要大分辨率,但是当我尝试上传大图像(大约 10 mb)时我得到 MethodNotAllowedHttpException。

所以我在这里和网上做了一些研究,确实有一些答案,但没有一个对我有用,我想知道为什么会这样?

try{
    $img = Input::file('gambar');

    if(!empty($img)){

        $file_max = ini_get('upload_max_filesize');
        $file_max_str_leng = strlen($file_max);
        $file_max_meassure_unit = substr($file_max,$file_max_str_leng - 1,1);
        $file_max_meassure_unit = $file_max_meassure_unit == 'K' ? 'kb' : ($file_max_meassure_unit == 'M' ? 'mb' : ($file_max_meassure_unit == 'G' ? 'gb' : 'unidades'));
        $file_max = substr($file_max,0,$file_max_str_leng - 1);
        $file_max = intval($file_max);

        $filename = $img->getClientOriginalName();


        $size = $img->getSize();

        if ($size < $file_max){
            if($this->save_image($img,$artikel,$filename)){
                $artikel->update(array(
                    'judul' => $judul,
                    'content' => Input::get('content'),
                    'kategori' => Input::get('kategori'),
                    'status' => Input::get('status'),
                    'pilihan' => Input::get('pilihan'),
                    'gambar' => $filename
                    ));
            }else{
                return Redirect::back()->withErrors($validator)->withInput();
            }
        }else{
            return Redirect::back()->withInput()->with('errormessage','El tamaño del archivo debe ser menor que %smb.',$file_max);
        }           
    }
}catch(Exception $e){
    return Redirect::back()->withInput()->with('errormessage','The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit);
}

这是我的 save_image 函数

function save_image($img,$artikel,$filename){

    list($width, $height) = getimagesize($img);

    $path = public_path('images_artikel/');
    File::delete($path . $artikel->gambar);

    if($width > 1280 && $height > 720){
        if(Image::make($img->getRealPath())->resize('1280','720')->save($path . $filename))
            return true;
        else
            return Redirect::back()->withInput()->with('errormessage','Terjadi kesalahan dalam penyimpanan');
    }else{
        if(Image::make($img->getRealPath())->save($path . $filename))
            return true;
        else
            return Redirect::back()->withInput()->with('errormessage','Terjadi kesalahan dalam penyimpanan');
    }
}

我也尝试在我的模型中使用验证规则,但不起作用...

private $file_max;

function _construct(){
    $file_max = ini_get('upload_max_filesize');
    $file_max_str_leng = strlen($file_max);
    $file_max_meassure_unit = substr($file_max,$file_max_str_leng - 1,1);
    $file_max_meassure_unit = $file_max_meassure_unit == 'K' ? 'kb' : ($file_max_meassure_unit == 'M' ? 'mb' : ($file_max_meassure_unit == 'G' ? 'gb' : 'unidades'));
    $file_max = substr($file_max,0,$file_max_str_leng - 1);
    $file_max = intval($file_max);
}

// Add your validation rules here
public static $rules = [
     'judul' => 'required|between:5,255',
     'content' => 'required',
     'gambar' => 'image|mimes:jpeg,jpg,png,bmp|max:{$file_max}'

];

这是我的路线

Route::resource('artikels','AdminArtikelsController');

还有我的表格

{{ Form::model($artikel, array('route' => array('admin.artikels.update',$artikel->id), 'method' => 'put', 'files' => true)) }}

那么还有其他解决方案吗?

【问题讨论】:

    标签: php file-upload laravel laravel-4


    【解决方案1】:

    我相信最佳做法是像这样创建您的自定义验证器:

    <?php
    
    namespace App\Validators;
    
    
    use Illuminate\Validation\Validator;
    
    class CustomImageValidator extends Validator {
    
        public function validateMaxResolution($attribute, $value, $parameters)
        {
            $this->requireParameterCount(1, $parameters, 'max_resolution');
    
            list($width, $height) = getimagesize($value);
            $resolution = explode('x', $parameters[0]);
            $max_width = $resolution[0];
            $max_height = $resolution[1];
    
            return ($width <= $max_width && $height <= $max_height);
        }
    
        protected function replaceMaxResolution($message, $attribute, $rule, $parameters)
        {
            return str_replace(':value', implode(', ', $parameters), $message);
        }
    
    }
    

    然后在AppServiceProvider.php注册验证器

    <?php
    
    namespace App\Providers;
    
    use App\Validators\CustomImageValidator;
    use Illuminate\Support\ServiceProvider;
    use Validator;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            Validator::resolver(function($translator, $data, $rules, $messages)
            { // custom image validator ;)
                return new CustomImageValidator($translator, $data, $rules, $messages);
            });
        }
    
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    }
    

    在您的resource/lang/en/validation.php 中添加您的默认消息:

     'max_resolution'           => 'The :attribute image resolution is too large to resize. Max resolution is :value.',
    

    最后将您的规则添加到规则数组中:

    'your_image_field' => 'required|mimes:png,jpg,jpeg,bmp|max:1024|max_resolution:3000x3000'
    

    这是在 Laravel 5.1.16 (LTS)

    上测试的

    【讨论】:

      【解决方案2】:

      确保您在表单中将其用作操作属性的路由是 post 类型,并且表单已激活文件。 像这样的:

      路线:

      Route::post('upload', array('uses' => 'UploadController@handleUpload', 'as' => 'upload'));
      

      对于表格:

      Form::open(array('route' => 'upload', 'files' => true));
      

      【讨论】:

      • 你能发布artisan routes的输出吗?
      【解决方案3】:

      检查两件事:

      1. 在您的 phpinfo.php 中:memory_limitupload_max_filesize
      2. 如果您的图像处理器是 GD,请尝试:Image::configure(array('driver' =&gt; 'imagick'));

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2017-08-20
        • 2017-07-25
        • 2019-01-16
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 2018-12-15
        • 2015-03-13
        • 2021-03-15
        相关资源
        最近更新 更多