Cml\Vendor\Image::makeThumb PHP Метод

makeThumb() публичный статический Метод

生成缩略图
public static makeThumb ( string $image, string $thumbName, null $type = null, integer $width = 100, integer $height = 50, boolean $isAutoFix = true ) : false | string
$image string 要缩略的图
$thumbName string 生成的缩略图的路径
$type null 要生成的图片类型 默认跟原图一样
$width integer 缩略图的宽度
$height integer 缩略图的高度
$isAutoFix boolean 是否按比例缩放
Результат false | string
    public static function makeThumb($image, $thumbName, $type = null, $width = 100, $height = 50, $isAutoFix = true)
    {
        is_dir(dirname($thumbName)) || mkdir(dirname($thumbName), 0700, true);
        $imageInfo = self::getImageInfo($image);
        if (!$imageInfo) {
            return false;
        }
        $type = is_null($type) ? strtolower($imageInfo['ext']) : strtolower($type == 'jpg' ? 'jpeg' : $type);
        if ($isAutoFix) {
            //根据比例缩放
            $fixScale = min($width / $imageInfo['width'], $height / $imageInfo['height']);
            //缩放的比例
            if ($fixScale > 1) {
                //缩略图超过原图大小
                $width = $imageInfo['width'];
                $height = $imageInfo['height'];
            } else {
                $width = $imageInfo['width'] * $fixScale;
                $height = $imageInfo['height'] * $fixScale;
            }
        } else {
            $width > $imageInfo['width'] && ($width = $imageInfo['width']);
            $height > $imageInfo['height'] && ($height = $imageInfo['height']);
        }
        if (!in_array($type, ['jpeg', 'gif', 'png'])) {
            return false;
        }
        $createImageFunc = "imagecreatefrom{$type}";
        $sourceCreateImage = $createImageFunc($image);
        //载入原图
        //生成缩略图
        if ($type == 'gif' || !function_exists('imagecreatetruecolor')) {
            $thumbImg = imagecreate($width, $height);
        } else {
            $thumbImg = imagecreatetruecolor($width, $height);
        }
        if ($type == 'png') {
            imagealphablending($thumbImg, false);
            //取消默认的混色模式(为解决阴影为绿色的问题)
            imagesavealpha($thumbImg, true);
            //设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)
        } elseif ($type == 'gif') {
            $transparentIndex = imagecolortransparent($sourceCreateImage);
            if ($transparentIndex >= 0) {
                $transparentColor = imagecolorsforindex($sourceCreateImage, $transparentIndex);
                $transparentIndex = imagecolorallocate($thumbImg, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
                imagefill($thumbImg, 0, 0, $transparentIndex);
                imagecolortransparent($thumbImg, $transparentIndex);
            }
        }
        //复制
        function_exists('imagecopyresampled') ? imagecopyresampled($thumbImg, $sourceCreateImage, 0, 0, 0, 0, $width, $height, $imageInfo['width'], $imageInfo['height']) : imagecopyresized($thumbImg, $sourceCreateImage, 0, 0, 0, 0, $width, $height, $imageInfo['width'], $imageInfo['height']);
        self::output($thumbImg, $type, $thumbName);
        imagedestroy($sourceCreateImage);
        return $thumbName;
    }

Usage Example

Пример #1
0
 /**
  * 保存
  *
  * @param array $file
  *
  * @return bool
  */
 private function save($file)
 {
     $filename = $file['savepath'] . $file['savename'];
     if (!$this->config['replace'] && is_file($filename)) {
         //不覆盖同名文件
         $this->errorInfo = "文件已经存在{$filename}";
         return false;
     }
     //如果是图片,检查格式
     if (in_array(strtolower($file['extension']), ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && false === getimagesize($file['tmp_name'])) {
         $this->errorInfo = '非法图像文件';
         return false;
     }
     if (!move_uploaded_file($file['tmp_name'], $filename)) {
         $this->errorInfo = '文件上传错误!';
         return false;
     }
     if ($this->config['thumb'] && in_array(strtolower($file['extension']), ['gif', 'jpg', 'jpeg', 'bmp', 'png'])) {
         if ($image = getimagesize($filename)) {
             //生成缩略图
             $thumbPath = $this->config['thumbPath'] ? $this->config['thumbPath'] : dirname($filename);
             $thunbName = $this->config['thumbFile'] ? $this->config['thumbFile'] : $this->config['thumbPrefix'] . basename($filename);
             Image::makeThumb($filename, $thumbPath . '/' . $thunbName, null, $this->config['thumbMaxWidth'], $this->config['thumbMaxHeight']);
         }
     }
     return true;
 }