Example #1
0
 /**
  * 生成特定尺寸缩略图 解决原版缩略图不能满足特定尺寸的问题 PS:会裁掉图片不符合缩略图比例的部分
  * @static
  * @access public
  * @param string $image  原图
  * @param string $type 图像格式
  * @param string $thumbname 缩略图文件名
  * @param string $maxWidth  宽度
  * @param string $maxHeight  高度
  * @param boolean $interlace 启用隔行扫描
  * @return void
  */
 static function thumb2($image, $thumbname, $type = '', $maxWidth = 200, $maxHeight = 50, $interlace = true)
 {
     // 获取原图信息
     $info = Yfimage::getImageInfo($image);
     if ($info !== false) {
         $srcWidth = $info['width'];
         $srcHeight = $info['height'];
         $type = empty($type) ? $info['type'] : $type;
         $type = strtolower($type);
         $interlace = $interlace ? 1 : 0;
         unset($info);
         $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight);
         // 计算缩放比例 宽度比高度的比例
         //判断原图和缩略图比例 如原图宽于缩略图则裁掉两边 反之..
         if ($maxWidth / $srcWidth > $maxHeight / $srcHeight) {
             //高于
             $srcX = 0;
             $srcY = ($srcHeight - $maxHeight / $scale) / 2;
             $cutWidth = $srcWidth;
             $cutHeight = $maxHeight / $scale;
         } else {
             //宽于
             $srcX = ($srcWidth - $maxWidth / $scale) / 2;
             $srcY = 0;
             $cutWidth = $maxWidth / $scale;
             $cutHeight = $srcHeight;
         }
         // 载入原图
         $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
         $srcImg = $createFun($image);
         //创建缩略图
         if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
             $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
         } else {
             $thumbImg = imagecreate($maxWidth, $maxHeight);
         }
         // 复制图片
         if (function_exists("ImageCopyResampled")) {
             imagecopyresampled($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
         } else {
             imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
         }
         if ('gif' == $type || 'png' == $type) {
             //imagealphablending($thumbImg, false);//取消默认的混色模式
             //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
             $background_color = imagecolorallocate($thumbImg, 0, 255, 0);
             //  指派一个绿色
             imagecolortransparent($thumbImg, $background_color);
             //  设置为透明色,若注释掉该行则输出绿色的图
         }
         // 对jpeg图形设置隔行扫描
         if ('jpg' == $type || 'jpeg' == $type) {
             imageinterlace($thumbImg, $interlace);
         }
         // 生成图片
         $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
         if ($type == 'png') {
             $imageFun($thumbImg, $thumbname, 0);
             //png 0-9
         } else {
             $imageFun($thumbImg, $thumbname, 100);
             //jpg 100-0
         }
         imagedestroy($thumbImg);
         imagedestroy($srcImg);
         return $thumbname;
     }
     return false;
 }
Example #2
0
 /**
  * 上传一个文件
  * @access public
  * @param mixed $name 数据
  * @param string $value  数据表名
  * @return string
  */
 private function save($file)
 {
     $filename = $file['savepath'] . $file['savename'];
     if (!$this->uploadReplace && is_file($filename)) {
         // 不覆盖同名文件
         $this->error = '文件已经存在!' . $filename;
         return false;
     }
     // 如果是图像文件 检测文件格式
     if (in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'))) {
         $info = getimagesize($file['tmp_name']);
         if (false === $info) {
             $this->error = '非法图像文件';
             return false;
         }
     }
     if (!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename, 'utf-8', 'gbk'))) {
         $this->error = '文件上传保存错误!';
         return false;
     }
     if ($this->thumb && in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png'))) {
         $image = getimagesize($filename);
         if (false !== $image) {
             //是图像文件生成缩略图
             $thumbWidth = explode(',', $this->thumbMaxWidth);
             $thumbHeight = explode(',', $this->thumbMaxHeight);
             $thumbPrefix = explode(',', $this->thumbPrefix);
             $thumbSuffix = explode(',', $this->thumbSuffix);
             $thumbFile = explode(',', $this->thumbFile);
             $thumbPath = $this->thumbPath ? $this->thumbPath : $file['savepath'];
             $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension'];
             //自定义缩略图扩展名
             // 生成图像缩略图
             import($this->imageClassPath);
             for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) {
                 if (!empty($thumbFile[$i])) {
                     $thumbname = $thumbFile[$i];
                 } else {
                     $prefix = isset($thumbPrefix[$i]) ? $thumbPrefix[$i] : $thumbPrefix[0];
                     $suffix = isset($thumbSuffix[$i]) ? $thumbSuffix[$i] : $thumbSuffix[0];
                     $thumbname = $prefix . basename($filename, '.' . $file['extension']) . $suffix;
                 }
                 if (1 == cookie('thumbType')) {
                     /*if ($image[0] < 600 || $image[1]<600) {
                           $this->error = '作品图片宽度与高度必须大于600像素';
                           return false;
                       }*/
                     Yfimage::thumbMain($filename, $thumbPath . $thumbname . '.' . $thumbExt, '', $thumbWidth[$i], $thumbHeight[$i], true);
                     cookie('thumbType', null);
                 } else {
                     Yfimage::thumb2($filename, $thumbPath . $thumbname . '.' . $thumbExt, '', $thumbWidth[$i], $thumbHeight[$i], true);
                     cookie('thumbType', null);
                 }
             }
             if ($this->thumbRemoveOrigin) {
                 // 生成缩略图之后删除原图
                 unlink($filename);
             }
         }
     }
     if ($this->zipImags) {
         // ODO 对图片压缩包在线解压
     }
     return true;
 }