免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1393 | 回复: 0
打印 上一主题 下一主题

图片缩放加水印的类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP