打造最大的网络营销知识与推广的信息化航母

进入论坛 | 会员注册 | 会员登陆 |
首页 >> php >> php进阶教程 >> 图片缩放加水印的类正文

图片缩放加水印的类

来源:it我领先 发布时间:2010-04-03               浏览次数:31 进入论坛社区

<?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到缩略图中

}
}

文章来源:http://it503.com/detail.PHP
添加收藏到:

关键词:图片缩放加水印的类,php
联系我们 | 关于我们 | rss订阅 | 网络社区 | 网站帮助 | 网站地图
Copyright(C) 2006-2007 it503 All Rights Reserved
本站如有转载或引用的文章涉及版权问题请速与我们联系 由于将本站资源用于商业用途而引起的纠纷,本站不负任何责任。
冀ICP备07500673号