Example #1
0
 /**
  * @param string $imgUrl image url e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg
  * @param string $savePath 图片保存路径
  * @param string $rename 图片重命名(只写名称,不用后缀) 为空则使用原名称
  * @return string
  */
 public static function fetchImg($imgUrl, $savePath, $rename = '')
 {
     // e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg?t=34512323
     if (strpos($imgUrl, '?')) {
         list($real, ) = explode('?', $imgUrl, 2);
     } else {
         $real = $imgUrl;
     }
     $last = trim(strrchr($real, '/'), '/');
     // special url e.g http://img.blog.csdn.net/20150929103749499
     if (false === strpos($last, '.')) {
         $suffix = '.jpg';
         $name = $rename ?: $last;
     } else {
         $suffix = File::getSuffix($real) ?: '.jpg';
         $name = $rename ?: File::getName($real, 1);
     }
     $imgFile = $savePath . '/' . $name . $suffix;
     if (file_exists($imgFile)) {
         return $imgFile;
     }
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, UrlHelper::encode2($imgUrl));
     // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     // 伪造网页来源地址,伪造来自百度的表单提交
     curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');
     $imgData = self::execute($ch);
     Directory::create($savePath);
     file_put_contents($imgFile, $imgData);
     return $imgFile;
 }