php显示时图片加水印(转)
时间:2008-10-30
来源:互联网
[php]
<?php
/*
* @(#)Breviary.class.php 1.0 05/08/25
*
*Copyright 2005-2008 www.8y8u.com.cn
*/
/**
* <b>图片缩略图的生成</b><br>
* @author Kevin
* @version 1.1 08/05/19
* @since php 5.2.5 mysql 5.0.45
*/
class Breviary {
/**
* 源图路径
*/
public $sourceFile;
/**
* 目标图路径
*/
public $targetFile;
/**
* 目标图宽度
*/
public $targetWidth = 200;
/**
* 目标图高度
*/
public $targetHeight = 200;
/**
* 水印的图片路径
*/
public $watemark;
/**
* 水印的图片位置
*/
public $position = "left-top";
/**
* 水印的图片透明度
*/
private $diaphaneity = 50;
/**
* 得到图像缩放比率
* @param sourceWidth 源图宽
* @param sourceHeight 源图高
* @return scaling 缩放比率
*/
function getScaling( $sourceWidth, $sourceHeight )
{
if( ( $sourceWidth + $sourceHeight ) < ( $this->targetWidth + $this->targetHeight ) ){
return 1;
}
$widthScaling = $this->targetWidth / $sourceWidth;
$heightScaling = $this->targetHeight / $sourceHeight;
$scaling = ( $widthScaling < $heightScaling ) ? $widthScaling : $heightScaling;
return $scaling;
}
/**
* 生成图片
* @return void
*/
function buildImage ()
{
//读取源文件信息
$sourceInfo = getimagesize( $this->sourceFile );
//计算缩放比例
$scaling = $this->getScaling( $sourceInfo[0], $sourceInfo[1] );
$targetWidth = $sourceInfo[0] * $scaling;
$targetHeight = $sourceInfo[1] * $scaling;
//生成真彩目标图
$target = imagecreatetruecolor( $targetWidth, $targetHeight );
//得到源图内容
switch( $sourceInfo[2] ){
case 1:
$source = imagecreatefromgif( $this->sourceFile );
break;
case 2:
$source = imagecreatefromjpeg( $this->sourceFile );
break;
case 3:
$source = imagecreatefrompng( $this->sourceFile );
break;
}
//将源图复制到目标图
imagecopyresampled(
$target,
$source,
0,
0,
0,
0,
$targetWidth,
$targetHeight,
$sourceInfo[0],
$sourceInfo[1]
);
if( !empty( $this->watemark ) && file_exists( $this->watemark ) ){
$target = $this->builderWatemark( $target, $targetWidth, $targetHeight );
}
//保存目标图
imagejpeg( $target, $this->targetFile, 100 );
}
/**
* 设置图片水印
* @return void
*/
function builderWatemark( &$target, $targetWidth, $targetHeight ){
$sourceInfo = getimagesize( $this->watemark );
if( ( $sourceInfo[0] + $sourceInfo[1] ) > ( $targetWidth + $targetHeight ) ){
return false;
}
switch( $sourceInfo[2] ){
case 1:
$source = imagecreatefromgif( $this->watemark );
break;
case 2:
$source = imagecreatefromjpeg( $this->watemark );
break;
case 3:
$source = imagecreatefrompng( $this->watemark );
break;
}
//计算方位
switch( $this->position ){
case "left-top":
$x = $y = 0;
break;
case "left-down":
$x = 0;
$y = $targetHeight - $sourceInfo[1];
break;
case "right-top":
$x = $targetWidth - $sourceInfo[0];
$y = 0;
break;
case "right-down":
$x = $targetWidth - $sourceInfo[0];
$y = $targetHeight - $sourceInfo[1];
break;
}
imagecopymerge(
$target,
$source,
$x,
$y,
0,
0,
$sourceInfo[0],
$sourceInfo[1],
$this->diaphaneity
);
return $target;
}
/**
* 设置源图片路径
* @return void
*/
function setSourceFile( $sf ){
$this->sourceFile = $sf;
}
/**
* 设置目标图路径
* @return void
*/
function setTargetFile( $tf ){
$this->targetFile = $tf;
}
/**
* 设置目标图宽度
* @return void
*/
function setTargetWidth( $tw ){
$this->targetWidth = $tw;
}
/**
* 设置目标图高度
* @return void
*/
function setTargetHeight( $th ){
$this->targetHeight = $th;
}
/**
* 设置水印图片路径
* @return void
*/
function setWatemark( $w ){
$this->watemark = $w;
}
/**
* 设置水印图片位置
* @return void
*/
function setPosition( $p ){
$this->position = $p;
}
/**
* 设置水印图片透明度
* @return void
*/
function setDiaphaneity( $d ){
$this->diaphaneity = $d;
}
}
?>
[/php]
<?php
/*
* @(#)Breviary.class.php 1.0 05/08/25
*
*Copyright 2005-2008 www.8y8u.com.cn
*/
/**
* <b>图片缩略图的生成</b><br>
* @author Kevin
* @version 1.1 08/05/19
* @since php 5.2.5 mysql 5.0.45
*/
class Breviary {
/**
* 源图路径
*/
public $sourceFile;
/**
* 目标图路径
*/
public $targetFile;
/**
* 目标图宽度
*/
public $targetWidth = 200;
/**
* 目标图高度
*/
public $targetHeight = 200;
/**
* 水印的图片路径
*/
public $watemark;
/**
* 水印的图片位置
*/
public $position = "left-top";
/**
* 水印的图片透明度
*/
private $diaphaneity = 50;
/**
* 得到图像缩放比率
* @param sourceWidth 源图宽
* @param sourceHeight 源图高
* @return scaling 缩放比率
*/
function getScaling( $sourceWidth, $sourceHeight )
{
if( ( $sourceWidth + $sourceHeight ) < ( $this->targetWidth + $this->targetHeight ) ){
return 1;
}
$widthScaling = $this->targetWidth / $sourceWidth;
$heightScaling = $this->targetHeight / $sourceHeight;
$scaling = ( $widthScaling < $heightScaling ) ? $widthScaling : $heightScaling;
return $scaling;
}
/**
* 生成图片
* @return void
*/
function buildImage ()
{
//读取源文件信息
$sourceInfo = getimagesize( $this->sourceFile );
//计算缩放比例
$scaling = $this->getScaling( $sourceInfo[0], $sourceInfo[1] );
$targetWidth = $sourceInfo[0] * $scaling;
$targetHeight = $sourceInfo[1] * $scaling;
//生成真彩目标图
$target = imagecreatetruecolor( $targetWidth, $targetHeight );
//得到源图内容
switch( $sourceInfo[2] ){
case 1:
$source = imagecreatefromgif( $this->sourceFile );
break;
case 2:
$source = imagecreatefromjpeg( $this->sourceFile );
break;
case 3:
$source = imagecreatefrompng( $this->sourceFile );
break;
}
//将源图复制到目标图
imagecopyresampled(
$target,
$source,
0,
0,
0,
0,
$targetWidth,
$targetHeight,
$sourceInfo[0],
$sourceInfo[1]
);
if( !empty( $this->watemark ) && file_exists( $this->watemark ) ){
$target = $this->builderWatemark( $target, $targetWidth, $targetHeight );
}
//保存目标图
imagejpeg( $target, $this->targetFile, 100 );
}
/**
* 设置图片水印
* @return void
*/
function builderWatemark( &$target, $targetWidth, $targetHeight ){
$sourceInfo = getimagesize( $this->watemark );
if( ( $sourceInfo[0] + $sourceInfo[1] ) > ( $targetWidth + $targetHeight ) ){
return false;
}
switch( $sourceInfo[2] ){
case 1:
$source = imagecreatefromgif( $this->watemark );
break;
case 2:
$source = imagecreatefromjpeg( $this->watemark );
break;
case 3:
$source = imagecreatefrompng( $this->watemark );
break;
}
//计算方位
switch( $this->position ){
case "left-top":
$x = $y = 0;
break;
case "left-down":
$x = 0;
$y = $targetHeight - $sourceInfo[1];
break;
case "right-top":
$x = $targetWidth - $sourceInfo[0];
$y = 0;
break;
case "right-down":
$x = $targetWidth - $sourceInfo[0];
$y = $targetHeight - $sourceInfo[1];
break;
}
imagecopymerge(
$target,
$source,
$x,
$y,
0,
0,
$sourceInfo[0],
$sourceInfo[1],
$this->diaphaneity
);
return $target;
}
/**
* 设置源图片路径
* @return void
*/
function setSourceFile( $sf ){
$this->sourceFile = $sf;
}
/**
* 设置目标图路径
* @return void
*/
function setTargetFile( $tf ){
$this->targetFile = $tf;
}
/**
* 设置目标图宽度
* @return void
*/
function setTargetWidth( $tw ){
$this->targetWidth = $tw;
}
/**
* 设置目标图高度
* @return void
*/
function setTargetHeight( $th ){
$this->targetHeight = $th;
}
/**
* 设置水印图片路径
* @return void
*/
function setWatemark( $w ){
$this->watemark = $w;
}
/**
* 设置水印图片位置
* @return void
*/
function setPosition( $p ){
$this->position = $p;
}
/**
* 设置水印图片透明度
* @return void
*/
function setDiaphaneity( $d ){
$this->diaphaneity = $d;
}
}
?>
[/php]
作者: fdmk 发布时间: 2008-10-30
好贴,收藏起来,以后用!!!!!!!!!!!!!!!!!!!!!
作者: bitterain 发布时间: 2008-12-02
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28