php圖片處理的知識內容
upload_image.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>圖片上傳頁</title>
<style type="text/css">
.file-box{ position:relative;width:340px}
.txt{ height:22px; border:1px solid #cdcdcd; width:200px;border-right:none;}
.btn{ background-color:#FFF; border:1px solid #CDCDCD;height:26px; width:70px;}
.file{ position:absolute; top:0; right:80px; height:24px; filter:alpha(opacity:0);opacity: 0;width:260px }
</style>
<script type="text/javascript" >
//JS獲取路徑 ---成功
function change()
{
var pic=document.getElementById('pic');
var file=document.getElementById('fileField');
pic.src=file.value;
if(file.files){
if(file.files.item(0)){
//src = window.URL.createObjectURL(fileObj.files[0]);
url=window.URL.createObjectURL(file.files.item(0));
document.getElementById('textfield').value = pic.src ;
pic.src=url;
}
}
}
</script>
</head>
<body>
<form action="check_image.php" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<td>上傳人名稱:</td>
<td><input type="text" class='txt' name="username" /></td>
</tr>
<tr>
<td>*上傳圖片:</td>
<td><div class="file-box"><input type='text' name='textfield' id='textfield' class='txt' /><input type='button' class='btn' value='浏覽...' /><br />
<input type="file" name="uploadfile" class="file" id="fileField" size="28" onchange="change();" />
<input type="hidden" id="theFilePath" name="theFilePath" value="">
</div>
</td>
</tr>
<tr>
<td colspan="2">
<small><em> * 可接受的圖像格式:GIF,JPG、JPEG和PNG。上傳後圖片會變為1280X960大小。</em></small>
</td>
</tr>
<tr>
<td>圖片標題:</td>
<td><input type="text" class='txt' name="caption"/></td>
</tr>
<tr>
<td colspan="2" >
<input type="submit" name="submit" value="Upload" class='btn' />
</td>
</tr>
</table>
<img src="" name="pic" id="pic" /> <br />
</form>
</body>
</html>
check_image.php 集合圖片上傳,MySQL數據庫保存,圖片放大至1280X960,圖片加文字,圖片PS處理,圖片縮略圖,圖片打LOGO水印。功能集合強大。
<?php
//session
session_start();
if($_POST['submit'] == 'Upload')
{
if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
switch($_FILES['uploadfile']['error'])
{
case UPLOAD_ERR_INI_SIZE: //其值為 1,上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值
die('上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值');
break;
case UPLOAD_ERR_FORM_SIZE: //其值為 2,上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值
die('上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值');
break;
case UPLOAD_ERR_PARTIAL: //其值為 3,文件只有部分被上傳
die('文件只有部分被上傳');
break;
case UPLOAD_ERR_NO_FILE: //其值為 4,沒有文件被上傳
die('沒有文件被上傳');
break;
case UPLOAD_ERR_NO_TMP_DIR: //其值為 6,找不到臨時文件夾
die('找不到臨時文件夾');
break;
case UPLOAD_ERR_CANT_WRITE: //其值為 7,文件寫入失敗
die('文件寫入失敗');
break;
case UPLOAD_ERR_EXTENSION: //其他異常
die('其他異常');
break;
}
}
//判斷圖片的後綴
switch($type)
{
case IMAGETYPE_GIF:
$ext = '.gif';
break;
case IMAGETYPE_JPEG:
$ext = '.jpg';
break;
case IMAGETYPE_PNG:
$ext = '.png';
break;
default :
die('您上傳的文件不支持的文件類型。');
}
//關鍵位置-取出文件後綴
$_SESSION['ext'] = $ext;
}
/*
//改變圖像的大小為1280x960的方法
function resizepic($imgsrc,$imagename,$toimgwidth,$toimgheigth)
{
//$imgsrc:圖片路徑 $imgname: 圖片保存的名稱 $toimgwidth: 圖片最終的寬度 $toimgheigth: 圖片最終的高度
$arr = getimagesize($imgsrc);
$imgWidth = $toimgwidth;
$imgHeigth = $toimgheigth;
if($_SESSION['ext'] == '.jpg')
{
header('Content-Type:image/jpeg');
$imgSrc = imagecreatefromjpeg($imgsrc);
}
if($_SESSION['ext'] == '.png')
{
header('Content-Type:image/png');
$imgSrc = imagecreatefrompng($imgsrc);
}
if($_SESSION['ext'] == '.gif')
{
header('Content-Type:image/gif');
$imgSrc = imagecreatefromgif($imgsrc);
}
$image = imagecreatetruecolor($imgWidth, $imgHeigth);
imagecopyresampled($image,$imgSrc,0,0,0,0,$imgWidth,$imgHeigth,$arr[0],$arr[1]);
if($_SESSION['ext'] == '.jpg')
{
imagejpeg($image,$dir.'/'.$imagename);
}
if($_SESSION['ext'] == '.png')
{
imagepng($image,$dir.'/'.$imagename);
}
if($_SESSION['ext'] == '.gif')
{
imagegif($image,$dir.'/'.$imagename);
}
//銷毀由url生成的圖片
imagedestroy($image);
}
*/
//創建moviesite數據庫--MySQL
$coon = mysql_connect("localhost","root","lifu") or die('不能連接到數據庫');
if (!$coon)
{
die('無法連接: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE if not exists moviesite",$coon)) //如果沒有就創建數據庫
{
echo "數據庫創建成功<br>";
}
else
{
echo "數據庫創建錯誤: " . mysql_error();
}
// 創建images表
mysql_select_db("moviesite", $coon);
//如果沒有就創建表
$sql = "CREATE TABLE if not exists images
(
image_id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(image_id),
image_caption varchar(15),
image_username varchar(15),
image_filename varchar(15),
image_date datetime
)";
mysql_query($sql,$coon);
mysql_close($coon);
//修改圖片效果
$db = mysql_connect('localhost','root','lifu') or die('不能連接到數據庫');
mysql_select_db('moviesite',$db) or die(mysql_error($db));
//上傳文件的路徑
$dir = 'D:\phpStudy\WWW\loadimages\images';
//縮略圖的路徑
$thumbdir = 'D:\phpStudy\WWW\loadimages\images\thumbs';
//設置環境變量
putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "C:\Windows\Fonts\arial.ttf";
//upload_image.php頁面傳遞過來的參數,如果是上傳圖片
if($_POST['submit'] == 'Upload')
{ /*代碼重復
if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
{
switch($_FILES['uploadfile']['error'])
{
case UPLOAD_ERR_INI_SIZE: //其值為 1,上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值
die('上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值');
break;
case UPLOAD_ERR_FORM_SIZE: //其值為 2,上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值
die('上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值');
break;
case UPLOAD_ERR_PARTIAL: //其值為 3,文件只有部分被上傳
die('文件只有部分被上傳');
break;
case UPLOAD_ERR_NO_FILE: //其值為 4,沒有文件被上傳
die('沒有文件被上傳');
break;
case UPLOAD_ERR_NO_TMP_DIR: //其值為 6,找不到臨時文件夾
die('找不到臨時文件夾');
break;
case UPLOAD_ERR_CANT_WRITE: //其值為 7,文件寫入失敗
die('文件寫入失敗');
break;
case UPLOAD_ERR_EXTENSION: //其他異常
die('其他異常');
break;
}
}
*/
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = date('Y-m-D');
/*getimagesize方法返回一個數組,
$width : 索引 0 包含圖像寬度的像素值,
$height : 索引 1 包含圖像高度的像素值,
$type : 索引 2 是圖像類型的標記:
= GIF,2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP,
= TIFF(intel byte order),8 = TIFF(motorola byte order),
= JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM,
$attr : 索引 3 是文本字符串,內容為“height="yyy" width="xxx"”,可直接用於 IMG 標記
*/
list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);
//imagecreatefromgXXX方法從一個url路徑中創建一個新的圖片
switch($type)
{
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die('你上傳的文件是不支持的文件類型');
$ext = '.gif';
$array = getimagesize($_FILES['uploadfile']['tmp_name']);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die('你上傳的文件是不支持的文件類型');
$ext = '.jpg';
$array = getimagesize($_FILES['uploadfile']['tmp_name']);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die('你上傳的文件是不支持的文件類型');
$ext = '.png';
$array = getimagesize($_FILES['uploadfile']['tmp_name']);
break;
default :
die('您上傳的文件不支持的文件類型。');
}
//將圖片改為1280X960
//新建一個指定大小的真彩色圖像big
$big = imagecreatetruecolor(1280, 960);
//重采樣拷貝部分圖像並調整大小為1280X960
imagecopyresampled($big, $image, 0, 0, 0, 0, 1280, 960, $array[0], $array[1]);
$query = 'insert into images(image_caption,image_username,image_date) values ("'.$image_caption.'","'.$image_username.'",now())';
mysql_query($query , $db) or die(mysql_error($db));
$last_id = mysql_insert_id();
//用寫入的id作為圖片的名字,避免同名的文件存放在同一目錄中
$imagename = $last_id.$ext;
$image_id = $last_id;
$query = 'update images set image_filename="'.$imagename.'" where image_id='.$last_id;
mysql_query($query , $db) or die(mysql_error($db));
//有url指定的圖片創建圖片並保存到指定目錄
switch($type)
{
case IMAGETYPE_GIF:
imagegif($big,$dir.'/'.$imagename);
break;
case IMAGETYPE_JPEG:
imagejpeg($big,$dir.'/'.$imagename);
break;
case IMAGETYPE_PNG:
imagepng($big,$dir.'/'.$imagename);
break;
}
//銷毀由url生成的圖片
imagedestroy($big);
imagedestroy($image);
}
else //如果圖片已經上傳,則從數據庫中取圖片名字
{
$query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];
$result = mysql_query($query,$db) or die(mysql_error($db));
//將結果集轉換成關聯數據,再將關聯數組的鍵名當作變量名,值作為變量的值。
extract(mysql_fetch_assoc($result));
list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.$_SESSION['ext']);
}
//如果是保存圖片
if($_POST['submit'] == 'Save')
{
if(isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir.'/'.$_POST['id'].$_SESSION['ext']))
{
if($_SESSION['ext'] == '.jpg')
{
$image = imagecreatefromjpeg($dir.'/'.$_POST['id'].$_SESSION['ext']);
}
if($_SESSION['ext'] == '.png')
{
$image = imagecreatefrompng($dir.'/'.$_POST['id'].$_SESSION['ext']);
}
if($_SESSION['ext'] == '.gif')
{
$image = imagecreatefromgif($dir.'/'.$_POST['id'].$_SESSION['ext']);
}
}
else
{
die('指定的無效圖像');
}
$effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;
switch($effect)
{
case IMG_FILTER_NEGATE:
imagefilter($image , IMG_FILTER_NEGATE); //將圖像中所有顏色反轉
break;
case IMG_FILTER_GRAYSCALE:
imagefilter($image , IMG_FILTER_GRAYSCALE); //將圖像轉換為灰度的
break;
case IMG_FILTER_EMBOSS:
imagefilter($image , IMG_FILTER_EMBOSS); //使圖像浮雕化
break;
case IMG_FILTER_GAUSSIAN_BLUR:
imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊圖像
break;
}
//圖片指定位置打印文字
if(isset($_POST['emb_caption']))
{ /*
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
$image 圖像源 $size 字體大小 $angle 逆時針旋轉角度 $fontfile 字體類型 $text 文本內容
*/
imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption);
}
//圖片指定位置打LOGO
if(isset($_POST['emb_logo']))
{
//獲取水印圖片的尺寸並創建水印
list($wmk_width , $wmk_height) = getimagesize('images/logo.png');
//$x = ($width-$wmk_width) / 2;
//$y = ($height-$wmk_height)/2;
$x = $width-$wmk_width;
$y = $height-$wmk_height;
$wmk = imagecreatefrompng('images/logo.png');
//把水印圖片和原圖片合並在一起
/*
將 wmk 圖像中坐標從 0,0 開始,寬度為 $wmk_width,高度為 $wmk_height 的一部分拷貝到 image 圖像中坐標為 $x 和 $y 的位置上。
兩圖像將根據 pct=20 來決定合並程度,其值范圍從 0 到 100。當 pct = 0 時,實際上什麼也沒做,當為
100 時對於調色板圖像本函數和 imagecopy() 完全一樣,它對真彩色圖像實現了 alpha 透明。
*/
imagecopymerge($image , $wmk , $x , $y , 0 , 0 , $wmk_width , $wmk_height , 20);
//清除水印圖片
imagedestroy($wmk);
}
//縮略圖大小
$thumb_width = $width * 0.10;
$thumb_height = $height * 0.10;
//創建一個縮略圖
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
//保存縮略圖
if($_SESSION['ext'] == '.jpg')
{
imagejpeg($thumb, $thumbdir . '/' . $_POST['id'] . $_SESSION['ext'], 100);
}
if($_SESSION['ext'] == '.png')
{
imagepng($thumb, $thumbdir . '/' . $_POST['id'] . $_SESSION['ext']);
}
if($_SESSION['ext'] == '.gif')
{
imagegif($thumb, $thumbdir . '/' . $_POST['id'] . $_SESSION['ext']);
}
echo "修改好的縮略圖:<img src='"."images/thumbs/" . $_POST['id'] . $_SESSION['ext']."' alt='' /> <br />";
//清除縮略圖
imagedestroy($thumb);
//保存原圖
if($_SESSION['ext'] == '.jpg')
{
imagejpeg($image , $dir.'/'.$_POST['id'].$_SESSION['ext'] , 100);
}
if($_SESSION['ext'] == '.png')
{
//壓縮等級必須是0-9的png文件
imagepng($image , $dir.'/'.$_POST['id'].$_SESSION['ext']);
}
if($_SESSION['ext'] == '.gif')
{
imagegif($image , $dir.'/'.$_POST['id'].$_SESSION['ext']);
}
echo "修改好的大圖:<img src='"."images/" . $_POST['id'] . $_SESSION['ext']."' alt='' /> <br />";
?>
<html>
<head>
<title>你的圖片在這!</title>
</head>
<body>
<h1>你的圖片被保存!</h1>
<img src="images/<?php echo $_POST['id'];?>.jpg" alt="" />
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>你的圖片在這!</title>
</head>
<body>
<p>這裡是你上傳到服務器上的圖片:</p>
</body>
</html>
<?php
if($_POST['submit'] == 'Upload')
{
$imagename = 'images/'.$image_id.$_SESSION['ext'];
}
else
{
$imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect'];
if(isset($_POST['emb_caption']))
{
$imagename .= '&capt='.urlencode($image_caption);
}
if(isset($_POST['emb_logo']))
{
$imagename .= '&logo=1';
}
}
?>
<div>
<?php echo "src:".$imagename ; ?>
<img src="<?php echo $imagename;?>">as:</td>
<td><?php echo "moviesite數據庫images表第".$image_id ."行!" ;?></td>
</tr>
<tr>
<td>Height:</td>
<td><?php echo $height;?></td>
</tr>
<tr>
<td>Widht:</td>
<td><?php echo $width;?></td>
</tr>
<tr>
<td>Upload date:</td>
<td><?php echo $image_date;?></td>
</tr>
</table>
<p>你可以從下面的選項列表中對你的圖像應用一個特殊的效果。注:使用任何一個過濾器,保存一個圖像 <em>可以撤消</em></p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="hidden" name="id" value="<?php echo $image_id;?>"/>
<select name="effect" id="">
<option value="-1">保持原狀</option>
<?php
echo '<option value="'.IMG_FILTER_GRAYSCALE.'" ';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GRAYSCALE)
{
echo 'selected="selected"';
}
echo ' >灰度圖像</option>';
echo '<option value="'.IMG_FILTER_GAUSSIAN_BLUR.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GAUSSIAN_BLUR)
{
echo ' selected="selected"';
}
echo '>高斯算法模糊圖像</option>';
echo '<option value="'.IMG_FILTER_EMBOSS.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_EMBOSS)
{
echo 'selected="selected"';
}
echo '>圖像浮雕化</option>';
echo '<option value="'.IMG_FILTER_NEGATE.'"';
if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_NEGATE)
{
echo 'selected="selected"';
}
echo '>圖像中所有顏色反轉</option>';
?>
</select><br />
<?php
echo '<input type="checkbox" name="emb_caption"';
if(isset($_POST['emb_caption']))
{
echo ' checked="checked"';
}
echo ' />是否嵌入在圖像的標題?';
echo '<br />';
//添加水印選項
echo '<input type="checkbox" name="emb_logo" ';
if(isset($_POST['emb_logo']))
{
echo 'checked="checked"';
}
echo ' />是否嵌入668LOGO?'. '<br />';
?>
<input type="submit" value="Save" name="submit" />
<!--<input type="submit" value="Preview" name="submit" />-->
</form>
</div>
<?php
}
mysql_close($db);
?>
本來有一個Preview的,後來沒弄了,但是代碼還是給上 image_effect.php ,已經把Preview注釋掉了。
<?php
//上傳文件的路徑
$dir = 'D:\phpStudy\WWW\loadimages\images';
//設置環境變量
putenv('GDFONTPATH='.'C:\Windows\Fonts');
$font = "C:\Windows\Fonts\arial.ttf";
if(isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir.'/'.$_GET['id'].$_SESSION['ext']))
{
$image = imagecreatefromjpeg($dir.'/'.$_GET['id'].$_SESSION['ext']);
}
else
{
die('invalid image specified');
}
$effect = (isset($_GET['e'])) ? $_GET['e'] : -1;
switch($effect)
{
case IMG_FILTER_NEGATE:
imagefilter($image , IMG_FILTER_NEGATE);
break;
case IMG_FILTER_GRAYSCALE:
imagefilter($image , IMG_FILTER_GRAYSCALE);
break;
case IMG_FILTER_EMBOSS:
imagefilter($image , IMG_FILTER_EMBOSS);
break;
case IMG_FILTER_GAUSSIAN_BLUR:
imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR);
break;
}
if(isset($_GET['capt']))
{
//echo $_GET['capt'];
imagettftext($image, 12, 0, 20, 20, 0, $font, $_GET['capt']);
}
if(isset($_GET['logo']))
{
list($widht , $height) = getimagesize($dir.'/'.$_GET['id'].$_SESSION['ext']);
list($wmk_width , $wmk_height) = getimagesize('images/logo.png');
$x = $width-$wmk_width;
$y = $height-$wmk_height;
$wmk = imagecreatefrompng('images/logo.png');
imagecopymerge($image , $wmk , $x , $y , 0 , 0 , $wmk_width , $wmk_height , 20);
imagedestroy($wmk);
}
if($_SESSION['ext'] == '.jpg')
{
header('Content-Type:image/jpeg');
imagejpeg($image , '' , 100);
}
if($_SESSION['ext'] == '.png')
{
header('Content-Type:image/png');
imagepng($image , '' );
}
if($_SESSION['ext'] == '.gif')
{
header('Content-Type:image/gif');
imagegif($image , '' );
}
?>
內容總結:
本人是自學的php,代碼書寫多有不規范之處,並且有些位置的代碼有重復累贅。以後需要加強。
upload_image.php: 主要是 HTML+Javascritp+Css 沒什麼可說的,主要就 javascript 調用 window.URL.createObjectURL(fileObj) 方法。獲取圖片的相對路徑,並做圖片的展示。
check_image.php: 1、session 用於保存從 upload_image.php 傳遞過來的圖片的後綴名。
2、數據庫moviesite創建,images表的創建
$sql = "CREATE TABLE if not exists images
(
image_id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(image_id),
image_caption varchar(15),
image_username varchar(15),
image_filename varchar(15),
image_date datetime
)";
3、使用 imagecreatetruecolor 和 imagecopyresampled 將圖片大小調整為1280X960.然後將圖片上傳保存到指定位置。
//將圖片改為1280X960
//新建一個指定大小的真彩色圖像big
$big = imagecreatetruecolor(1280, 960);
//重采樣拷貝部分圖像並調整大小為1280X960
imagecopyresampled($big, $image, 0, 0, 0, 0, 1280, 960, $array[0], $array[1]);
4、如果圖片已經上傳,則從數據庫中取圖片名字
$query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];
$result = mysql_query($query,$db) or die(mysql_error($db));
//將結果集轉換成關聯數據,再將關聯數組的鍵名當作變量名,值作為變量的值。
extract(mysql_fetch_assoc($result));
list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.$_SESSION['ext']);
5、bool imagefilter ( resource $src_im , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 ]]] ) 對圖像使用過濾器 PS圖片
imagefilter() 把過濾器 filtertype 應用到圖像上,在需要時使用 arg1,arg2 和 arg3。
filtertype 可以是下列中的一個:
IMG_FILTER_NEGATE:將圖像中所有顏色反轉。
IMG_FILTER_GRAYSCALE:將圖像轉換為灰度的。
IMG_FILTER_BRIGHTNESS:改變圖像的亮度。用 arg1 設定亮度級別。
IMG_FILTER_CONTRAST:改變圖像的對比度。用 arg1 設定對比度級別。
IMG_FILTER_COLORIZE:與 IMG_FILTER_GRAYSCALE 類似,不過可以指定顏色。用 arg1,arg2 和 arg3 分別指定 red,blue 和 green。每種顏色范圍是 0 到 255。
IMG_FILTER_EDGEDETECT:用邊緣檢測來突出圖像的邊緣。
IMG_FILTER_EMBOSS:使圖像浮雕化。
IMG_FILTER_GAUSSIAN_BLUR:用高斯算法模糊圖像。
IMG_FILTER_SELECTIVE_BLUR:模糊圖像。
IMG_FILTER_MEAN_REMOVAL:用平均移除法來達到輪廓效果。
IMG_FILTER_SMOOTH:使圖像更柔滑。用 arg1 設定柔滑級別。
6、圖片加文字
//圖片指定位置打印文字
if(isset($_POST['emb_caption']))
{ /*
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
$image 圖像源 $size 字體大小 $angle 逆時針旋轉角度 $fontfile 字體類型 $text 文本內容
*/
imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption);
}
7、圖片打LOGO
//圖片指定位置打LOGO
if(isset($_POST['emb_logo']))
{
//獲取水印圖片的尺寸並創建水印
list($wmk_width , $wmk_height) = getimagesize('images/logo.png');
//$x = ($width-$wmk_width) / 2;
//$y = ($height-$wmk_height)/2;
$x = $width-$wmk_width;
$y = $height-$wmk_height;
$wmk = imagecreatefrompng('images/logo.png');
//把水印圖片和原圖片合並在一起
/*
將 wmk 圖像中坐標從 0,0 開始,寬度為 $wmk_width,高度為 $wmk_height 的一部分拷貝到 image 圖像中坐標為 $x 和 $y 的位置上。
兩圖像將根據 pct=20 來決定合並程度,其值范圍從 0 到 100。當 pct = 0 時,實際上什麼也沒做,當為
100 時對於調色板圖像本函數和 imagecopy() 完全一樣,它對真彩色圖像實現了 alpha 透明。
*/
imagecopymerge($image , $wmk , $x , $y , 0 , 0 , $wmk_width , $wmk_height , 20);
//清除水印圖片
imagedestroy($wmk);
}
8、縮略圖 128X96 大小
//縮略圖大小
$thumb_width = $width * 0.10;
$thumb_height = $height * 0.10;
//創建一個縮略圖
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);