Home > php教程 > PHP源码 > 将文件流生成缩略图,并分发到fastDFS

将文件流生成缩略图,并分发到fastDFS

PHP中文网
Release: 2016-05-23 16:40:46
Original
1675 people have browsed it


 20, 'h' => 20),
        array('w' => 30, 'h' => 30),

    );

    private $allow_type = array(
        'image/png' => 'png',
        'image/jpeg' => 'jpg',
        'image/jpg' => 'jpg',
    );


    /**
     * 以二进制流方式接收文件上传
     * @param $buff 文件的二进制流
     * @return bool|string 成功返回源文件的访问地址,失败返回false
     */
    public function uploadImageByBuffOne($buff)
    {
        if (!$buff || ($buff > $this->max_size)) { // 当文件为空或者大于允许上传的上限
            return false;
        }

        $filename = $this->getFileNameFromBuff($buff); // 生成写入的文件名
        if (!$filename) {
            return false;
        }
        $file_path = $this->saveBuffToDisk($filename, $buff); // 保存文件
        if (!$file_path) {
            return false;
        }

        $files = $this->createThumb($file_path); // 生成文件的缩略图
        return $this->postImageToFastDFSByFileName($files);
    }

    /**
     * 将图片文件上传的fastDFS
     * @param $files 图片数组
     * @return bool|string 成功返回源文件的访问地址,失败返回false
     */
    private function postImageToFastDFSByFileName($files)
    {
        $source = $files['source'];
        unset($files['source']);

        $tracker = fastdfs_tracker_get_connection();
        if (!fastdfs_active_test($tracker)) {
            error_log("errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
            return false;
        }
        $storage = fastdfs_tracker_query_storage_store();
        if (!$storage) {
            error_log("errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
            return false;
        }

        // 保存源文件
        $original_uploaded_info = fastdfs_storage_upload_by_filename($source, null, array(), null, $tracker, $storage);

        if ($original_uploaded_info) {
            unlink($source); // 删除源文件
            $group_name = $original_uploaded_info['group_name'];
            $remote_filename = $original_uploaded_info['filename'];
            foreach ($files as $size => $path) {
                // 将文件缩略图以从文件方式上传
                $thumbnail_info = fastdfs_storage_upload_slave_by_filename($path, $group_name, $remote_filename, '_' . $size);
                unlink($path); // 删除缩略图
            }
            return '/' . $group_name . '/' . $remote_filename;
        } else {
            error_log("errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
            return false;
        }
    }

    /**
     * 根据文件流判断文件的mimes信息并生成要写入的文件名
     * @param $buff
     * @return bool|string
     */
    private function getFileNameFromBuff($buff)
    {
        // 根据二进制流的mimes信息获取文件的类型
        // 通过将过滤掉不符合mimes头的信息
        $imagick = new \Imagick();
        $imagick->readImageBlob($buff);
        $mimes = $imagick->getImageMimeType();
        if (isset($this->allow_type[$mimes]) == false) {
            return false;
        }

        $file_name = md5($buff) . '.' . $this->allow_type[$mimes];
        return $file_name;
    }

    /**
     * 将二进制流写入磁盘文件
     * @param $filename 要写入的文件名
     * @param $buff 内容的二进制流
     * @return bool|string
     */
    private function saveBuffToDisk($filename, $buff)
    {
        $path = $this->image_dir . '/' . $filename;
        $fp = fopen($path, 'w');
        fwrite($fp, $buff);
        fclose($fp);

        return is_file($path) ? $path : false;
    }

    /**
     * 根据配置文件创建不同尺寸的缩略图
     * @param $file_path
     * @return array('宽X高'=>'path/filename')
     */
    private function createThumb($file_path)
    {
        $ret['source'] = $file_path; // 源文件路径

        if (!is_file($file_path)) { // 如果没有配置缩略图尺寸或者文件不存在,则直接退出
            return $ret;
        }

        foreach ($this->thumb_size as $size) {
            $path = $this->createThumbBySize($file_path, $size['w'], $size['h']);
            $key = $size['w'] . 'X' . $size['h'];
            $ret[$key] = $path;
        }
        return $ret;
    }

    /**
     * 根据参数生成缩略图
     * @param $file_path 文件路径
     * @param $weight 缩略图的宽
     * @param $high 缩略图高
     * @param int $quality 质量 默认80
     * @return string 缩略图保存的路径
     */
    private function createThumbBySize($file_path, $weight, $high, $quality = 80)
    {
        $image = new \Think\Image(2); # 1=GD, 2=Imagick
        $ext = pathinfo($file_path, PATHINFO_EXTENSION); // 获取文件后缀名
        $image->open($file_path);
        $thumb = uniqid() . '.' . $ext;
        $path = $this->image_dir . '/' . $thumb;
        $image->thumb($weight, $high)->save($path, $image->type(), $quality);
        return $path;
    }
}
Copy after login

                                   

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template