上传与的file_get_contents卷曲/ imagecreatefromstring

问题描述:

我有如下代码:上传与的file_get_contents卷曲/ imagecreatefromstring

$image = imagecreatefromstring(file_get_contents('http://externaldomain.com/image.png')); 

,并希望使卷曲要求:

$files = array(); 
$files[] = '@' . $image['tmp_name'] . ';filename=' . $image['name'] . ';type=' . $image['type']; 

$ch = curl_init('index.php?route=upload'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $files); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$response = curl_exec($ch); 

的主要问题是,如何让文件名和从$image变量的扩展名?

我可以用tempnam()代替$image['tmp_name'],但是其他的东西呢?

还有其他办法吗?

imagecreatefromstring返回一个GD句柄。没有与其关联的tmp_name,名称,类型。您试图将该GD句柄视为标准的基于POST的$ _FILES数据结构的结果。

GD句柄无法传递到卷曲上传,因为它不是一个真正的图像,直到你使用imagejpeg/imagepng/imagewhatever将它保存到一个文件,然后你可以指向卷曲,例如

$image = imagecreatefromstring(...); 
imagejpeg($image, 'tempfile.jpg'); 

$files[] = '@tempfile.jpg;filename=tempfile.jpg;type=image/jpeg'; 

curl...