Exemplo n.º 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;
 }
Exemplo n.º 2
0
 public static function buildUrl($path, $baseUrl = '', $basePath = '', $hostUrl = '')
 {
     $path = str_replace('\\', '/', trim($path));
     //是完整的url路径
     if (UrlHelper::isUrl($path)) {
         return $path;
         // 是url绝对路径
     } else {
         if ($path[0] == '/') {
             return $hostUrl ? $hostUrl . $path : $path;
         }
     }
     // 相对路径
     $baseUrl = $baseUrl ? rtrim($baseUrl, '/') . '/' : '/';
     if ($basePath) {
         $basePath = rtrim($basePath, '/') . '/';
         if (!file_exists($basePath . $path)) {
             throw new \RuntimeException('资源 [' . $basePath . $path . '] 不存在 !!');
         }
     }
     return ($hostUrl ? $hostUrl . '/' : $baseUrl) . $path;
 }
Exemplo n.º 3
0
 /**
  * Sets the 资源基础URL
  * @param string $baseUrl
  * @return $this
  */
 public function setBaseUrl($baseUrl)
 {
     if ($baseUrl) {
         if (UrlHelper::isUrl($baseUrl)) {
             $this->baseUrl = rtrim($baseUrl, '/') . '/';
         } else {
             $this->baseUrl = rtrim($baseUrl, '/') . '/';
         }
     }
     return $this;
 }