Ejemplo 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;
 }
Ejemplo n.º 2
0
 /**
  * @param string $url
  * @param string $saveFile If is empty, will save to old file dir.
  * @param string $webPath
  * @return string
  * @throws FileNotFoundException
  * @throws FileSystemException
  */
 public function compressAndSave($url, $saveFile = '', $webPath = '')
 {
     // is full url, have http ...
     if (!UrlHelper::isRelative($url)) {
         return $url;
     }
     $oldKey = null;
     $basePath = $this->getBasePath();
     $sourceFile = $basePath . '/' . $url;
     if (!is_file($sourceFile)) {
         throw new FileNotFoundException("File [{$sourceFile}] don't exists!!");
     }
     // maybe need create directory.
     if ($saveFile) {
         $saveDir = dirname($saveFile);
         // create path.
         if (!Directory::create($saveDir)) {
             throw new FileSystemException("Create dir path [{$saveDir}] failure!!");
         }
     } else {
         $saveFile = substr($sourceFile, 0, -strlen($this->assetType)) . 'min.' . $this->assetType;
     }
     // check target file exists
     if (file_exists($saveFile)) {
         $oldKey = md5(file_get_contents($saveFile));
     }
     if ($this->assetType === self::TYPE_CSS) {
         $minifier = new Minify\CSS($sourceFile);
     } else {
         $minifier = new Minify\JS($sourceFile);
     }
     // check file content has changed.
     if ($oldKey) {
         $newContent = $minifier->minify();
         $newKey = md5($newContent);
         if ($newKey !== $oldKey) {
             File::write($newContent, $saveFile);
         }
     } else {
         $minifier->minify($saveFile);
     }
     return $this->baseUrl . str_replace($webPath ?: $basePath, '', $saveFile);
 }
Ejemplo n.º 3
0
 /**
  * 获得目录下的文件以及详细信息,可选择类型、是否遍历子文件夹
  * @param $dirName string 目标目录
  * @param $ext array('css','html','php') css|html|php
  * @param $recursive int|bool 是否包含子目录
  * @param array $list
  * @return array
  * @throws NotFoundException
  */
 public static function getFilesInfo($dirName, $ext = null, $recursive = 0, &$list = [])
 {
     $dirName = self::pathFormat($dirName);
     if (!is_dir($dirName)) {
         throw new NotFoundException('目录' . $dirName . ' 不存在!');
     }
     $ext = is_array($ext) ? implode('|', $ext) : trim($ext);
     static $id = 0;
     //glob()寻找与模式匹配的文件路径
     foreach (glob($dirName . '*') as $file) {
         $id++;
         // 匹配文件 如果没有传入$ext 则全部遍历,传入了则按传入的类型来查找
         if (is_file($file) && (!$ext || preg_match("/\\.({$ext})\$/i", $file))) {
             $list[$id] = File::getInfo($file);
             //是否遍历子目录
         } elseif ($recursive) {
             $list = self::getFilesInfo($file, $ext, $recursive, $list);
         }
     }
     return $list;
 }
Ejemplo n.º 4
0
 /**
  * @param string $from The is full file path
  * @param string $to  The is a relative path
  * @param bool|false $override
  */
 public function publishFile($from, $to, $override = false)
 {
     $targetFile = Directory::isAbsPath($to) ? $to : $this->publishPath . '/' . $to;
     //$targetFile = $to . '/' . basename($from);
     if (!file_exists($targetFile) || $override) {
         if (!Directory::create(dirname($targetFile), 0775)) {
             throw new FileSystemException('Create dir path [' . dirname($targetFile) . '] failure.');
         }
         File::copy($from, $targetFile);
         $this->publishedAssets['created'][] = $targetFile;
     } else {
         $this->publishedAssets['skipped'][] = $targetFile;
     }
 }