我正在尝试使用带有“干预图像”包的Laravel,以不同分辨率将上传的图像调整为2倍的大小。那就是为什么我使用backup()方法在调整大小之前存储图片的原始分辨率。但是,当我运行代码时,出现错误“ Method Illuminate \ Http \ UploadedFile :: backup不存在”。有谁知道哪里出了问题?
Create.blade.php
@extends('layout')
@section('content')
<div class="container2">
<div class="container">
<div class="card card-container">
<!-- <img class="profile-img-card" src="//lh3.googleusercontent.com/-6V8xOA6M7BA/AAAAAAAAAAI/AAAAAAAAAAA/rzlHcD0KYwo/photo.jpg?sz=120" alt="" /> -->
<h1 style="text-align: center;">Napiši Vijest</h1>
@if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors ->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::open(['action' => 'MainController@store', 'method' => 'POST', 'files' => true]) !!}
<div class="form-group">
{{Form::label('postName', 'Ime')}}
{{Form::text('postName', '', ['id' => 'postName', 'class' => ($errors->has('postName')) ? 'form-control is-invalid' : 'form-control', 'placeholder' => 'Unesite naslov'])}}
</div>
<div class="form-group">
{{Form::label('naslov', 'Naslov')}}
{{Form::text('naslov', '', ['id' => 'inputname', 'class' => ($errors->has('naslov')) ? 'form-control is-invalid' : 'form-control', 'placeholder' => 'Unesite naslov'])}}
</div>
<div class="form-group">
{{Form::label('sadržaj', 'Sadržaj')}}
{{Form::textarea('sadržaj', '', ['id' => 'inputtext', 'class' => ($errors->has('sadržaj')) ? 'form-control is-invalid' : 'form-control', 'placeholder' => 'Unesite sadržaj'])}}
</div>
<div class="form-group">
{{Form::label('file', 'Izaberi fajl')}}
{{ Form::file('file') }}
</div>
<div class="form-group">
{{Form::label('slika', 'Izaberi sliku')}}
{{Form::file('slika')}}
</div>
<div class="form-group">
{{Form::label('Category', 'Izaberi kategoriju')}}
{{Form::select('category', $category, null) }}
</div>
{{Form::submit('Prihvati', ['class' => 'btn btn-success']) }}
<a href="{{URL::route('main')}}" class="btn btn-info btn-xs proba" style="margin-right: 3px;">Početna strana</a>
{!! Form::close() !!}
</div><!-- /card-container -->
</div><!-- /container -->
</div><!-- /container -->
@endsection
Web.php
Route::get('/createPost', 'MainController@create')->name('post.create')->middleware('admin');
Controller.php
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\Http\Request2;
//including post model to controller
use validate;
use App\Post;
use App\Document;
use App\Category;
//if we want to use sql syntax for queries
use DB;
use File;
use Image;
use Mail;
use Session;
USE Validator;
use Redirect;
use Illuminate\Support\Facades\Input;
class MainController extends Controller
{
public function create()
{
$category = Category::pluck('title', 'id');
return View('create', compact('category',$category));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$rules = [
'naslov' => 'required|min:3|max:20',
'sadržaj' => 'required|min:40',
'slika' => 'mimes:jpeg,bmp,png'
];
$customMessages = [
'required' => 'Unesite ":attribute" !',
'min' => 'Polje ":attribute" mora da ima minimum :min karaktera.',
'max' => 'Polje ":attribute" može da ima najviše :max karaktera.',
'email' => 'Polje ":attribute" mora da ima validan format',
'mimes' => '":attribute" mora biti u sledećim formatima: :values'
];
$validator = Validator::make(Input::all(), $rules, $customMessages);
if ($validator->fails())
{
return \Redirect::back()->withErrors($validator)->withInput();
}
//create new post
$post= new Post;
$post -> name = $request -> input('postName');
$post -> title = $request -> input('naslov');
$post -> content = $request -> input('sadržaj');
$post -> category_id = $request -> input('category');
// Handle File Upload
if( $request->hasFile('file') ) {
$filenameWithExt = $request->file('file')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('file')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('file')->storeAs('public/upload', $fileNameToStore);
$post->file_name = $fileNameToStore;
}
// Check if file is present
if( $request->hasFile('slika') ) {
$post_thumbnail = $request->file('slika');
$filename = time() . '.' . $post_thumbnail->getClientOriginalExtension();
$post_thumbnail->backup();
ini_set('memory_limit', '256M');
$filename=Image::make($post_thumbnail);
$filename->resize(329.33, 199.33)->save( public_path('uploads/' . $filename ) );
$post->post_thumbnail = $filename;
}
$post->save();
return redirect()->route('posts.show', $post)->with('successPost', 'Napisali ste novu vijest !');
}
}
答案 0 :(得分:1)
您可以做到,
<div class="row">
<div class="col-md-4">
<div class="dash-box dash-box-color-1">
<div class="dash-box-icon">
<i class="glyphicon glyphicon-cloud"></i>
</div>
<div class="dash-box-body">
<span class="dash-box-count">someNumber</span>
<span class="dash-box-title">sometitle</span>
</div>
<div class="dash-box-action">
<button>More Info</button>
</div>
</div>
</div>
<div class="col-md-4">
<div class="dash-box dash-box-color-2">
<div class="dash-box-icon">
<i class="glyphicon glyphicon-download"></i>
</div>
<div class="dash-box-body">
<span class="dash-box-count">someNumber</span>
<span class="dash-box-title">someTitle</span>
</div>
<div class="dash-box-action">
<button>More Info</button>
</div>
</div>
</div>
</div>
</div>