搜尋
首頁php教程PHP源码图片缩略水印类

图片缩略水印类

May 25, 2016 pm 05:09 PM

跳至

/**
 * 图片缩放水印类
 *
  * @version 1.0 ;
 *
 */
class cls_photo
{
    protected $waterrate = 0.2; //水印图标在图片上的比例
    protected $width = 300; //缩略图默认宽度
    protected $height = 200; //缩略图默认高度
    protected $padding = 5;  //水印图到边的距离
    protected $water_mark = "./water.png";
    protected $water_mark_pos = 5;//水印图片位置(1=左上角,2=右上角,3=左下角,4=右下角,5中央)
    protected $watermode = 0;// 0缩略图时不打水印 1缩略图时打水印
    protected $magick_handle;//图片操作句柄
    protected $format = array('jpg','gif','png','jpeg'); // 图片文件格式限定
    protected $smallpic_mode = 2;//默认模式 0为不生成缩略图, 1为裁切缩放 ,2为比例缩放 3为缩放填充模式

    /**
     * 设置图片类参数
     *
     * @param $arg 图片参数 多次可放入数组里 如下
     * @param $protected 参数值
     * array(
     *      'waterrate'=>0.2,
     *      'water_mark'=>'./water.png',
     *      'water_mark_pos'=>4,
     *      'smallpic_mode'=>1
     *      );
     * @return ture/false
     */
    public function set_args($arg,$val="")
    {
        $params = array('waterrate','water_mark','water_mark_pos','smallpic_mode','watermode','width','height');
        if(is_array($arg))
        {
            foreach ($arg as $k =>$v)
            {
                if(in_array($k,$params))
                {
                    $this->$k = $v;
                }
            }
        }
        else
        {
        	if(empty($val))
        	{
        	    return false;
        	}
        	else
        	{
        		if(in_array($arg,$params))
                {
                    $this->$arg = $val;
                }
        	}
        }
        return true;
    }

    /**
     * 图片缩放
     *
     * @param $src_file 源文件路径
     * @param $dst_file 目标文件路径
     * @return 缩略图片路径/false
     */
    public function scale($src_file,$dst_file="")
    {
        $dst_width  = $this->width;
        $dst_height = $this->height;
        $mode       = $this->smallpic_mode;
        $magic_water_handle = NewMagickWand();
        if (!MagickReadImage($magic_water_handle, $src_file))return false;

        //类型
        $srcext = strtolower(MagickGetImageFormat($magic_water_handle));
        if($srcext=='bmp')
        {
            $srcext = 'jpeg';
        }
        if(!in_array($srcext,$this->format))return false;
        //尺寸
        $src_width = MagickGetImageWidth($magic_water_handle);
        $src_height = MagickGetImageHeight($magic_water_handle);

        //裁切缩放模式
        if($mode == 1)
        {
            $pos_x=$pos_y = 0;//裁切临时位置
            $src_widthc = $src_width;//裁切临时宽度
            $src_heightc = $src_height;//裁切临时高度
            if($src_width/$src_height>$dst_width/$dst_height)
            {
                $src_widthc = $src_height*$dst_width/$dst_height;
                $pos_x = ($src_width-$src_widthc)/2;

            }
            else
            {
                $src_heightc = $src_width*$dst_height/$dst_width;
                $pos_y = ($src_height-$src_heightc)/2;
            }
        MagickCropImage($magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y);//裁切
        //因为MagickCropImage函数后,Gif 图像改,但画布不变
        $this->magick_handle = NewMagickWand();
        MagickNewImage($this->magick_handle,$src_widthc,$src_heightc,'#ffffff');
        MagickSetFormat($this->magick_handle,$srcext);
        MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0);
        //缩放
        MagickScaleImage($this->magick_handle, $dst_width, $dst_height);

        }
        //比例缩放模式
        if($mode == 2)
        {
            if($src_width/$src_height>$dst_width/$dst_height)
            {
                $dst_height=$dst_width*$src_height/$src_width;
            }
            else
            {
                $dst_width=$dst_height*$src_width/$src_height;
            }
            $this->magick_handle=$magic_water_handle;//替换
            MagickScaleImage($this->magick_handle, $dst_width, $dst_height);//缩放
        }
        //缩放填充模式
        if($mode == 3)
        {
            if($src_width/$src_height>$dst_width/$dst_height)
            {
                $dst_heightc=$dst_width*$src_height/$src_width;
                $dst_widthc=$dst_width;
            }
            else
            {
                $dst_widthc=$dst_height*$src_width/$src_height;
                $dst_heightc=$dst_height;
            }
              MagickScaleImage($magic_water_handle, $dst_widthc, $dst_heightc);//缩放
            $this->magick_handle = NewMagickWand();
            MagickNewImage($this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor);
            MagickSetFormat($this->magick_handle,$srcext);
            MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,($dst_width-$dst_widthc)/2,($dst_height-$dst_heightc)/2);
        }
        //打水印
        if($this->watermode == 1)
        {
            $this->set_mark();
        }
        if(empty($dst_file))
        {
            //建立临时文件
            $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
        }
        MagickWriteImage($this->magick_handle, $dst_file);
        return $dst_file;
    }    

    /**
     * 打水印
     *
     * @param $src_file 要打水印的图片路径
     * @param $dst_file 生产水印的文件保存路径,为空则生产随机临时文件
     * @return 水印文件路径/false
     */
    public function water_mark($src_file,$dst_file="")
    {
        $this->magick_handle = NewMagickWand();
        if (!MagickReadImage($this->magick_handle, $src_file))
        return false;
        $this->set_mark();
        if(empty($dst_file))
        {
            //建立临时文件
            $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");
        }
        MagickWriteImage($this->magick_handle, $dst_file);
        return $dst_file;
    }

    /**
     * 对内接口
     * 给图片打水印
     *
     */
    protected  function set_mark()
    {

        //尺寸
        $dst_width = MagickGetImageWidth($this->magick_handle);
        $dst_height = MagickGetImageHeight($this->magick_handle);
        //处理水印图
        if ($this->water_mark && is_file($this->water_mark))
        {
            $magic_water_handle = NewMagickWand();
            MagickRemoveImage($magic_water_handle);
            if (MagickReadImage($magic_water_handle, $this->water_mark))
            {
                MagickScaleImage($magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight($magic_water_handle)/MagickGetImageWidth($magic_water_handle));//缩放水印到图片的1/5
                if ($this->water_mark_pos == 1)
                {
                    $left = $this->padding;
                    $top = $this->padding;
                }
                elseif ($this->water_mark_pos == 2)
                {
                    $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
                    $top = $this->padding;
                }
                elseif ($this->water_mark_pos == 3)
                {
                    $left = $this->padding;
                    $top = $dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
                }
                elseif ($this->water_mark_pos == 4)
                {
                    $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);
                    $top =$dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);
                }
                elseif ($this->water_mark_pos == 5)
                {
                    $left = ($dst_width-MagickGetImageWidth($magic_water_handle))/2;
                    $top =($dst_height -MagickGetImageHeight($magic_water_handle))/2;
                }
                MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top);
            }
        }
    }
}

                   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能