上传一个php图片处理类

<?phpclass Image {    private $file;    private $image;    private $width;    private $height;    private $bits;    private $mime;

    /**     * 图片路径     * Image constructor.     * @param $file     */    public function __construct($file) {        if (file_exists($file)) {            $this->file = $file;

            $info = getimagesize($file);

            $this->width  = $info[0];            $this->height = $info[1];            $this->bits = isset($info[‘bits‘]) ? $info[‘bits‘] : ‘‘;            $this->mime = isset($info[‘mime‘]) ? $info[‘mime‘] : ‘‘;

            if ($this->mime == ‘image/gif‘) {                $this->image = imagecreatefromgif($file);            } elseif ($this->mime == ‘image/png‘) {                $this->image = imagecreatefrompng($file);            } elseif ($this->mime == ‘image/jpeg‘) {                $this->image = imagecreatefromjpeg($file);            }        } else {            exit(‘Error: Could not load image ‘ . $file . ‘!‘);        }    }

    public function getFile() {        return $this->file;    }

    public function getImage() {        return $this->image;    }

    public function getWidth() {        return $this->width;    }

    public function getHeight() {        return $this->height;    }

    public function getBits() {        return $this->bits;    }

    public function getMime() {        return $this->mime;    }

    /**     * 保存新图片     * @param $file     * @param int $quality 图片质量 0 -100 imagepng与imagegif函数都没有第三个参数quality。     */    public function save($file, $quality = 90) {        $info = pathinfo($file);

        $extension = strtolower($info[‘extension‘]);

        if (is_resource($this->image)) {            if ($extension == ‘jpeg‘ || $extension == ‘jpg‘) {                imagejpeg($this->image, $file, $quality);            } elseif ($extension == ‘png‘) {                imagepng($this->image, $file);            } elseif ($extension == ‘gif‘) {                imagegif($this->image, $file);            }            imagedestroy($this->image);        }    }

    /**     * 设置图像大小     * @param int $width     * @param int $height     * @param string $default 已那个高度或宽度比例为准     */    public function resize($width = 0, $height = 0, $default = ‘‘) {        if (!$this->width || !$this->height) {            return;        }

        $xpos = 0;        $ypos = 0;        $scale = 1;

        $scale_w = $width / $this->width;        $scale_h = $height / $this->height;

        if ($default == ‘w‘) {            $scale = $scale_w;        } elseif ($default == ‘h‘) {            $scale = $scale_h;        } else {            $scale = min($scale_w, $scale_h);        }

        if ($scale == 1 && $scale_h == $scale_w && $this->mime != ‘image/png‘) {            return;        }

        $new_width = (int)($this->width * $scale);        $new_height = (int)($this->height * $scale);        $xpos = (int)(($width - $new_width) / 2);        $ypos = (int)(($height - $new_height) / 2);

        $image_old = $this->image;        $this->image = imagecreatetruecolor($width, $height);

        if ($this->mime == ‘image/png‘) {            imagealphablending($this->image, false);            imagesavealpha($this->image, true);            $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);            imagecolortransparent($this->image, $background);        } else {            $background = imagecolorallocate($this->image, 255, 255, 255);        }

        imagefilledrectangle($this->image, 0, 0, $width, $height, $background);

        imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height);        imagedestroy($image_old);

        $this->width = $width;        $this->height = $height;    }

    /**     * 添加图片水印     * @param $watermark 水印图片对象     * @param string $position 水印位置     */    public function watermark($watermark, $position = ‘bottomright‘) {        switch($position) {            case ‘topleft‘: //左上                $watermark_pos_x = 0;                $watermark_pos_y = 0;                break;            case ‘topright‘://右上                $watermark_pos_x = $this->width - $watermark->getWidth();                $watermark_pos_y = 0;                break;            case ‘bottomleft‘://左下                $watermark_pos_x = 0;                $watermark_pos_y = $this->height - $watermark->getHeight();                break;            case ‘bottomright‘://右下                $watermark_pos_x = $this->width - $watermark->getWidth();                $watermark_pos_y = $this->height - $watermark->getHeight();                break;        }

        imagecopy($this->image, $watermark->getImage(), $watermark_pos_x, $watermark_pos_y, 0, 0, $watermark->getWidth(), $watermark->getHeight());

        imagedestroy($watermark->getImage());    }

    /**     * 裁剪图片有效部分     * @param $top_x     * @param $top_y     * @param $bottom_x     * @param $bottom_y     */    public function crop($top_x, $top_y, $bottom_x, $bottom_y) {        $image_old = $this->image;        $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);

        imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->width, $this->height);        imagedestroy($image_old);

        $this->width = $bottom_x - $top_x;        $this->height = $bottom_y - $top_y;    }

    /**     * 旋转图片     * @param $degree     * @param string $color     */    public function rotate($degree, $color = ‘FFFFFF‘) {        $rgb = $this->html2rgb($color);

        $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));

        $this->width = imagesx($this->image);        $this->height = imagesy($this->image);    }

    private function filter() {        $args = func_get_args();

        call_user_func_array(‘imagefilter‘, $args);    }

    private function text($text, $x = 0, $y = 0, $size = 5, $color = ‘000000‘) {        $rgb = $this->html2rgb($color);

        imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));    }

    /**     * 合并水印图片     * @param $merge     * @param int $x     * @param int $y     * @param int $opacity     */    private function merge($merge, $x = 0, $y = 0, $opacity = 100) {        imagecopymerge($this->image, $merge->getImage(), $x, $y, 0, 0, $merge->getWidth(), $merge->getHeight(), $opacity);    }

    /**     * 16进制颜色转rgb     * @param $color     * @return array|bool     */    private function html2rgb($color) {        if ($color[0] == ‘#‘) {            $color = substr($color, 1);        }

        if (strlen($color) == 6) {            list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);        } elseif (strlen($color) == 3) {            list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);        } else {            return false;        }

        $r = hexdec($r);        $g = hexdec($g);        $b = hexdec($b);

        return array($r, $g, $b);    }}

$imgpath = "old/1.jpg";$newimgpath = "new/";$type = 1;$image = new Image($imgpath);

$width = $image->getWidth();$height = $image->getHeight();//$width $height 为新图片的高度和宽度$image->resize($width, $height);

//输入新图片$image->save($newimgpath);

原文地址:https://www.cnblogs.com/jj0219/p/10436287.html

时间: 2024-08-29 22:00:37

上传一个php图片处理类的相关文章

关于同时上传多个图片的类(有点粗糙)

<?phpclass Upload{    private $doc;            //文件    private $docsize;        //文件大小    private $docname;        //文件名字    private $doctype;        //文件类型    private $docnewname;    //图片新名字    private $allowtype;        //可以上传的文件类型    private $sete

dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.

http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术:HTML5的api,作为一名前端的菜鸟,没什么可说的,直接分享自己学习的资料: 关于HTML5 的这些新的特性大家可以到Mozilla的开发者社区MDN https://developer.mozilla.org/zh-CN/ 上查HTML5的资料 还有就是发掘到的比较牛逼的一篇博客:http:/

经典的图片上传并绘制缩略图的类的代码

首先我们有3个文件 1个文件夹 images文件夹是默认存储图片地址 index.php是主页面 fileupload.class.php是图片上传类 ResizeImage.class.php是图片缩略图类 fileupload.class.php代码如下: <?php /** * file: fileupload.class.php 文件上传类FileUpload * 本类的实例对象用于处理上传文件,可以上传一个文件,也可同时处理多个文件上传 */ class FileUpload { pr

MVC使用JCrop上传、裁剪图片

JCrop用来裁剪图片,本篇想体验的是: 在视图页上传图片: 上传成功,跳转到另外一个编辑视图页,使用JCrop对该图片裁剪,并保存图片到指定文件夹: 裁剪成功后,在主视图页显示裁剪图片: 当然,实际项目中最有可能的做法是:在本页上传.裁剪并保存. □ 思路 →在上传图片视图页,把图片上传保存到一个临时文件夹Upload→在编辑裁剪视图页,点击"裁剪"按钮,把JCrop能提供的参数,比如宽度.高度.离顶部距离,离底部距离,离左右端距离等封装成类,传递给控制器方法→控制器方法根据接收到的

Django上传并显示图片

Django上传并显示图片 非常详细的教程,教大家一步步用Django上传与显示图片.用例子学习是一个不错的方法,下面我用一个非常简单的例子为大家讲解Django中图片的上传与显示. 1. 创建名称为'a'的项目 1 $django-admin startproject a 2.在项目'a'中创建名为'b'的app 12 $cd a$python manage.py startapp b 3.把b加入到settings.py中的INSTALLED_APPS中 123456789 INSTALLE

使用FormData上传文件、图片

关于FormData XMLHttpRequest Level 2添加了一个新的接口  ---- FormData 利用FormData对象,可以通过js用一些键值对来模拟一系列表单控件,可以使用XMLHttpRequest的 send( ) 方法来异步提交表单 与普通的ajax相比,使用FormData的最大优点就是可以异步上传二进制文件 FormData对象 FormData对象,可以把所有表单元素的name与value组成一个queryString,提交到后台. 在使用ajax提交时,使用

html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器

原文:html5 图片上传,支持图片预览.压缩.及进度显示,兼容IE6+及标准浏览器 以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上传组件添加了单独的图片上传UI,支持图片预览和缩放(通过调整图片的大小以实现图片压缩). 上传组件特点 轻量级,不依赖任何JS库,核心代码(Q.Uploader.js)仅约700行,min版本加起来不到12KB 纯

文件上传插件 plupload ,上传一个,删除之前上传,才能继续上传

var uploader = new plupload.Uploader({ runtimes: 'html5,flash,silverlight,html4',//用来指定上传方式,指定多个上传方式请使用逗号隔开. browse_button: 'browse',//触发文件选择对话框的按钮,为那个元素id container: container, //用来指定Plupload所创建的html结构的父容器,默认为前面指定的browse_button的父元素.该参数的值可以是一个元素的id,也

图片上传时获取图片的宽和高

经常会遇到图片上传的问题,这时候我们会传图片的地址,宽和高到服务器,至于图片上传就不说了,这里主要说图片上传时获取图片的原始宽和高的问题. 一般而言,我们把图片上传至服务器时,服务器会返回一个上传地址给我们,这个就是我们图片的url了,但是光有这个还是不够的,因为还要将图片的宽和高传给服务器,这时候就可以这样做了.直接上代码: var img = new Image() img.src = url 然后就可以使用img.width和img.height来获取图片的宽和高了.当然仅仅这样做是不够的