2011-02-19 37 views
1

我想使用codeiogniter下载远程托管的图像。并且还必须添加验证功能,以便只下载图像。 是否可以使用CI提供的内置函数?在codeigniter中下载远程图像

谢谢。

回答

2

这个解决方案不是来自CI,而是PHP,认为它可以是有用的。 可能是检查的图像尺寸是足够的,以确保它是一个形象,喜欢

$imagedata = getimagesize('http://othersite.com/image.jpg'); 

或使用exif_imagetype

if (! function_exists('exif_imagetype')) { 
    function exif_imagetype ($filename) { 
     if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) { 
      return $type; 
     } 
    return false; 
    } 
} 


if(exif_imagetype('http://othersite.com/image.jpg')) 
{ 
//is an image 
} 
4

要获得远程图像,可以使用file_get_contents,假设在php.ini中将allow_url_fopen设置为on。

$image=file_get_contents('http://othersite.com/image.jpg'); 

一旦你有,你可以验证文件扩展名,或尝试resize the image检查,这确实是一个图像。