Laravel 9:将图像从字节转换为JPEG格式,从存储目录中

huangapple go评论65阅读模式
英文:

Laravel 9: Converting An Image From Bytes To Jpeg From Storage Directory

问题

I'm using Laravel 9 and I wanted to show an image which is stored at storage/app/avatars.

So I tried this at the Blade:

{{ \App\Http\HelperClasses\ImageHelper::admAvatar() }}

And this is the ImageHelper Class:

namespace App\Http\HelperClasses;

use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;

class ImageHelper
{
    public static function admAvatar()
    {
        $content = Storage::get('avatars/profile.png');

        return Response::make($content)->header('content-type','image/jpeg');
    }
}

But the problem is it does not show anything!

And when I dd(Response::make($content)->header('content-type','image/jpeg')), I get this:

Laravel 9:将图像从字节转换为JPEG格式,从存储目录中

And the result of dd($content) also goes like this:

Laravel 9:将图像从字节转换为JPEG格式,从存储目录中

So how can I convert this properly into an image?

英文:

I'm using Laravel 9 and I wanted to show an image which is stored at storage/app/avatars.

So I tried this at the Blade:

{{ \App\Http\HelperClasses\ImageHelper::admAvatar() }}

And this is the ImageHelper Class:

namespace App\Http\HelperClasses;

use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;

class ImageHelper
{
    public static function admAvatar()
    {
        $content = Storage::get('avatars/profile.png');

        return Response::make($content)->header('content-type','image/jpeg');
    }
}

So I tried making an image from the profile.png and return it after all.

But the problem is it does not show anything!

And when I dd(Response::make($content)->header('content-type','image/jpeg')), I get this:

Laravel 9:将图像从字节转换为JPEG格式,从存储目录中

And the result of dd($content) also goes like this:

Laravel 9:将图像从字节转换为JPEG格式,从存储目录中

So how can I convert this properly into an image?

答案1

得分: 1

Controller:

public function getFile($type, $id) {
    $attachment = Attachment::where([['parent_type', $type], ['parent_id', $id]])->latest()->firstOrFail();
    $headers = ['content-type' => 'image/jpeg'];
    $contents = Storage::get($attachment->file_path); 
    return response($contents, 200, $headers); 
}

Routes:

Route::get('/attachments/display/{type}/{id}', [App\Http\Controllers\AttachmentController::class, 'getFile']);

HTML:

<img src="/attachments/display/avatar/1" />
英文:

I did it like this

Controller:

public function getFile($type, $id) {
    $attachment = Attachment::where([[&#39;parent_type&#39;, $type], [&#39;parent_id&#39;, $id]])-&gt;latest()-&gt;firstOrFail();
    $headers = [&#39;content-type&#39; =&gt; &#39;image/jpeg&#39;];
    $contents = Storage::get($attachment-&gt;file_path); 
    return response($contents, 200, $headers); 
}

Routes:

Route::get(&#39;/attachments/display/{type}/{id}&#39;, [App\Http\Controllers\AttachmentController::class, &#39;getFile&#39;]);

HTML:

&lt;img src=&quot;/attachments/display/avatar/1&quot; /&gt;

huangapple
  • 本文由 发表于 2023年2月6日 18:34:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定