<?php

class Image {
    //类开始
    public $originimage ""//源图片文件地址
    public $imageext ""//源图片格式
    public $thumbimage ""//缩略图文件存放地址
    public $thumb_maxwidth = 1440; //缩略图最大宽度
    public $thumb_maxheight = 900; //缩略图最大高度
    public $watermark_text ""//水印文字内容
    public $watermark_minwidth = 300; //源图片最小宽度:大于此值时加水印
    public $watermark_minheight = 200; //源图片最小高度:大于此值时加水印
    public $watermark_fontfile ""//字体文件
    public $watermark_fontsize = 14; //字体大小
    public $watermark_logo "config/water.png"//水印LOGO地址
    public $watermark_transparent = 20; //水印LOGO不透明度
    private $origin_width = 0; //源图片宽度
    private $origin_height = 0; //源图片高度
    private $imageQuilty = 90; //图片质量
    private $tmp_originimage ""//临时图片(源图片)
    private $tmp_thumbimage ""//临时图片(缩略图)
    private $tmp_waterimage ""//临时图片(水印LOGO)
    private $_waterPosition = 2; //1正中间 2右下角
    //生成缩略图
    public function gen_thumbimage() {
        if ($this->originimage == "" || $this->thumbimage == "") {
            return 0;
        }
        $this->get_oriwidthheight ();
        if ($this->origin_width < $this->thumb_maxwidth && $this->origin_height < $this->thumb_maxheight) {
            $this->thumb_maxwidth = $this->origin_width;
            $this->thumb_maxheight = $this->origin_height;
        else {
            if ($this->origin_width < $this->origin_height) {
                $this->thumb_maxwidth = ($this->thumb_maxheight / $this->origin_height) * $this->origin_width;
            else {
                $this->thumb_maxheight = ($this->thumb_maxwidth / $this->origin_width) * $this->origin_height;
            }
        }
        $this->get_imagetype ();
        $this->gen_tmpimage_origin ();
        $this->gen_tmpimage_thumb ();
        if ($this->tmp_originimage == "" || $this->tmp_thumbimage == "") {
            return - 1;
        }
        imagecopyresampled ( $this->tmp_thumbimage, $this->tmp_originimage, 0, 0, 0, 0, $this->thumb_maxwidth,$this->thumb_maxheight, $this->origin_width, $this->origin_height );
        switch ($this->imageext) {
            case "gif" :
                imagegif ( $this->tmp_thumbimage, $this->thumbimage );
                return 1;
                break;
            case "jpg" :
                imagejpeg ( $this->tmp_thumbimage, $this->thumbimage, $this->imageQuilty );
                return 2;
                break;
            case "png" :
                imagepng ( $this->tmp_thumbimage, $this->thumbimage );
                return 3;
                break;
            default :
                return - 2;
                break;
        }
    }
    //添加文字水印
    public function add_watermark1() {
        if ($this->originimage == "" || $this->watermark_text == "" || $this->watermark_fontfile == "") {
            return 0;
        }
        $this->get_oriwidthheight ();
        if ($this->origin_width < $this->watermark_minwidth || $this->origin_height < $this->watermark_minheight) {
            return 0;
        }
        $this->get_imagetype ();
        $this->gen_tmpimage_origin ();
        if ($this->tmp_originimage == "") {
            return - 1;
        }
        $textcolor = imagecolorallocate ( $this->tmp_originimage, 255, 0, 0 );
        $angle = 0;
        $px $this->origin_width / 2 - 200;
        $py $this->origin_height / 2 - 10;
        imagettftext ( $this->tmp_originimage, $this->watermark_fontsize, $angle$px$py$textcolor$this->watermark_fontfile, $this->watermark_text );
        switch ($this->imageext) {
            case "gif" :
                imagegif ( $this->tmp_originimage, $this->originimage );
                return 1;
                break;
            case "jpg" :
                imagejpeg ( $this->tmp_originimage, $this->originimage, $this->imageQuilty );
                return 2;
                break;
            case "png" :
                imagepng ( $this->tmp_originimage, $this->originimage );
                return 3;
                break;
            default :
                return - 2;
                break;
        }
    }
    //添加LOGO水印
    public function add_watermark2() {
        if ($this->originimage == "" || $this->watermark_logo == "") {
            return 0;
        }
        $this->get_oriwidthheight ();
        if ($this->origin_width < $this->watermark_minwidth || $this->origin_height < $this->watermark_minheight) {
            return 0;
        }
        $this->get_imagetype ();
        $this->gen_tmpimage_origin ();
        $this->gen_tmpimage_waterlogo ();
        if ($this->tmp_originimage == "" || $this->tmp_waterimage == "") {
            return - 1;
        }
        list ( $logo_width$logo_height ) = getimagesize $this->watermark_logo );
        //正中间
        $waterZb $this->waterPosition ( $logo_width$logo_height );
        $px $waterZb ['x'];
        $py $waterZb ['y'];
        imagecopymerge ( $this->tmp_originimage, $this->tmp_waterimage, $px$py, 0, 0, $logo_width,$logo_height$this->watermark_transparent );
        switch ($this->imageext) {
            case "gif" :
                imagegif ( $this->tmp_originimage, $this->originimage );
                return 1;
                break;
            case "jpg" :
                imagejpeg ( $this->tmp_originimage, $this->originimage, $this->imageQuilty );
                return 2;
                break;
            case "png" :
                imagepng ( $this->tmp_originimage, $this->originimage );
                return 3;
                break;
            default :
                return - 2;
                break;
        }
    }
      
    /**
     * 上传缩略图
     * 注意上传文件大小限制
     *@param String $files $_FILES['upload'] 类型
     *@param String  $path  存储的目录 默认在/static/attached/
     *@param boolean $isWater 是否添加水印
     * @return string $filePath 网页url图片路径
     */
    public function upload($files$path$isWater) {
        if (is_uploaded_file $files ['tmp_name'] )) {
            $upfile $files;
            $name $upfile [name];
            $type $upfile [type];
            $size $upfile [size];
            $tmp_name $upfile [tmp_name];
            $error $upfile [error];
              
            if ($size > 1048576) {
                return array ('status' => false, 'message' => "$name图片太大超过1MB" );
            }
              
            $rs $this->getImageSize $tmp_name );
            if (! $rs ['status']) {
                $rs ['message'] = $name $rs ['message'];
                return $rs;
            }
              
            // 创建文件夹
            $save_path getcwd () . "/static/attached/" $path "/";
            $save_url "./static/attached/" $path "/";
            $ym date "Ym" );
            $save_path .= $ym "/";
            $save_url .= $ym "/";
            if (! file_exists $save_path )) {
                mkdir $save_path );
            }
              
            if ($error == '0') {
                $fileType substr $namestrpos $name"." ) + 1 );
                $prefix $this->getRandPrefix ();
                $newName date "YmdHi" ) . $prefix "." $fileType;
                $filepath $save_path $newName;
                move_uploaded_file ( $tmp_name$filepath );
              
            }
              
            if ($isWater) {
                $this->water ( $filepath );
            }
              
            return array ('status' => true, 'message' => $save_url $newName );
        }
    }
      
    /**
     * 图片增加水印处理
     * @param unknown_type $image
     */
    public function water($image) {
        $this->watermark_logo = ROOT . $this->watermark_logo;
        $this->originimage = $image;
        //LOGO水印
        $this->add_watermark2 ();
    }
      
    /**
     *
     * 获取随机前缀
     */
    private function getRandPrefix() {
        $string "abcdefghijklmnopqrstuvwxyz0123456789";
        $prefix '';
        for($i = 0; $i < 4; $i ++) {
            $rand = rand ( 0, 33 );
            $prefix .= $string {$rand};
        }
        return $prefix;
    }
      
    //检测图片大小
    private function getImageSize($image) {
        list ( $width$height$type$attr ) = getimagesize $image );
          
        if ($type != 2 && $type != 3) {
            return array ('status' => false, 'message' => "图片格式不正确,请上传JPG或者PNG图片" $type );
        }
        //检测图片大小
        if ($width > 1440) {
            return array ('status' => false, 'message' => "图片宽度请小于1440px,当前为" $width "px" );
        }
          
        if ($height > 900) {
            return array ('status' => false, 'message' => "图片高度请小于900px,当前为" $height "px" );
          
        }
        return array ('status' => true );
    }
      
    /**
     * 生成缩略图
     *
     * @param String $imagefile 原始文件
     * @param String $thumbWidth  缩略图宽度
     * @param String $thumbHeight 缩略图高度
     * @return String 缩略图url        
     */
    public function reduceImage($imagefile$thumbWidth$thumbHeight$path "thumb") {
          
        // 生成缩略图
        $dir date "Ym", time () );
        $imagefile = ROOT . $imagefile;
        $imagefile_s = ROOT . "static/attached/" $path "/" $dir "/s_" basename $imagefile );
        $imagetrans new Image ();
        $imagetrans->originimage = $imagefile;
        $imagetrans->thumbimage = $imagefile_s;
        $imagetrans->thumb_maxwidth = $thumbWidth;
        $imagetrans->thumb_maxheight = $thumbHeight;
        $isokid $imagetrans->gen_thumbimage ();
          
        return "./static/attached/" $path "/" $dir "/s_" basename $imagefile );
      
    }
    /**
     * 水印位置
     * @param int $logo_width
     * @param int $logo_height
     * @return 水印坐标
     */
    private function waterPosition($logo_width$logo_height) {
        switch ($this->_waterPosition) {
            case 1 :
                $px $this->origin_width / 2 - $logo_width / 2;
                $py $this->origin_height / 2 - $logo_height / 2;
                break;
            case 2 :
                $px $this->origin_width - $logo_width - 10;
                $py $this->origin_height - $logo_height - 10;
                break;
            default :
                $px $this->origin_width / 2 - $logo_width / 2;
                $py $this->origin_height / 2 - $logo_height / 2;
                break;
        }
          
        return array ('x' => $px'y' => $py );
    }
    //获取图片尺寸
    private function get_oriwidthheight() {
        list ( $this->origin_width, $this->origin_height ) = getimagesize $this->originimage );
        return 1;
    }
    /*
     * 检测图片格式
     * 原方法需要开启exif 扩展
     */
    private function get_imagetype() {
        $ext $this->getImgext ( $this->originimage );
        switch ($ext) {
            case 1 :
                $this->imageext = "gif";
                break;
            case 2 :
                $this->imageext = "jpg";
                break;
            case 3 :
                $this->imageext = "png";
                break;
            default :
                $this->imageext = "unknown";
                break;
        }
    }
    //创建临时图片(源图片)
    private function gen_tmpimage_origin() {
        $ext $this->getImgext ( $this->originimage );
        switch ($ext) {
            case 1 :
                $this->tmp_originimage = imagecreatefromgif ( $this->originimage );
                $bgcolor = imagecolorallocate ( $this->tmp_originimage, 0, 0, 0 );
                $bgcolortrans = imagecolortransparent ( $this->tmp_originimage, $bgcolor );
                break;
            case 2 :
                $this->tmp_originimage = imagecreatefromjpeg ( $this->originimage );
                break;
            case 3 :
                $this->tmp_originimage = imagecreatefrompng ( $this->originimage );
                imagesavealpha ( $this->tmp_originimage, true );
                break;
            default :
                $this->tmp_originimage = "";
                break;
        }
    }
    //创建临时图片(缩略图)
    private function gen_tmpimage_thumb() {
        $ext $this->getImgext ( $this->originimage );
        switch ($ext) {
            case 1 :
                $this->tmp_thumbimage = imagecreatetruecolor ( $this->thumb_maxwidth, $this->thumb_maxheight );
                $bgcolor = imagecolorallocate ( $this->tmp_thumbimage, 255, 255, 255 );
                imagefill ( $this->tmp_thumbimage, 0, 0, $bgcolor );
                break;
            case 2 :
                $this->tmp_thumbimage = imagecreatetruecolor ( $this->thumb_maxwidth, $this->thumb_maxheight );
                break;
            case 3 :
                $this->tmp_thumbimage = imagecreatetruecolor ( $this->thumb_maxwidth, $this->thumb_maxheight );
                $bgcolor = imagecolorallocate ( $this->tmp_thumbimage, 255, 255, 255 );
                imagefill ( $this->tmp_thumbimage, 0, 0, $bgcolor );
                imagealphablending ( $this->tmp_thumbimage, false );
                imagesavealpha ( $this->tmp_thumbimage, true );
                break;
            default :
                $this->tmp_thumbimage = "";
                break;
        }
    }
    //创建临时图片(LOGO水印)
    private function gen_tmpimage_waterlogo() {
        $ext $this->getImgext ( $this->watermark_logo );
        switch ($ext) {
            case 1 :
                $this->tmp_waterimage = imagecreatefromgif ( $this->watermark_logo );
                $bgcolor = imagecolorallocate ( $this->tmp_waterimage, 0, 0, 0 );
                $bgcolortrans = imagecolortransparent ( $this->tmp_waterimage, $bgcolor );
                break;
            case 2 :
                $this->tmp_waterimage = imagecreatefromjpeg ( $this->watermark_logo );
                break;
            case 3 :
                $this->tmp_waterimage = imagecreatefrompng ( $this->watermark_logo );
                imagesavealpha ( $this->tmp_waterimage, true );
                break;
            default :
                $this->tmp_waterimage = "";
                break;
        }
    }
    /*
     * 获取后缀名
     */
    public function getImgext($filename) {
        return exif_imagetype ( $filename );
    }
    //释放资源
    public function __destruct() {
        if (is_object $this->tmp_originimage ) == true) {
            imagedestroy ( $this->tmp_originimage );
        }
        if (is_object $this->tmp_thumbimage ) == true) {
            imagedestroy ( $this->tmp_thumbimage );
        }
        if (is_object $this->tmp_waterimage ) == true) {
            imagedestroy ( $this->tmp_waterimage );
        }
    }
}

php 图片操作类,支持生成缩略图,添加水印,上传缩略图的更多相关文章

  1. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  2. 适用于各浏览器支持图片预览,无刷新异步上传js插件

    文件上传无疑是web应用中一个非常常用的功能,不管是PHP.jsp还是aspx.mvc等都会需要文件上传,但是众所周知当使用自带的文件上传功能时总会出现页面刷新的情况.当然现在有了html5这个好东西 ...

  3. DedecmsV5.7本地上传缩略图无法自动添加水印的解决方法

    问题:dedecms后台 系统->图片水印设置 图片水印设置有开启了,但是本地上传缩略图无法自动添加水印 网上有很多资料,所以记录一下 1.打开dede(实际项目后台文件夹)/archives_ ...

  4. javascript 图片上传缩略图预览

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  5. Dedecms本地上传缩略图无法自动添加水印的解决方法

    客户遇到一个问题,DEDECMS(V5.7)后台添加文档时,本地上传缩略图无法自动添加水印(系统设置里的图片水印设置没有问题),找了半天,终于找到了解决方法,留个记号: 打开dede/archives ...

  6. phpcms v9升级后台无法上传缩略图的原因分析

    phpcms V9 是目前国内使用人数最多的一款开源免费的CMS系统,正是由于他的免费性,开源性,以及其自身的功能性比较强大,所以倍受许多站长朋友们的亲来,以及许多的公司的喜欢.phpcms也为了完善 ...

  7. web自动化测试难点 滚动条操作、日期框处理、上传文件

    如何把页面滑到最低部? 一般来说,做web自动化测试时,不需要单独写代码,把页面滑到可见,因为click操作,只要是元素存在并且加载出来了,就可以点击到,无需另外写滑动元素的代码. 如果特殊情况需要滑 ...

  8. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (二) 图片裁剪

    图片裁剪参见: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail        一个js插件 http://www.mikes ...

  9. C#——图片操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

随机推荐

  1. Java入门之JDK安装及环境变量配置

    一.安装JDK 安装过程中会出现两次安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹中. (注:若无安装目录要求,可全默认设置.无需做任何 ...

  2. c#ArrayList 对象集合 按某字段(属性)排序

    主程序代码 newsCompare newsCompare = new ItemManage.newsCompare(); newsList.Sort(newsCompare); 自定义类代码(按照C ...

  3. 设计模式之装饰器模式io的小入门(十一)

    装饰器模式详解地址 原文总结 定义: 在不必改变原类文件和使用继承的情况下, 动态的扩展一个对象的功能. 通过创建一个包装对象, 也就是装饰来包裹真实的对象 部分详解提示 看了一些文档, 装饰器模式非 ...

  4. Hibernate课程 初探一对多映射3-2 单向多对一的配置

    1 多方实体类中加入,一方类和getset方法 //多方定义一个一方的引用 private Grade grade; public Grade getGrade() { return grade; } ...

  5. 设置Tomcat的字符编码

    在 server.xml 中的 Connector 标签在加入 URIEncoding="UTF-8" 属性. <Connector port="8080" ...

  6. iDempiere 使用指南 销售发货流程

    Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...

  7. 转:ACCESS数据库转ORACLE数据库分享

    来源: 作者:zz 网上有很多文章介绍access转oracle数据库的方法,本人都尝试了,不是很成功,列举一下,后来人不必盲目试了,基本不成功: 1.Access-->excel-->P ...

  8. Java实习问题记录

    1. $(window).height() 获取屏幕高度2. $("#chartbottomdiv").width() 某个控件的属性 用"."3. // 保留 ...

  9. 【HHHOJ】ZJOI2019模拟赛(十二)03.03 解题报告

    点此进入比赛 得分: \(0+77+20=97\) 排名: \(Rank\ 5\) \(Rating\):\(+46\) \(T1\):[HHHOJ178]依神(点此看题面) 这套题目中的唯一一道传统 ...

  10. Uva 12299 带循环移动的RMQ(线段树)

    题目链接:https://vjudge.net/contest/147973#problem/C 题意:传统的RMQ是一个不变的数组a求区间最值.现在要循环移动(往前移动). 分析:求区间问题,很容易 ...