本文章來給各同學總結了一些常用的圖像處理函數,包括有縮放、剪裁、縮放、翻轉、旋轉、透明、銳化功能,大家可參考參考。
注意事項:如果要使用php gd處理我們需要開啟gd庫
Windows下開啟PHP的GD庫支持
找到php.ini,打開內容,找到:
;extension=php_gd2.dll
把最前面的分號“;”去掉,再保存即可,如果本來就沒有分號,那就是已經開啟了。
linux開啟GD庫
##檢測GD庫是否安裝命令
php5 -m | grep -i gd
或者
php -i | grep -i --color gd
##如未安裝GD庫,則為服務器安裝,方法如下
### 如果是源碼安裝,則加入參數
--with-gd
### 如果是debian系的linux系統,用apt-get安裝,如下
apt-get install php5-gd
### 如果是CentOS系的系統,用yum安裝,如下
yum install php-gd
### 如果是suse系的linux系統,用yast安裝,如下
yast -i php5_gd
一、創建圖片資源
imagecreatetruecolor(width,height);
imagecreatefromgif(圖片名稱);
imagecreatefrompng(圖片名稱);
imagecreatefromjpeg(圖片名稱);
畫出各種圖像
imagegif(圖片資源,保存路徑);
imagepng()
imagejpeg();
二、獲取圖片屬性
imagesx(res//寬度
imagesy(res//高度
getimagesize(文件路徑)
返回一個具有四個單元的數組。索引 0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標記:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。這些標記與 PHP 4.3.0 新加的 IMAGETYPE 常量對應。索引 3 是文本字符串,內容為“height="yyy" width="xxx"”,可直接用於 IMG 標記。銷毀圖像資源
imagedestroy(圖片資源);
三、透明處理
PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource image [,int color])//將某個顏色設置成透明色
imagecolorstotal()
imagecolorforindex();
四、圖片的裁剪
imagecopyresized()
imagecopyresampled();
五、加水印(文字、圖片)
字符串編碼轉換string iconv ( string $in_charset , string $out_charset , string $str )
六、圖片旋轉
imagerotate();//制定角度的圖片翻轉
七、圖片的翻轉
沿X軸 沿Y軸翻轉
八、銳化
imagecolorsforindex()
imagecolorat()
在圖片上畫圖形
$img=imagecreatefromgif("./images/map.gif");
$red= imagecolorallocate($img, 255, 0, 0);
imageline($img, 0, 0, 100, 100, $red);
imageellipse($img, 200, 100, 100, 100, $red);
imagegif($img, "./images/map2.gif");
imagedestroy($img);圖片普通縮放
$filename="./images/hee.jpg";
$per=0.3;
list($width, $height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$width*$per;
$new=imagecreatetruecolor($n_w, $n_h);
$img=imagecreatefromjpeg($filename);
//拷貝部分圖像並調整
imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//圖像輸出新圖片、另存為
imagejpeg($new, "./images/hee2.jpg");
imagedestroy($new);
imagedestroy($img);圖片等比例縮放、沒處理透明色
function thumn($background, $width, $height, $newfile) {
list($s_w, $s_h)=getimagesize($background);//獲取原圖片高度、寬度
if ($width && ($s_w < $s_h)) {
$width = ($height / $s_h) * $s_w;
} else {
$height = ($width / $s_w) * $s_h;
}
$new=imagecreatetruecolor($width, $height);
$img=imagecreatefromjpeg($background);
imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
imagejpeg($new, $newfile);
imagedestroy($new);
imagedestroy($img);
}
thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg");gif透明色處理
function thumn($background, $width, $height, $newfile) {
list($s_w, $s_h)=getimagesize($background);
if ($width && ($s_w < $s_h)) {
$width = ($height / $s_h) * $s_w;
} else {
$height = ($width / $s_w) * $s_h;
}
$new=imagecreatetruecolor($width, $height);
$img=imagecreatefromgif($background);
$otsc=imagecolortransparent($img);
if($otsc >=0 && $otst < imagecolorstotal($img)){//判斷索引色
$tran=imagecolorsforindex($img, $otsc);//索引顏色值
$newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
imagefill($new, 0, 0, $newt);
imagecolortransparent($new, $newt);
}
imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
imagegif($new, $newfile);
imagedestroy($new);
imagedestroy($img);
}
thumn("images/map.gif", 200, 200, "./images/map3.gif");圖片裁剪
function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){
$back=imagecreatefromjpeg($background);
$new=imagecreatetruecolor($cut_width, $cut_height);
imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);
imagejpeg($new, $location);
imagedestroy($new);
imagedestroy($back);
}
cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg");圖片加水印
文字水印
function mark_text($background, $text, $x, $y){
$back=imagecreatefromjpeg($background);
$color=imagecolorallocate($back, 0, 255, 0);
imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text);
imagejpeg($back, "./images/hee7.jpg");
imagedestroy($back);
}
mark_text("./images/hee.jpg", "細說PHP", 150, 250);
//圖片水印
function mark_pic($background, $waterpic, $x, $y){
$back=imagecreatefromjpeg($background);
$water=imagecreatefromgif($waterpic);
$w_w=imagesx($water);
$w_h=imagesy($water);
imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
imagejpeg($back,"./images/hee8.jpg");
imagedestroy($back);
imagedestroy($water);
}
mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200);圖片旋轉
$back=imagecreatefromjpeg("./images/hee.jpg");
$new=imagerotate($back, 45, 0);
imagejpeg($new, "./images/hee9.jpg");圖片水平翻轉垂直翻轉
function turn_y($background, $newfile){
$back=imagecreatefromjpeg($background);
$width=imagesx($back);
$height=imagesy($back);
$new=imagecreatetruecolor($width, $height);
for($x=0; $x < $width; $x++){
imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
}
imagejpeg($new, $newfile);
imagedestroy($back);
imagedestroy($new);
}
function turn_x($background, $newfile){
$back=imagecreatefromjpeg($background);
$width=imagesx($back);
$height=imagesy($back);
$new=imagecreatetruecolor($width, $height);
for($y=0; $y < $height; $y++){
imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
}
imagejpeg($new, $newfile);
imagedestroy($back);
imagedestroy($new);
}
turn_y("./images/hee.jpg", "./images/hee11.jpg");
turn_x("./images/hee.jpg", "./images/hee12.jpg"); 圖片銳化
function sharp($background, $degree, $save){
$back=imagecreatefromjpeg($background);
$b_x=imagesx($back);
$b_y=imagesy($back);
$dst=imagecreatefromjpeg($background);
for($i=0; $i<$b_x; $i++){
for($j=0; $j<$b_y; $j++){
$b_clr1=imagecolorsforindex($back, imagecolorat($back, $i-1, $j-1));\前一個像素顏色數組
$b_clr2=imagecolorsforindex($back, imagecolorat($back, $i, $j));\取出當前顏色數組
$r=intval($b_clr2["red"]+$degree*($b_clr2["red"]-$b_clr1["red"]));\加深
$g=intval($b_clr2["green"]+$degree*($b_clr2["green"]-$b_clr1["green"]));
$b=intval($b_clr2["blue"]+$degree*($b_clr2["blue"]-$b_clr1["blue"]));
$r=min(255, max($r, 0));//限制r范圍在0-255之間
$g=min(255, max($g, 0));
$b=min(255, max($b, 0));
if(($d_clr=imagecolorexact($dst, $r, $g, $b))==-1){//等於1不在顏色范圍內
$d_clr=Imagecolorallocate($dst, $r, $g, $b);//創建一個顏色
}
imagesetpixel($dst, $i, $j, $d_clr);
}
}
imagejpeg($dst, $save);
imagedestroy($back);
imagedestroy($dst);
}
sharp("./images/hee.jpg", 20, "./images/hee13.jpg");十月 26th, 2011No comments yet真誠待 php設計實現和應用驗證碼類
validationcode.class.php
/* 開發一個驗證碼類
*
* 1. 什麼是驗證碼
*
* 2. 驗證碼的作用
*
* 3. 編寫驗證碼類(PHP圖像處理)
*
* 4. 使用驗證碼
*
*
*/<?php
class ValidationCode {
private $width;
private $height;
private $codeNum;
private $image; //圖像資源
private $disturbColorNum;
private $checkCode;
function __construct($width=80, $height=20, $codeNum=4){
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
$this->checkCode=$this->createCheckCode();
$number=floor($width*$height/15);
if($number > 240-$codeNum){
$this->disturbColorNum= 240-$codeNum;
}else{
$this->disturbColorNum=$number;
}
}
//通過訪問該方法向浏覽器中輸出圖像
function showImage($fontFace=""){
//第一步:創建圖像背景
$this->createImage();
//第二步:設置干擾元素
$this->setDisturbColor();
//第三步:向圖像中隨機畫出文本
$this->outputText($fontFace);
//第四步:輸出圖像
$this->outputImage();
}
//通過調用該方法獲取隨機創建的驗證碼字符串
function getCheckCode(){
return $this->checkCode;
}
private function createImage(){
//創建圖像資源
$this->image=imagecreatetruecolor($this->width, $this->height);
//隨機背景色
$backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
//為背景添充顏色
imagefill($this->image, 0, 0, $backColor);
//設置邊框顏色
$border=imagecolorallocate($this->image, 0, 0, 0);
//畫出矩形邊框
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
private function setDisturbColor(){
for($i=0; $i<$this->disturbColorNum; $i++){
$color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color);
}
for($i=0; $i<10; $i++){
$color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
}
}
private function createCheckCode(){
$code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string='';
for($i=0; $i < $this->codeNum; $i++){
$char=$code{rand(0, strlen($code)-1)};
$string.=$char;
}
return $string;
}
private function outputText($fontFace=""){
for($i=0; $i<$this->codeNum; $i++){
$fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
if($fontFace==""){
$fontsize=rand(3, 5);
$x=floor($this->width/$this->codeNum)*$i+3;
$y=rand(0, $this->height-15);
imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
}else{
$fontsize=rand(12, 16);
$x=floor(($this->width-8)/$this->codeNum)*$i+8;
$y=rand($fontSize+5, $this->height);
imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
}
}
}
private function outputImage() {
if(imagetypes() & IMG_GIF){
header("Content-Type:image/gif");
imagepng($this->image);
}else if(imagetypes() & IMG_JPG){
header("Content-Type:image/jpeg");
imagepng($this->image);
}else if(imagetypes() & IMG_PNG){
header("Content-Type:image/png");
imagepng($this->image);
}else if(imagetypes() & IMG_WBMP){
header("Content-Type:image/vnd.wap.wbmp");
imagepng($this->image);
}else{
die("PHP不支持圖像創建");
}
}
function __destruct(){
imagedestroy($this->image);
}
}code.php
<?php
session_start();
include "validationcode.class.php";
$code=new ValidationCode(80, 20, 4);
$code->showImage(); //輸出到頁面中供 注冊或登錄使用
$_SESSION["code"]=$code->getCheckCode(); //將驗證碼保存到服務器中demo.php
<?php
session_start();
echo $_POST["code"]."<br>";
echo $_SESSION["code"]."<br>";
if(strtoupper($_POST["code"])==strtoupper($_SESSION["code"])){
echo "ok";
}else{
echo "error";
}
?>
<body>
<form action="login.php" method="post">
user:<input type="text" name="usenrame"><br>
pass:<input type="passowrd" name="pass"><br>
code: <input type="text" name="code" onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()"> <img src="code.php" onclick="this.src='code.php?'+Math.random()"><br>
<input type="submit" name="sub" value="login"><br>
</form>
</body>
1、畫圖
驗證碼,統計圖
安裝GD庫(圖片處理庫)
■創建一個畫布—-創建資源類型——–高度、寬度
■繪制圖像
1、制定各種顏色
2、矩形、園、點、線段、扇形、畫字(字符、字符串、freetype字體),每一個圖像對應一個函數
■輸出圖像/保存處理好的圖像
1、輸出各種類型(gif、png、jpeg)
imagegif();
imagepng();
imagegpeg();
■釋放資源
<?php
//step 1創建圖片資源
$img=imagecreatetruecolor(200,200);
$red=imagecolorallocate($img,255,0,0);
$yellow=imagecolorallocate($img,255,255,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
imagefill($img,0,0,$yellow);//顏色填充
//step2畫各種圖形
imagefilledrectangle($img,10,10,80,80,$red);//畫矩形並填充
imagerectangle($img,10,10,80,80,$red));//畫矩形不填充,顏色填充
//線段
imageline($img,0, 0, 200, 200 ,$blue);
imageline($img,200, 0, 0, 200, $blue);
//點
imagesetpixel($img,50, 50 ,$red);
imagesetpixel($img,55, 50 ,$red);
imagesetpixel($img,59, 50 ,$red);
imagesetpixel($img,64, 50 ,$red);
imagesetpixel($img,72, 50 ,$red);
//圓
imageellipse($img, 100, 100, 100, 100,$green);
//圓
imagefilledellipse($img, 100, 100, 10, 10,$blue);
邊框
//輸出圖像或保存圖像
header("Content-Type:img/gif");
imagegif($img);
//釋放資源
imagedestory($img);
畫餅狀圖
//step 1創建圖片資源
$img=imagecreatetruecolor(200, 200);
// $img=imagecreate(200, 200);
$white=imagecolorallocate($img, 255, 255, 255);
$gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
$darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
$navy=imagecolorallocate($img, 0, 0, 0x80);
$darknavy=imagecolorallocate($img, 0, 0, 0x50);
$red= imagecolorallocate($img, 0xFF, 0, 0);
$darkred=imagecolorallocate($img, 0x90, 0, 0);
imagefill($img, 0, 0, $white);
//3D效果
for($i=60; $i>50; $i--){
imagefilledarc($img, 50, $i,100, 50, -160, 40, $darkgray, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 40, 75, $darknavy, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 75, 200, $darkred, IMG_ARC_PIE);
}
imagefilledarc($img, 50, $i,100, 50, -160, 40, $gray, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 40, 75, $navy, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 75, 200, $red, IMG_ARC_PIE);
header("Content-Type:image/gif");
imagegif($img);
//釋放資源
imagedestroy($img);畫文字
<?php
//step 1創建圖片資源
$img=imagecreatetruecolor(200, 200);
// $img=imagecreate(200, 200);
$white=imagecolorallocate($img, 255, 255, 255);
$gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
$darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
$navy=imagecolorallocate($img, 0, 0, 0x80);
$darknavy=imagecolorallocate($img, 0, 0, 0x50);
$red= imagecolorallocate($img, 0xFF, 0, 0);
$darkred=imagecolorallocate($img, 0x90, 0, 0);
imagefill($img, 0, 0, $gray);
imagechar($img, 5, 100, 100, "A", $red);
imagechar($img, 5, 120, 120, "B", $red);
imagecharup($img, 5, 60, 60, "C", $red);
imagecharup($img, 5, 80, 80, "D", $red);
imagestring($img, 3, 10, 10, "Hello", $navy);
imagestringup($img, 3, 10, 80, "Hello", $navy);
imagettftext($img, 25, 60, 150, 150, $red, "simkai.ttf", "##");
imagettftext($img, 12, -60, 50, 150, $red, "simli.ttf", "##");
header("Content-Type:image/gif");
imagegif($img);
//釋放資源
imagedestroy($img);2、處理原有的圖像