 |
Laravel 7 Image source not readable รบกวนช่วยดูหน่อยว่าพลาดตรงไหน |
|
 |
|
|
 |
 |
|
รบกวนช่วยดูหน่อยว่าพลาดตรงไหน ตอนกดปุ่ม บันทึก มันขึ้นว่า
Error Code
Intervention\Image\Exception\NotReadableException
Image source not readable
http://localhost:8000/books
View Source
Code (PHP)
<form method="POST" action="http://localhost:8000/books" accept-charset="UTF-8" class="form" enctype="multipart/form-data"><input name="_token" type="hidden" value="89K3jJgliuN1rZVitMyAIdol3RpXqlQesID1MM4E">
Create
Code (PHP)
<div class="card-body">
{!!Form::open(array('url'=> 'books','class'=>'form', 'method' => 'post', 'files'=> true))!!}
<div class="form-group">
<?php echo Form::label('name_book','ชื่อหนังสือ');?>
<?php echo Form::text('name_book',null,['class'=>'form-control','placeholder'=>'ชื่อหนังสือ']);?>
</div>
<div class = "form-group">
{!! Form::label('price','ราคา'); !!}
{!! Form::text('price',null,['class' => 'form-control','placeholder' => 'เช่น 100,100.50']);!!}
</div>
<div class="form-group">
{!! Form::label('image','รูปภาพ');!!}
<?php echo Form::file('image',null,['class' =>'form-control']); ?>
</div>
<div class="form-group">
<?php echo Form::submit('บันทึก',['class'=>'btn btn-primary']); ?>
</div>
@if(count($errors)> 0)
<div class="alert alert-warning">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error}}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::close()!!}
</div>
Controller
Code (PHP)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Books; //เรียก Model เข้ามาใช้งาน
use App\Http\Requests\StoreBooksRequest;
use Image; //เรียกใช้ Library จัดการรูปภาพเข้ามาใช้งาน
use Illuminate\Support\Str;//นำ Helpers String เข้ามาใช้งาน
class BooksController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$books = Books::with('typebooks')->orderBy('id','desc')->paginate(3);
return view('books/index',['books' => $books]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('books.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(StoreBooksRequest $request)
{
$book = new books();
$book->name_book = $request->name_book;
$book->price = $request->price;
$book->typebooks_id = $request->typebooks_id;
//upload image
if($request->hasFile('image')){
$filename = Str::random(10).'.'.$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(public_path().'/images'.$filename);
Image::make(public_path().'/images/'.$filename)->resize(50,50)->save(public_path().'/images/resize/'.$filename);
$book->image = $filename;
}else{
$book->image = 'nopic.jpg';
}
//$book->save();
return redirect()->action('BooksController@index');
}
}
Route
Code (PHP)
//Books ตั้งชื่อ Method index ว่า books
Route::resource('books','BooksController')->name('index','books');
Tag : PHP, Laravel Framework
|
|
 |
 |
 |
 |
Date :
2020-04-10 15:03:16 |
By :
nottpoo |
View :
1415 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเป็น package ของ Intervention\Image ทำตามนี้ครับ
Controller
use Image; // ประกาศ Image ลงทะเบียนไว้ส่วนหัวบนสุดของ Controller
$avatar=$request->file('avatar');
if($avatar)
{
$w=240;
$h=240;
$rename=date('YmdHis').'-'.$avatar->getClientOriginalName();
$public_path='images/avatar/'.$rename;
$destination=base_path().'/public/'.$public_path; // ถ้าต้องการให้รูปอยู่ใน public ใช้เป็น base_path() แทนไม่งั้นมันจะหา path ไม่เจอ
Image::make($avatar->getRealPath())->resize($w,$h)->save($destination); // ชี้ด้วยว่า image นั้นมากจาก RealPath ไหน ค่อยทำการ Resize
$user->avatar=$public_path; // นำข้อมูลที่ได้ tinker ลงใน Field ค่อย Save
}
Remark : ข้อดีของ Tinker ไม่ต้องทำ fillable ใน Model
Resources/View
<img src="{{ asset(.....) }}">
|
 |
 |
 |
 |
Date :
2020-04-13 12:02:55 |
By :
Genesis™ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|