GD庫 生成縮略圖、文字水印、圖片水印的函數封裝

慕課網後 端開發PHP教程-->PHP進階篇--GD庫圖像處理-->http://www.imooc.com/learn/701-->第3章 圖像常用操作

<?php
header("content-type:text/html;charset=utf-8");

/*
指定縮放比例
最大寬度和高度,等比例縮放
可以對縮略圖文件添加前綴
選擇是否刪除縮略圖的源文件
*/

/**
 * 返回圖片信息
 * @param  [string] $filename [文件地址名]
 * @return [array]  圖片的寬度、高度、擴展名、創建和輸出的函數名
 */
function getImageInfo($filename){
	if(@!$info=getimagesize($filename)){
		exit('文件不是真實圖片');
	}
	$fileInfo['width']=$info[0];
	$fileInfo['height']=$info[1];
	$mime=image_type_to_mime_type($info[2]);

	$createFun=str_replace('/','createfrom',$mime);
	$outFun=str_replace('/','',$mime);
	$fileInfo['createFun']=$createFun;
	$fileInfo['outFun']=$outFun;
	$fileInfo['ext']=strtolower(image_type_to_extension($info[2]));
	return $fileInfo;
}

/**
 * 形成縮略圖
 * @param  string  $filename  文件名
 * @param  string  $dest      縮略圖保存路徑
 * @param  string  $pre       文件名前綴
 * @param  [type]  $dst_w     最大寬度
 * @param  [type]  $dst_h     最大高度
 * @param  float   $scale     縮放比例
 * @param  boolean $delSource 是否刪除源文件的標誌
 * @return string             圖片保存路徑及文件名
 */
function thumb($filename,$dest='thumb',$pre='thumb_',$dst_w=null,$dst_h=null,$scale=0.5,$delSource=false){

	$fileInfo=getImageInfo($filename);
	$src_w=$fileInfo['width'];
	$src_h=$fileInfo['height'];

	//如果指定最大寬度和高度,按照等比例縮放進行處理
	if(is_numeric($dst_w)&&is_numeric($dst_h)){
		$ratio_orig = $src_w/$src_h;

		if ($dst_w/$dst_h > $ratio_orig) {
		   $dst_w = $dst_h*$ratio_orig;
		} else {
		   $dst_h = $dst_w/$ratio_orig;
		}
	}else{
		$dst_w=ceil($src_w*$scale);
		$dst_h=ceil($src_h*$scale);
	}

	$src_image=$fileInfo['createFun']($filename);
	$dst_image=imagecreatetruecolor($dst_w,$dst_h);

	imagecopyresampled($dst_image,$src_image,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);

	//檢測目標目錄是否存在
	if($dest&&!file_exists($dest)){
		mkdir($dest,0777,true);
	}
	$randNum=mt_rand(1000,9999);
	$dstName=$pre.$randNum.$fileInfo['ext'];

	$destination=$dest?$dest.'/'.$dstName:$dstName;
	$fileInfo['outFun']($dst_image,$destination);
	imagedestroy($src_image);
	imagedestroy($dst_image);
	if($delSource){
		@unlink($filename);
	}
	return $destination;

}

/**
 * 文字水印
 * @param  string  $filename  文件名
 * @param  string  $fontfile  字體文件
 * @param  string  $text      水印文字
 * @param  string  $dest      縮略圖保存路徑
 * @param  string  $pre       文件名前綴
 * @param  boolean $delSource 是否刪除源文件的標誌
 * @param  integer $r         紅
 * @param  integer $g         綠
 * @param  integer $b         藍
 * @param  integer $alpha     透明度
 * @param  integer $size      文字大小
 * @param  integer $angle     傾斜角度
 * @param  integer $x         水印位置x
 * @param  integer $y         水印位置y
 * @return string             圖片保存路徑及文件名
 */
function waterText($filename,$fontfile,$text="慕課網",$dest="water",$pre="waterText_",$delSource=false,$r=255,$g=0,$b=0,$alpha=60,$size=30,$angle=0,$x=0,$y=30){

	$fileInfo=getImageInfo($filename);
	$image=$fileInfo['createFun']($filename);
	$color=imagecolorallocatealpha($image,$r,$g,$b,$alpha);
	imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
	//檢測目標目錄是否存在
	if($dest&&!file_exists($dest)){
		mkdir($dest,0777,true);
	}
	$randNum=mt_rand(1000,9999);
	$dstName=$pre.$randNum.$fileInfo['ext'];
	$destination=$dest?$dest.'/'.$dstName:$dstName;
	$fileInfo['outFun']($image,$destination);
	imagedestroy($image);
	if($delSource){
		@unlink($filename);
	}
	return $destination;
}

/**
 * [waterPic description]
 * @param  string  $dstName   源文件名
 * @param  string  $srcName   水印圖片文件名
 * @param  integer $pos       水印圖片位置--九宮格
 * @param  string  $dest      縮略圖保存路徑
 * @param  string  $pre       文件名前綴
 * @param  integer $pct       合併程度
 * @param  boolean $delSource 是否刪除源文件的標誌
 * @return string             圖片保存路徑及文件名
 */
function waterPic($dstName,$srcName,$pos=0,$dest="water",$pre="waterPic_",$pct=50,$delSource=false){

	$dstInfo=getImageInfo($dstName);
	$srcInfo=getImageInfo($srcName);
	$dst_im=$dstInfo['createFun']($dstName);
	$src_im=$srcInfo['createFun']($srcName);

	$src_w=$srcInfo['width'];
	$src_h=$srcInfo['height'];
	$dst_w=$dstInfo['width'];
	$dst_h=$dstInfo['height'];

	switch($pos){
		case 0://左上角
			$x=0;
			$y=0;
			break;
		case 1:
			$x=($dst_w-$src_w)/2;
			$y=0;
			break;
		case 2://右上角
			$x=$dst_w-$src_w;
			$y=0;
			break;

		case 3:
			$x=0;
			$y=($dst_h-$src_h)/2;
			break;
		case 4:
			$x=($dst_w-$src_w)/2;
			$y=($dst_h-$src_h)/2;
			break;
		case 5:
			$x=$dst_w-$src_w;
			$y=($dst_h-$src_h)/2;
			break;

		case 6://左下角
			$x=0;
			$y=$dst_h-$src_h;
			break;
		case 7:
			$x=($dst_w-$src_w)/2;
			$y=$dst_h-$src_h;
			break;
		case 8://右下角
			$x=$dst_w-$src_w;
			$y=$dst_h-$src_h;
			break;

		default://
			$x=0;
			$y=0;
			break;
	}
	imagecopymerge($dst_im,$src_im,$x,$y,0,0,$src_w,$src_h,$pct);
	//檢測目標目錄是否存在
	if($dest&&!file_exists($dest)){
		mkdir($dest,0777,true);
	}
	$randNum=mt_rand(1000,9999);
	$name=$pre.$randNum.$dstInfo['ext'];
	$destination=$dest?$dest.'/'.$name:$name;
	$dstInfo['outFun']($dst_im,$destination);
	imagedestroy($dst_im);
	imagedestroy($src_im);
	if($delSource){
		@unlink($dstName);
	}
	return $destination;
}

?>


測試代碼

<?php
require_once 'image.func.php';
$filename='image/1.jpg';
$fontfile="fonts/7.ttf";
//thumb($filename);
//waterText($filename,$fontfile);

$dstName="image/6.jpg";
$srcName="image/logo.jpg";
waterPic($dstName,$srcName,4);

?>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章