Chinaunix

标题: 图片缩放加水印的类 [打印本页]

作者: xrsf    时间: 2009-07-20 12:42
标题: 图片缩放加水印的类

?php
/**
* 作用:图片缩略
* 时间:2006-8-16 15:47
* 欣然随风
* 参数:
* @src_path -- 源图路径
* @new_path -- 新图路径
* @new_width -- 新图宽
* @new_height -- 新图高
* @on -- 是否裁图
* @is_sy -- 值'FALSE'不加水印,否则为数组:
* ['path'] -- 水印图地址
* ['set_x'] -- 横向:1|左 2|中 3|右
* ['set_y'] -- 竖向:1|上 2|中 3|下
*/
/*
$src_path = '1.jpg';
$new_path = 'f:\1.gif';
$new_width = '600';
$new_height='400';
$on = 1;
$is_sy = array('path'=>'2.gif','set_x'=> 3,'set_y'=> 3);
new class_img($src_path,$new_path,$new_width,$new_height,$on,$is_sy);
*/
class class_img
{
    public $src_path;        // 源图路径
    public $src_width;        // 源图宽
    public $src_height;        // 源图高
    public $new_path;        // 新图路径
    public $new_width;        // 新图宽
    public $new_height;        // 新图高
    public $on;                // 是否裁图
    public $im;                // 图像流
    public $new_im;            // 新图像流
    function __construct($src_path,$new_path,$new_width,$new_height,$on,$is_sy=FALSE)
    {
        $this->src_path        = $src_path;
        $this->new_path        = $new_path;
        $this->new_width    = $new_width;
        $this->new_height    = $new_height;
        $this->on            = $on;
        $this->is_sy        = $is_sy;
        $this->img_gut($this->src_path);
        $this->img_new();
        if($is_sy) $this->img_sy(); // 加水印
        $this->img_echo();            // 输出
        ImageDestroy ($this->im);
        ImageDestroy ($this->new_im);
    }
    // 方法:取得源图
    function img_gut($path)
    {
        $type = substr($path,-3);
        if($type == 'jpg') $this->im = imagecreatefromjpeg($path);
        if($type == 'gif') $this->im = imagecreatefromgif($path);
        if($type == 'png') $this->im = imagecreatefrompng($path);
        $this->src_width = imagesx($this->im);
        $this->src_height = imagesy($this->im);
    }
    // 方法:输出新图
    function img_echo()
    {
        $type = substr($this->new_path,-3);
        if($type == 'jpg') imagejpeg($this->new_im,$this->new_path);
        if($type == 'gif') imagegif($this->new_im,$this->new_path);
        if($type == 'png') imagepng($this->new_im,$this->new_path);
    }
    // 方法:生成缩略图
    function img_new()
    {
        $new_ratio = $this->new_width/$this->new_height;    // 新图比例
        $src_ratio = $this->src_width/$this->src_height;    // 源图比例
        // 裁图
        if($this->on)
        {
            $this->new_im = imagecreatetruecolor($this->new_width,$this->new_height);    // 新建一个真彩色图像
            //高度优先
            if($src_ratio >= $new_ratio)
            {
                imagecopyresampled($this->new_im,$this->im,0,0,0,0,$this->new_width,$this->new_height,$this->src_height*$new_ratio,$this->src_height);
            }
            //宽度优先
            if($src_ratio  $new_ratio)
            {
                imagecopyresampled($this->new_im,$this->im,0,0,0,0,$this->new_width,$this->new_height,$this->src_width,$this->src_width/$new_ratio);
            }
        }
        else
        //不裁图
        {
            if($src_ratio >= $new_ratio)
            {
                $this->new_im = imagecreatetruecolor($this->new_width,$this->new_width/$src_ratio);
                imagecopyresampled($this->new_im,$this->im,0,0,0,0,$this->new_width,$this->new_width/$src_ratio,$this->src_width,$this->src_height);
            }
            if($src_ratio  $new_ratio)
            {
                $this->new_im = imagecreatetruecolor($this->new_height*$src_ratio,$this->new_height);
                imagecopyresampled($this->new_im, $this->im,0,0,0,0,$this->new_height*$src_ratio,$this->new_height,$this->src_width,$this->src_height);
            }
        }
    }
    // 方法:生成水印
    function img_sy()
    {
        $this->img_gut($this->is_sy['path']);
        ImageAlphaBlending($this->im, true);
        switch($this->is_sy['set_x'])
        {
            case 1: $w = 0;                                            break;
            case 2: $w = $this->new_width/2 - $this->src_width/2;    break;
            case 3: $w = $this->new_width - $this->src_width;        break;
        }
        switch($this->is_sy['set_y'])
        {
            case 1: $h = 0;                                            break;
            case 2: $h = $this->new_height/2 - $this->src_height/2;    break;
            case 3: $h = $this->new_height - $this->src_height;        break;
        }
        imagecopy($this->new_im,$this->im,$w,$h,0,0,$this->src_width,$this->src_height);    // 增加LOGO到缩略图中
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/12228/showart_2001218.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2