开花石头
(南冥洗)
版主
  
努力把内裤穿在外面
UID 7949
精华
12
积分 25623
帖子 4518
金钱 25503 喜悦币
威望 120
人脉 0
阅读权限 100
注册 2001-4-19
状态 离线
|
[广告]: 代充Paypal帐号美元
<?php
/* * @(#)Breviary.class.php 1.0 05/08/25 * *Copyright 2005-2008 [url]www.8y8u.com.cn[/url] */
/** * <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; } }
?>
| 
 |
|