所以我正在使用干预包保存并且如果它必须具有大分辨率也会调整我的图像大小,但是当我尝试上传大图像(大约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)) }}
还有其他解决方案吗?
答案 0 :(得分:1)
我认为最佳做法是创建自定义验证器,如下所示:
class Campaign < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :campaigns
end
然后在<?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.',
这是在 Laravel 5.1.16(LTS)
上测试的答案 1 :(得分:0)
确保您在表单中将其用作操作属性的路由为post类型,并且表单还激活了文件。 像这样:
路线:
Route::post('upload', array('uses' => 'UploadController@handleUpload', 'as' => 'upload'));
表格:
Form::open(array('route' => 'upload', 'files' => true));
答案 2 :(得分:0)
memory_limit
和upload_max_filesize
Image::configure(array('driver' => 'imagick'));
希望有所帮助