php保存网络图片,php laravel 图片保存

这是一个简单的图片保存功能,其中包括原图片和压缩图片两种,目录结构是/images/year/month/xxx.png和/images/thumbnail/year/month/xxx.png;具体上传结果如下图所示:

3bfa3496bad8

upload_icon.png

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Log;

use Illuminate\Support\Facades\Storage;

use Intervention\Image\ImageManagerStatic as Image;

public function uploadImage(Request $request)

{

$inputData = $request->all();

if ($request->hasFile('file')) {

try {

$year = date("Y");

$month = "/images/$year/" . date("m");

$directory = Storage::exists($month);

if (!$directory) {

Storage::makeDirectory($month);

}

$apath = "/images/thumbnail/$year/" . date("m");

$thumbnail = Storage::exists($apath);

if (!$thumbnail) {

Storage::makeDirectory($apath);

}

$carid = strtoupper(md5(uniqid(mt_rand(), true)));

$uuid = substr($carid, 0, 8) . substr($carid, 8, 4) . substr($carid, 12, 4) . substr($carid, 16, 4) . substr($carid, 20, 12);

$file = $request->file('file');

$extension = strtolower($file->getClientOriginalExtension());

$photo = $inputData['file'];

$file_name = $uuid . "." . $extension;

$file_relative_path = 'storage/' . $month;

$file_path = public_path($file_relative_path);

if (!is_dir($file_path)) {

mkdir($file_path);

}

$relative_path = 'storage/' . $apath;

$thumbnail_path = public_path($relative_path);

$thumbnail_file_path = $thumbnail_path . '/' . $file_name;

$image = Image::make($photo)->resize(150, 150, function ($constraint) {

$constraint->aspectRatio();

})->save($thumbnail_file_path);

$file_path .= '/' . $file_name;

$image = Image::make($photo)->save($file_path);

$path = $month . "/" . $file_name;

if ($path) {

return ['success' => true, 'path' => $path];

} else {

return ['errors' => ['file' => 'uploaderror']];

}

} catch (\Exception $e) {

Log::error($e);

return ['errors' => ['file' => $e->getMessage()]];

}

} else {

return ['success' => false];

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel中上传图片并回显,你可以按照以下步骤进行操作: 1. 在表单中添加文件上传字段: ```html <form method="POST" action="/upload" enctype="multipart/form-data"> @csrf <input type="file" name="image"> <button type="submit">上传图片</button> </form> ``` 2. 在路由中定义上传图片的处理逻辑: ```php use Illuminate\Http\Request; Route::post('/upload', function (Request $request) { if ($request->hasFile('image')) { $path = $request->file('image')->store('public/images'); // 存储的路径为 "storage/images/filename" // 如果你想要访问图片,可以将路径存储到数据库,并使用 Storage::url() 获取完整路径 // 也可以直接拼接 URL,例如:$url = '/storage/images/' . $request->file('image')->hashName(); return redirect()->back()->with('success', '图片上传成功'); } return redirect()->back()->with('error', '请选择要上传的图片'); }); ``` 3. 在视图中回显上传的图片: ```html @if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif @if(session('error')) <div class="alert alert-danger">{{ session('error') }}</div> @endif @if(isset($url)) <img src="{{ $url }}" alt="Uploaded Image"> @endif ``` 上述代码中,`$url` 是存储图片路径的变量,可以在控制器中把它传递给视图。使用`$url`作为图片的`src`属性值,图片就会回显在页面上。 请确保在进行文件上传时,已经配置好了Laravel的文件存储系统,并且有合适的权限以保存和访问上传的图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值