亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区
Chinaunix
標(biāo)題:
對圖片進行水印添加以及生成縮率圖
[打印本頁]
作者:
majinquan
時間:
2015-08-12 13:50
標(biāo)題:
對圖片進行水印添加以及生成縮率圖
2個方法
1 給圖片進行水印添加
2生成一個新的縮率圖
代碼
<?php
class Image{
//水印配置項
private $waterOn;
private $waterImg;
private $waterPos;
private $waterPct;
private $waterText;
private $waterFont;
private $waterTextSize;
private $waterTextColor;
private $qua;
//縮略圖配置項
private $thumbWidth;
private $thumbHeight;
private $thumbType;
private $thumbEndfix;
//構(gòu)造函數(shù)
public function __construct(){
$this->waterOn=C("WATER_ON");
$this->waterImg=C("WATER_IMG");
$this->waterPos=C("WATER_POS");
$this->waterPct=C("WATER_PCT");
$this->waterText=C("WATER_TEXT");
$this->waterFont=C("WATER_FONT");
$this->waterTextSize=C("WATER_TEXT_SIZE");
$this->waterTextColor=C("WATER_TEXT_COLOR");
$this->qua=C("WATER_QUA");
//縮率圖
$this->thumbWidth=C("THUMB_WIDTH");
$this->thumbHeight=C("THUMB_HEIGHT");
$this->thumbType=C("THUMB_TYPE");
$this->thumbEndFix=C("THUMB_ENDFIX");
}
/*
*驗證圖片是否合法
*/
private function check($img){
return is_file($img)&&getimagesize($img)&&extension_loaded("gd");
}
/*
*縮率圖
*@param string $img 原圖
*@param string $outFile 縮率之后存儲的圖片
*@param int $thumbWidth 縮率圖寬度
*@param int $thumbHeight 縮率圖高度
*@param int $thumbType 那種方式進行縮略處理
*/
public function thumb($img,$outFile="",$thumbWidth="",$thumbHeight="",$thumbType=""){
if(!$this->check($img)){
return false;
}
//縮率圖處理方式
$thumbType=$thumbType?$thumbType:$this->thumbType;
//縮率圖寬度
$thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;
//縮率圖高度
$thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
//獲取原圖信息
$imgInfo=getimagesize($img);
//原圖寬度
$imgWidth=$imgInfo[0];
//原圖高度
$imgHeight=$imgInfo[1];
//獲得原圖類型
$imgtype=image_type_to_extension($imgInfo[2]);
//根據(jù)不同的縮略處理方式,獲得尺寸(原圖和縮略圖相應(yīng)的尺寸)
$thumb_size=$this->thumbsize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
//創(chuàng)建原圖
$func="imagecreatefrom".substr($imgtype,1);//變量函數(shù)
$resImg=$func($img);
//創(chuàng)建縮率圖畫布
if($imgtype==".gif"){
$res_thumb=imagecreate($thumb_size[2],$thumb_size[3]);
}else{
$res_thumb=imagecreatetruecolor($thumb_size[2],$thumb_size[3]);
}
imagecopyresized($res_thumb,$resImg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0],$thumb_size[1]);
$fileInfo=pathinfo($img);//文件信息
$outFile=$outFile?$outFile:$fileInfo['filename'].$this->thumbEndFix.$fileInfo['extension'];//文件名稱
$outFile=$fileInfo["dirname"]."/".$outFile;//加上目錄
$func="image".substr($imgtype,1);
$func($res_thumb,$outFile);
return $outFile;
}
private function thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
//縮率圖尺寸
$w=$thumbWidth;
$h=$thumbHeight;
//原圖尺寸
$img_w=$imgWidth;
$img_h=$imgHeight;
switch($thumbType){
case 1:
//寬度固定,高度自增
$h=$w/$imgWidth*$imgHeight;
break;
case 2://高度固定,寬度自
$w=$h/$imgHeight*$imgWidth;
break;
case 3:
if($imgHeight/$thumbHeight>$imgWidth/$thumbWidth){
$img_h=$imgWidth/$thumbWidth*$thumbHeight;
}else{
$img_w=$imgHeight/$thumbHeight*$thumbWidth;
}
}
return array($img_w,$img_h,$w,$h);
}
/*
*@param string $img 原圖
*@param string $outImg 加完水印后生成的圖
*@param int $pos 水印位置
*@param int $pct 透明度
*@param text $text 水印文字
*@param string $waterImg水印圖片
*/
public function water($img,$outImg=null,$pos="",$pct="",$text="",$waterImg="",$textColor=""){
if(!$this->check($img)){
return false;
}
//加完水印后生成的圖
$outImg=$outImg?$outImg:$img;
//水印位置
$pos=$pos?$pos:$this->waterPos;
//透明度
$pct=$pct?$pct:$this->waterPct;
//水印文字
$text=$text?$text:$this->waterText;
//水印圖片
$waterImg=$waterImg?$waterImg:$this->waterImg;
//驗證水印圖片
$waterImgOn=$this->check($waterImg);
//水印文字顏色
$textColor=$textColor?$textColor:$this->waterTextColor;
//原圖信息
$imgInfo=getimagesize($img);
//原圖寬度
$imgWidth=$imgInfo[0];
//原圖高度
$imgHeight=$imgInfo[1];
switch($imgInfo[2]){
case 1:
$resImg=imagecreatefromgif($img);
break;
case 2:
$resImg=imagecreatefromjpeg($img);
break;
case 3:
$resImg=imagecreatefrompng($img);
break;
}
if($waterImgOn){//水印圖片有效
//水印信息
$waterInfo=getimagesize($waterImg);
//水印寬度
$waterWidth=$waterInfo[0];
//水印高度
$waterHeight=$waterInfo[1];
//根據(jù)不同的情況創(chuàng)建不同的類型 gif jpeg png
$w_img=null;
switch($waterInfo[2]){
case 1:
$w_img=imagecreatefromgif($waterImg);
break;
case 2:
$w_img=imagecreatefromjpeg($waterImg);
break;
case 3:
$w_img=imagecreatefrompng($waterImg);
}
}else{//水印圖片失效,使用文字水印
if(empty($text)||strlen($textColor)!==7){
return false;
}
//獲得文字水印盒子信息
$textInfo=imagettfbbox($this->waterTextSize,0,$this->waterFont,$text);
//文字信息寬度
$textWidth=$textInfo[2]-$textInfo[6];
//文字信息高度
$textHeight=$textInfo[3]-$textInfo[7];
}
//水印位置
$x=$y=20;
switch($pos){
case 1:
break;
case 2:
$x=($imgWidth-$waterWidth)/2;
break;
case 3:
$y=$imgWidth-$waterWidth-10;
break;
case 4:
$x=($imgHeight-$waterHeight)/2;
break;
case 5:
$x=($imgWidth-$waterWidth)/2;
$y=($imgHeight-$waterHeight)/2;
break;
case 6:
$x=$imgWidth-$waterWidth-10;
$y=($imgHeight-$waterHeight)/2;
break;
case 7:
$x=$imgHeight-$waterHeight-10;
break;
case 8:
$x=($imgWidth-$waterWidth)/2;
$y=$imgHeight-$waterHeight-10;
break;
case 9:
$x=$imgWidth-$waterWidth-10;
$y=$imgHeight-$waterHeight-10;
break;
default:
$x=mt_rand(20,$imgWidth-$waterWidth);
$y=mt_rand(20,$imgHeight-$waterHeight);
}
if($waterImgOn){//當(dāng)水印圖片有效時,以圖片形式加水印
if($waterInfo[2]==3){
imagecopy($resImg,$w_img,$x,$y,0,0,$waterWidth,$waterHeight);
}else{
imagecopymerge($resImg,$w_img,$x,$y,0,0,$waterInfo,$waterHeight,$pct);
}
}else{//水印圖片失效,以文字水印加
$red=hexdec(substr($this->waterTextColor,1,2));
$greem=hexdec(substr($this->waterTextColor,3,2));
$blue=hexdec(substr($this->waterTextColor,5,2));
$color=imagecolorallocate($resImg,$red,$greem,$blue);
imagettftext($resImg,$this->waterTextSize,0,$x,$y,$color,$this->waterFont,$text);
}
//輸出圖片
switch($imgInfo[2]){
case 1:
imagegif($resImg,$outImg);
break;
case 2:
imagejpeg($resImg,$outImg);
break;
case 3:
imagepng($resImg,$outImg);
break;
}
//垃圾回收
if(isset($resImg)){
imagedestroy($resImg);
}
if(isset($w_img)){
imagedestroy($w_img);
}
return true;
}
}
?>
復(fù)制代碼
代碼
<?php
return array(
//水印處理
"WATER_ON"=>1,//水印開關(guān)
"WATER_IMG"=>"./data/logo.png",//水印圖片
"WATER_POS"=>9,//水印位置
"WATER_PCT"=>80,//水印透明度
"WATER_TEXT"=>"http://www.caoxiaobin.cn",
"WATER_FONT"=>"./data/simsunb.ttf",//水印字體
"WATER_TEXT_COLOR"=>"#333333",//文字顏色 16進制表示
"WATER_TEXT_SIZE"=>16,//文字大小
"WATER_QUA"=>80,//圖片壓縮比
//縮略圖
"THUMB_WIDTH"=>150,//縮率圖寬度
"THUMB_HEIGHT"=>150,//縮略圖高度
"THUMB_TYPE"=>1,//縮略圖處理 1寬度固定,高度自增 2高度固定,寬度自增 //縮略圖尺寸不變,對原圖進行裁切
"THUMB_ENDFIX"=>"_thmub"//縮略圖后綴
);
?>
復(fù)制代碼
代碼
/*
* 不區(qū)分大小寫的數(shù)據(jù)鍵檢測
*/
function array_key_exists_d($key,$arr){
$_key=strtolower($key);
foreach ($arr as $k=>$v){
if($_key==strtolower($k)){
return true;
}
}
}
/*
* 遞歸更改數(shù)組的KEY(鍵名)
* @param array;
* @stat int 0小寫 1大寫
*/
function array_change_key_case_d($arr,$stat=0){
$func=$stat?"strtoupper":"strtolower";
$_newArr=array();
if(!is_array($arr)||empty($arr)){
return $_newArr;
}
foreach($arr as $k=>$v){
$_k=$func($k);//通過變量函數(shù)轉(zhuǎn)換KEY大小寫
$_newArr[$_k]= is_array($v)?array_change_key_case_d($v):$v;
}
return $_newArr;
}
/*
* 讀取與設(shè)置配置項
* @param $name void 配置項名稱,如果不填寫返回所有配置項
* @param $value void 配置項的值
* @param $value 值 false null 只取$name值
*/
function C($name=null,$value=null){
static $config=array();//靜態(tài)變量$config存儲所有配置項
if(is_null($name)){
return $config;
}
//如果$name為數(shù)組
if(is_array($name)){
return $config=array_merge($config,array_change_key_case_d($name,1));
}
//$name為字符串 2種情況 $value無值表示獲得配置項的值,有值表示更改配置項
if(is_string($name)){
$name= strtoupper($name);
//獲得配置項的值
if(is_null($value)){
return array_key_exists_d($name,$config)?$config[$name]:null;
}else{
//設(shè)置值
$config[$name]=$value;
return true;
}
}
}
復(fù)制代碼
歡迎光臨 Chinaunix (http://www.72891.cn/)
Powered by Discuz! X3.2