1

So i'm using intervention package to save and also resize my image if it has to big resolution, but i get MethodNotAllowedHttpException when i tried to test with uploading big images (around 10 mb ).

so i do some research over here and the net, there is indeed some answer but none of them work with me, i wonder why is it happen?

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);
}

and here is my save_image function

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');
    }
}

i also tried to use validation rules in my models but not working...

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}'

];

here is my route

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

and my form

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

so is there other solution?

3 Answers 3

1

I believe the best practice is to create your custom validator like so:

<?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);
    }

}

Then register the validator in 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()
    {
        //
    }
}

In your resource/lang/en/validation.php add your default message:

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

And finally add your rule to the rules array:

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

This was tested on Laravel 5.1.16 (LTS)

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure that the route you use it as action attribute in the form is of type post, and also that the form has files activated. Something like this:

For the route:

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

For the form:

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

1 Comment

Can you post also the output of artisan routes?
0

check two things:

  1. In your phpinfo.php: memory_limit and upload_max_filesize
  2. If your image processor is GD, try: Image::configure(array('driver' => 'imagick'));

hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.