Home > Article > Backend Development > A method to get remote images in php
Requirement: You need to use the avatar of your personal WeChat, Weibo or QQ account as the avatar of the application. These avatars may expire or be unavailable, so you need to download these images locally first, and then upload them to the Qiniu picture server. Then quote the picture address of Qiniu.
The following is how it is written in codeIgniter:
/**
* Information page
*/
class Test extends Wap_base
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* Take out the third-party login avatar and put it on the Qiniu server and return the new url
* @param string $url
* @return array
*/
private function uploadOneImage($url)
{
$this->load->library('qiniu/upfile', '', 'upfile');
$pathInfo = pathinfo($url);
$imageInfo = ! empty($url) ? getimagesize($url) : array(
'mime' => 'application/unkown'
);
$uploadError = '';
if (! in_array($imageInfo['mime'], explode(',', FILE_TYPES))) {
$uploadError = "上传文件【{$pathInfo['basename']}】格式不正确!";
}
if(strpos($imageInfo['mime'],'jpeg') !== FALSE) {
if(strpos($pathInfo['basename'],'jpg') === FALSE) {
$filename = '/tmp/'.$pathInfo['basename'].'.jpg';
}
}elseif(strpos($imageInfo['mime'],'gif') !== FALSE) {
if(strpos($pathInfo['basename'],'gif') === FALSE) {
$filename = '/tmp/'.$pathInfo['basename'].'.gif';
}
}elseif(strpos($imageInfo['mime'],'png') !== FALSE) {
if(strpos($pathInfo['basename'],'png') === FALSE) {
$filename = '/tmp/'.$pathInfo['basename'].'.png';
}
}elseif(strpos($imageInfo['mime'],'bmp') !== FALSE) {
if(strpos($pathInfo['basename'],'bmp') === FALSE) {
$filename = '/tmp/'.$pathInfo['basename'].'.bmp';
}
}
//curl获取图片并写入到本地文件中
$fp = fopen($filename,'wb');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FILE,$fp);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_TIMEOUT,5);
curl_exec($ch);
fclose($fp);
curl_close($ch);
$fileurl = '';
if (empty($filename)) {
$uploadError = "上传文件失败,临时文件名为空!";
}
if (filesize($filename) > MAX_FILE_SIZE * 1024 * 1024) {
$uploadError = "上传文件【$filename】大小超过限制!";
} else {
$basename = basename($filename);
$uploadResult = $this->upfile->upload(QINIU_UPLOAD_BUCKET, $basename, $filename, 0);
//var_dump($uploadResult);
$fileurl = isset($uploadResult['smallimageurl']) && $uploadResult['smallimageurl'] != '' ? $uploadResult['smallimageurl'] : '';
}
if (! empty($uploadError)) {
return ['result'=>FALSE,'fileurl'=>$fileurl,'error'=>$uploadError];
}else {
return ['result'=>TRUE,'fileurl'=>$fileurl,'error'=>$uploadError];
}
}
public function index()
{
$url = 'http://wx.qlogo.cn/mmopen/bv5hAQW59rmliaaIuGuSLQbcIAumib0rCribkBKqNKhrqzSTrnjrhprldIPKyRu2JZdW9S6qFK4NEpibcUDWbsXUUlEWUQ4eFkcm/0';
//微信中的图片链接地址不是直接以图片的形式提供的,所以上传到七牛服务器上时要保证这些文件名是合法的。0肯定是不行的。
$begin = microtime(TRUE);
$result = $this->uploadOneImage($url);
$during = (microtime(TRUE) - $begin );
echo '执行该方法所用的时间为:'.$during.'
';
}
}
}
调用地址:http://localhost/wap/test/index
以上就介绍了php 获取远程图片的一个方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。