<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://stagingadmin.zenpepper.com/image/5b1a35e54a77da5969f1f98d?token=HIDDEN",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Postman-Token: HIDDEN",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}我有一个api调用,它将图像作为文本返回,该图像可以转换为图像。但是我想在laravel中用正确的扩展名保存该图像
目前我有以下代码,但我想尝试一个更好的解决方案。
if (!file_exists(public_path() . '/images/' . $image_id . '.png')) {
if (strpos($response, 'PNG') !== false || strpos($response, 'png') !== false) {
Image::make($response)->save(public_path('images/' . $image_id . ".png"));
}
}
if (!file_exists(public_path() . '/images/' . $image_id . '.jpg')) {
if (strpos($response, 'jpg') !== false || strpos($response, 'JPG') !== false) {
Image::make($response)->save(public_path('images/' . $image_id . ".jpg"));
}
}发布于 2019-02-06 18:06:16
在Laravel中有一个更好的保存文件的方法。
if($response)
{
$path = 'images/' . $image_id;
$fileName = str_replace(' ', '_', response->getClientOriginalName());
$response->storeAs($path,$fileName);
}发布于 2019-02-06 16:05:22
我认为在Laravel中使用文件存储驱动是一种更好的方式。https://laravel.com/docs/5.7/filesystem
对于确定类型的文件,可以读取MIME类型。
https://stackoverflow.com/questions/54548588
复制相似问题