我正在使用PHP中的cURL从远程服务器保存和调整大量图像的大小,但之前的开发人员没有实现任何类型的系统来确保用户上传的图片"安全",所以用户可以上传任何类型的文件格式和任何类型的名称,然后上传的文件名会按原样保存到数据库中,所以很多文件都有非URL安全和iso-8859-8字符。例如:
gaght101125659_5 Gn-eŐs mtó gÁrlós.jpg
根据这个答案,我制作了一个获取图片的代码。
private function getRemoteUrl($file)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$img = imagecreatefromstring($this->getRemoteUrl($url));//$url contains the plain url of the image.
此代码非常适用于名称类似gaht123115516_2.jpg
的图像,但对于上面显示的示例,它给出了一个错误:
Warning: imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format in /home/test/public_html/resizing.php on line 64
当然了,因为这些花哨的人物。那么,我应该在getRemoteUrl($file)
中使用$file
变量做些什么才能使其工作呢?这可以用PHP完成吗?如果没有,我应该尝试其他哪些方法/编程语言?
对文件名使用urlencode()
。不要urlencode()
整个url。
URL对指定的文件名进行编码。