【发布时间】:2019-09-02 07:10:32
【问题描述】:
我正在 Laravel 5.7 中制作应用程序。我想通过它上传数据库中的图像,我想从数据库中显示它。
当我遇到问题时,我在互联网上尝试了不同的方法
Intervention\Image\Facades\Image
我听从了互联网上的许多建议,在 config.app 中进行了更改 在 Composer
中进行了更改最后用了
use Intervention\Image\Facades\Image as Image;
所以我从问题“未定义的类图像”中得到解决 但现在我遇到了“未定义的类文件”的问题, 找不到方法 getClientOriginalExtension。 方法升迁,找不到。
我的代码是
<?php
namespace App\Http\Controllers;
use File;
use Intervention\Image\Facades\Image as Image;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
//
protected $user;
/**
* [__construct description]
* @param Photo $photo [description]
*/
public function __construct(
User $user )
{
$this->user = $user;
}
/**
* Display photo input and recent images
* @return view [description]
*/
public function index()
{
$users = User::all();
return view('profile', compact('users'));
}
public function uploadImage(Request $request)
{
$request->validate([
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
//check if image exist
if ($request->hasFile('image')) {
$images = $request->file('image');
//setting flag for condition
$org_img = $thm_img = true;
// create new directory for uploading image if doesn't exist
if( ! File::exists('images/originals/')) {
$org_img = File::makeDirectory('images/originals/', 0777, true);
}
if ( ! File::exists('images/thumbnails/')) {
$thm_img = File::makeDirectory('images/thumbnails', 0777, true);
}
// loop through each image to save and upload
foreach($images as $key => $image) {
//create new instance of Photo class
$newPhoto = new $this->user;
//get file name of image and concatenate with 4 random integer for unique
$filename = rand(1111,9999).time().'.'.$image->getClientOriginalExtension();
//path of image for upload
$org_path = 'images/originals/' . $filename;
$thm_path = 'images/thumbnails/' . $filename;
$newPhoto->image = 'images/originals/'.$filename;
$newPhoto->thumbnail = 'images/thumbnails/'.$filename;
//don't upload file when unable to save name to database
if ( ! $newPhoto->save()) {
return false;
}
// upload image to server
if (($org_img && $thm_img) == true) {
Image::make($image)->fit(900, 500, function ($constraint) {
$constraint->upsize();
})->save($org_path);
Image::make($image)->fit(270, 160, function ($constraint) {
$constraint->upsize();
})->save($thm_path);
}
}
}
return redirect()->action('UserController@index');
}
}
请在不更新存储库的情况下向我建议任何图像上传代码,或者建议我如何从该代码中删除问题。
【问题讨论】:
-
我要上传图片到数据库 - 意思是你想把
blob存到数据库里,还是把path/to/your/image.jpg存到数据库里? -
图像到数据库...
-
你不明白我的意思。您可以存储图像文件的路径或将图像存储为二进制对象
标签: php database laravel image laravel-5