Blade
<img src="https://i.imgur.com/Kd1LmNI.png" alt="IMG_2475.png" style="width: 120px;">
Controller
if ($prodImg) {
if ($product->images->first()) {
$product->images->first()->delete();
}
$client = new Client();
$imagePath = $prodImg->getPathname();
$response = $client->request('POST', 'https://api.imgur.com/3/image', [
'headers' => [
'Authorization' => 'Client-ID ' . env('IMGUR_CLIENT_ID'),
],
'multipart' => [
[
'name' => 'image',
'contents' => fopen($imagePath, 'r'),
],
],
]);
$body = json_decode($response->getBody()->getContents());
$imageUrl = $body->data->link;
$imgName = $prodImg->getClientOriginalName();
$imgExtension = $prodImg->getClientOriginalExtension();
$imgSize = $prodImg->getSize();
$newImgName = 'prod' . '_' . time() . uniqid() . '.' . strtolower($imgExtension);
$product->images()->create([
'name' => $newImgName,
'file_name' => $imgName,
'mime_type' => strtolower($imgExtension),
'size' => $imgSize,
'url' => $imageUrl,
]);
}
前端無法正常顯示圖片?
請問是哪裏出錯了嗎?
看不太懂你的問題
你提到的網址
https://i.imgur.com/Kd1LmNI.png
我直接點開來,圖片有正常顯示
代表你已用 imgur api 成功上傳圖片才對?