博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP封装的图片大小调整类
阅读量:7217 次
发布时间:2019-06-29

本文共 3875 字,大约阅读时间需要 12 分钟。

hot3.png

最近用PHP做项目,参考了网上的一些资源封装了一个PHP图片调整类,支持输出图片流或者保存图片,等比的宽度、高度和宽高度同时调整,代码如下:

img_des = $img_des;        $this->image_type = $image_info[2];        $this->compression = $compression;        $this->permissions = $permissions;        if ($this->image_type == IMAGETYPE_JPEG) {            $this->image = ImageCreateFromJPEG($img_src);        } elseif ($this->image_type == IMAGETYPE_GIF) {            $this->image = ImageCreateFromGIF($img_src);        } elseif ($this->image_type == IMAGETYPE_PNG) {            $this->image = ImageCreateFromPNG($img_src);        }    }    /**     * 保存图片     */    function save()    {        if ($this->image_type == IMAGETYPE_JPEG) {            imagejpeg($this->image, $this->img_des, $this->compression);        } elseif ($this->image_type == IMAGETYPE_GIF) {            imagegif($this->image, $this->img_des);        } elseif ($this->image_type == IMAGETYPE_PNG) {            imagepng($this->image, $this->img_des);        }        if ($this->permissions != null) {            chmod($this->image, $this->compression);        }    }    /**     * 做为图片流直接输出     */    function output()    {        if ($this->image_type == IMAGETYPE_JPEG) {            header('Content-Type: image/jpg');            imagejpeg($this->image);        } elseif ($this->image_type == IMAGETYPE_GIF) {            header('Content-Type: image/gif');            imagegif($this->image);        } elseif ($this->image_type == IMAGETYPE_PNG) {            header('Content-Type: image/png');            imagepng($this->image);        }    }    /**     * 获取图片宽度     * @return int 图片宽度     */    function getWidth()    {        return imagesx($this->image);    }    /*     * 获取图片高度     * @return int 图片高度     */    function getHeight()    {        return imagesy($this->image);    }    /**     * 按照固定高度缩放图片     * @param $height 需要改变大小的高度     */    function resizeToHeight($height)    {        $ratio = $height / $this->getHeight();        $width = $this->getWidth() * $ratio;        $this->resize($width, $height);    }    /**     * 按照固定宽度缩放图片     * @param $width 指定宽度     */    function resizeToWidth($width)    {        $ratio = $width / $this->getWidth();        $height = $this->getheight() * $ratio;        $this->resize($width, $height);    }    /**     * 等比缩放图片     * @param int $scale 缩放比例     */    function scale($scale)    {        $width = $this->getWidth() * $scale / 100;        $height = $this->getheight() * $scale / 100;        $this->resize($width, $height);    }    /**     * 指定宽度和高度缩放图片     * @param int $width  缩放宽度     * @param int $height 缩放高度     * @return 缩放后图片对象     */    function resize($width, $height)    {        $new_image = imagecreatetruecolor($width, $height);        if ($this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) {            $current_transparent = imagecolortransparent($this->image);            if ($current_transparent != -1) {                $transparent_color = imagecolorsforindex($this->image, $current_transparent);                $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);                imagefill($new_image, 0, 0, $current_transparent);                imagecolortransparent($new_image, $current_transparent);            } elseif ($this->image_type == IMAGETYPE_PNG) {                imagealphablending($new_image, false);                $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);                imagefill($new_image, 0, 0, $color);                imagesavealpha($new_image, true);            }        }        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());        $this->image = $new_image;    }}?>

使用:

$image = new ImageResize($tmp_image_name, NULL, 100);$image->output();

转载于:https://my.oschina.net/u/437511/blog/183055

你可能感兴趣的文章
前端开发注意的问题 ,浏览器兼容性
查看>>
centos和redhat下 uwsgi配置
查看>>
Markdown 学习笔记
查看>>
vue-element-admin 多层路由问题
查看>>
Css问题 margin float 文档流 背景图底部充满
查看>>
JS match() 方法 使用
查看>>
关于shopee平台接口(php)对接示例
查看>>
BNU OJ 51000 BQG's Random String
查看>>
PAT (Advanced Level) 1044. Shopping in Mars (25)
查看>>
hdu 1531 King
查看>>
***R
查看>>
Linux 源码编译安装mysql
查看>>
取消手机端页面长按图片出现保存或者图片被打开的方法
查看>>
关于图片居中问题
查看>>
并发下的死锁问题
查看>>
Winserver下的Hyper-v “未在远程桌面会话中捕获到鼠标”
查看>>
oracle体系结构基础
查看>>
有关TCP和UDP 粘包 消息保护边界
查看>>
Mono为何能跨平台?聊聊CIL(MSIL)
查看>>
安装scrapy问题:-bash:scrapy:command not found
查看>>