我知道可以使用 imagecreatefromjpeg()、imagecreatefrompng() 等,将 URL 作为“文件名”与 fopen(),但由于安全问题,我无法启用包装器。有没有办法在不启用它们的情况下将 URL 传递给 imagecreatefromX()?
我也尝试过使用 cURL,但这也给我带来了问题:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.../image31.jpg"); //Actually complete URL to image
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$image = imagecreatefromstring($data);
var_dump($image);
imagepng($image);
imagedestroy($image);
最佳答案
您可以使用 cURL 下载文件,然后将结果通过管道传输到 imagecreatefromstring
。
例子:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // good edit, thanks!
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // also, this seems wise considering output is image.
$data = curl_exec($ch);
curl_close($ch);
$image = imagecreatefromstring($data);
关于php - 我可以在不启用 fopen 包装器的情况下使用 URL 作为 imagecreatefromjpeg() 的来源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12337639/