【发布时间】:2019-06-30 02:36:47
【问题描述】:
<?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"));
}
}
【问题讨论】:
标签: php laravel image api mime