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 string $img         操作的图片文件路径(原图)
  * @param string $outPath     文件存放路径
  * @param string $outFilename 另存文件名
  * @param string $thumbWidth  缩略图宽度
  * @param string $thumbHeight 缩略图高度
  * @param string $thumbType   裁切图片的方式
  * @return static
  */
 public function thumbnail($img, $outPath = '', $outFilename = '', $thumbWidth = '', $thumbHeight = '', $thumbType = '')
 {
     if (!$this->_checkImage($img) || $this->hasError()) {
         return $this;
     }
     $imgInfo = pathinfo($img);
     $rawImgType = $imgInfo['extension'];
     //基础配置
     $thumbType = $thumbType ?: $this->thumbOptions['type'];
     $thumbWidth = $thumbWidth ?: $this->thumbOptions['width'];
     $thumbHeight = $thumbHeight ?: $this->thumbOptions['height'];
     $outPath = $outPath ?: ($this->thumbOptions['path'] ?: dirname($img));
     //获得图像信息
     list($imgWidth, $imgHeight) = getimagesize($img);
     $imgType = $this->_handleImageType($rawImgType);
     //获得相关尺寸
     $thumbSize = $this->_calcThumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight, $thumbType);
     //原始图像资源
     // imagecreatefromgif() imagecreatefrompng() imagecreatefromjpeg() imagecreatefromwbmp()
     $resImg = call_user_func("imagecreatefrom{$imgType}", $img);
     //缩略图的资源
     if ($imgType === static::IMAGE_GIF) {
         $resThumb = imagecreate($thumbSize[0], $thumbSize[1]);
         $color = imagecolorallocate($resThumb, 255, 0, 0);
         imagecolortransparent($resThumb, $color);
         //处理透明色
     } else {
         $resThumb = imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
         imagealphablending($resThumb, false);
         //关闭混色
         imagesavealpha($resThumb, true);
         //储存透明通道
     }
     // 绘制缩略图X
     if (function_exists('imagecopyresampled')) {
         imagecopyresampled($resThumb, $resImg, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $thumbSize[2], $thumbSize[3]);
     } else {
         imagecopyresized($resThumb, $resImg, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $thumbSize[2], $thumbSize[3]);
     }
     //配置输出文件名
     $outFilename = $outFilename ?: $this->thumbOptions['prefix'] . $imgInfo['filename'] . $this->thumbOptions['suffix'] . '.' . $rawImgType;
     $outFile = $outPath . DIRECTORY_SEPARATOR . $outFilename;
     if (!Directory::create($outPath)) {
         $this->_error = 'Failed to create the output directory path!. OUT-PATH: ' . $outPath;
         return $this;
     }
     // generate image to dst file. imagepng(), imagegif(), imagejpeg(), imagewbmp()
     call_user_func("image{$imgType}", $resThumb, $outFile);
     if (isset($resImg)) {
         imagedestroy($resImg);
     }
     if (isset($resThumb)) {
         imagedestroy($resThumb);
     }
     $this->working = ['raw' => $img, 'out' => $outFile];
     $this->_result['workThumb']['rawFile'] = $img;
     $this->_result['workThumb']['outFile'] = $outFile;
     return $this;
 }
Ejemplo n.º 4
0
 /**
  * 合并编译多个文件
  * @param $fileArr
  * @param $outFile
  * @param  boolean $deleteSpace [description]
  * @return void [type]               [description]
  */
 public static function margePhp($fileArr, $outFile, $deleteSpace = true)
 {
     $savePath = dirname($outFile);
     if (!is_dir($savePath)) {
         Directory::create($savePath);
     }
     if (!is_array($fileArr)) {
         $fileArr = array($fileArr);
     }
     $data = '';
     foreach ($fileArr as $v) {
         #删除注释、空白
         if ($deleteSpace) {
             $data .= StrHelper::deleteStripSpace($v);
         } else {
             $o_data = file_get_contents($v);
             $o_data = substr($o_data, 0, 5) == "<?php" ? substr($o_data, 5) : $o_data;
             $data .= substr($o_data, -2) == "?>" ? substr($o_data, 0, -2) : $o_data;
         }
     }
     $data = "<?php " . $data . "?>";
     file_put_contents($outFile, $data);
 }
Ejemplo n.º 5
0
 /**
  * @param $fromDir
  * @param $toDir
  * @param bool|false $override
  */
 public function publishDir($fromDir, $toDir, $override = false)
 {
     $files = $this->finder->findAll(1, $fromDir)->getFiles();
     $toDir = Directory::isAbsPath($toDir) ? $toDir : $this->publishPath . '/' . $toDir;
     // publish files ...
     foreach ($files as $file) {
         $this->publishFile($fromDir . '/' . $file, $toDir . '/' . $file, $override);
     }
 }