圖像的概念
在PHP中如何使用創建的圖像,並簡單的生成一個圖像
一.插入圖片
1.先插入一張圖片的png圖片,來了解它的Content-Type(內容類型)。
<?php ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>圖像類</title> </head> <body> <!--插入一張png頭像--> <img src="img.png" alt="嘚瑟猴"> </body> </html>
如果是php或html文件,那麼Content-Type爲:GET text/html HTTP/1.1
如果是png圖像,那麼Content-Type爲:GET image/png HTTP/1.1
二.圖像概念
1.一張圖片是由各種顏色的像素組成的矩形;
2.顏色通過調色板定義,由三種值:紅綠藍。每個值從0(無色)-255(全色)定義;
3.文件格式一般爲PNG、JPEG 和 GIF 等,推薦用 NG;
4.不同的格式處理透明度,PNG和GIF支持,JPEG不支持;
5.GIF只支持256中顏色,而真彩色可以支持24位的16777216種顏色。
三.創建圖像共分爲五步
1.使用imagecreatetruecolor函數創建一個真彩色圖像。
格式:resource imagecreatetruecolor (int $width,int $height)
2.設置一個顏色
格式:int imagecolorallocate (resource $image,int $red,int $green,int $blue)
3.填充矩形
格式:bool imagefilledrectangle(resource $image,int $x1,int $y1,int $x2,int $y2, int $color)
4.以PNG格式將圖像輸出到瀏覽器或文件
格式:bool imagepng(resource $image [,string $filename] )
5.設置輸出類型
創建圖像image.php
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個紅色 $red = imagecolorallocate($image, 255, 0, 0); //繪製一個矩形並將顏色填充到圖像上,第一個參數是圖像資源, //第二第三是是左上角的x 和y 軸,第三第四是右下角的x 和y 軸 //第四個是分配的顏色 imagefilledrectangle($image, 0, 0, 200, 200, $red); //設置php文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
插入一個設置爲image/png內容類型的php文件1.php。
<?php ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>圖像類</title> </head> <body> <!--插入一張php繪製的圖像,在 1.php裏--> <img src="image.php" alt="生成的圖像"> </body> </html>
執行1.php返回創建的圖片
圖像類-畫圖函數
一.畫圖函數
1.基於已有的圖像載入進行修改
<?php //imagecreatefromgif、imagecreatefromjpeg //加載一張圖片,基於這種圖片進行處理 $image = imagecreatefrompng('img.png'); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
2.給一張img.png圖片左上角填充白色
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //imagecreatefromgif、imagecreatefromjpeg //加載一張圖片,基於這種圖片進行處理 $image = imagecreatefrompng('img.png'); //填充顏色 imagefill($image, 0, 0, $white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
3.生成一張圖片並給圖片裏畫上一條白色直線。
bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color )
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //畫一條白色直線 imageline($image, 50, 50, 150, 150, $white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
4.畫一條矩形(空心矩形)
bool imagerectangle(resource $image,int $x1,int $y1,int $x2,int $y2,int $col)
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //畫一條矩形 imagerectangle($image, 50, 50, 150, 150, $white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
5.給生成的矩形塊中間填充白色矩形塊(實心矩形)
bool imagefilledrectangle(resource $image,int $x1,int $y1,int $x2,int $y2,int $color)
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //填充矩形 imagefilledrectangle($image, 50, 50, 150, 150, $white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
6.畫一個多邊形(三角形)
bool imagepolygon (resource $image,array $points,int $num_points,int $color)
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //畫一個多邊形 imagepolygon($image, array(50, 50, 20, 120, 150, 180), 3, $white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
7.填充多邊形塊
bool imagefilledpolygon (resource $image,array $points,int $num_points,int $color)
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //畫一個多邊形 imagefilledpolygon($image, array(50,50,20,120,150,180),3,$white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
8.畫一條弧線
bool imagearc(resource $image,int $cx,int $cy,int $w,int $h,int $s,int $e,int $color)
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //畫一個弧形 imagearc($image, 50, 50, 80, 80, 0, 360, $white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
9.創建一個背景爲紅色的圖片,給這張圖片上添加一張im.png圖片並根據角度旋轉圖像
resource imagerotate(resource $image,float $angle,int $bgd_color)
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個紅色 $red = imagecolorallocate($image, 255, 0, 0); //imagecreatefromgif、imagecreatefromjpeg //加載一張圖片,基於這種圖片進行處理 $image = imagecreatefrompng('img.png'); //旋轉圖像,第二個參數角度,第三個參數未填充的顏色 $image = imagerotate($image, 45, $red); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
圖像類-文本和縮放
一.文本函數(水印的方法)
1.帶文本的圖像(給創建的圖像裏添加一個文字Abc)
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //第二個參數表示font 內置字體1,2,3,4,5 可選 //第三和第四參數表示文字的x,y 軸位置 //第五個參數表示文字內容 imagestring($image, 5, 10, 10, 'Abc', $white); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
2.基於字體的圖像(給創建的圖像裏添加一個字體爲ariblk.ttf的Abc和字體爲simhei.ttf的中文黑體)
array imagettftext(resource $image,float $size,float $angle,int $x,int $y,int $color,string $fontfile,string $text)
<?php //創建一個真彩色,長度高度均爲200 像素,默認黑色圖像,返回資源類型 $image = imagecreatetruecolor(200, 200); //創建一個白色 $white = imagecolorallocate($image, 255, 255, 255); //第二個參數表示字體大小 //第三個參數表示旋轉的角度 //第三和第四個是位置x,y軸 //第五個是字體,如果要支持中文,請選擇中文字體 //第六個是文本 imagettftext($image,40,0,50,50,$white,'ariblk.ttf','Abc'); //支持中文,直接用中文字體就行了 imagettftext($image,20,45,50,150,$white,'simhei.ttf','中文黑體'); //設置php 文件爲png 類型,image.php header('Content-Type: image/png'); //生成png 圖像 imagepng($image);
二.縮放函數(縮略和裁剪)
1.縮放圖片函數(把原來圖片縮放成100*100的圖像並生成新的圖像)
bool imagecopyresampled(resource $dst_image,resource $src_image,int $dst_x,int $dst_y,int $src_x,int $src_y,int $dst_w,int $dst_h,int $src_w,int $src_h)
<?php //200x200的畫布 $image = imagecreatefrompng('img.png'); //求出長度 $width = imagesx($image); //求出高度 $height = imagesy($image); //新圖長度 $x = $width / 2; //新圖高度 $y = $height / 2; //建立一個新畫布, 100x100的畫布 $newImage = imagecreatetruecolor($x,$y); //第一個參數是縮略圖的句柄 //第二個參數是原圖的句柄 //第三四五六參數是座標和位移 //第六七參數表示新圖的大小 //第八就參數表示原圖的大小 //縮略和裁剪都是這個函數(主要通過後面這些參數) imagecopyresampled($newImage, $image, 0, 0, 0, 0, $x, $y, $width, $height); //設置php 文件爲png類型,image.php header('Content-Type: image/png'); //生成png圖像 imagepng($newImage);
圖像類-創建類之等比例縮放
創建一個圖形處理類,核心功能爲等比例縮放。
一.成員字段
1.設置圖像處理類等比例的必要字段。
<?php //圖像處理類 class Image { //原圖片的地址 private $origFile; //原圖片的長度 private $origWidth; //原圖片的寬度 private $origHeight; //原圖片的類型 private $origType; //原圖片的對象 private $origImage; //新圖片的對象 private $newImage; //新圖片的地址 private $newPath; }
二.構造和自我實例化
1.構造方法初始化數據。
//構造方法,初始化 public function __construct($file) { //保存原圖片地址 $this->origFile = $file; //保存原圖片的長度、高度和類型 list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile); //創建原圖的對象 $this->origImage = $this->getFromImage($this->origFile, $this->origType); }
三.創建圖形對象
1.根據gif、jpeg和png三種不同的圖形生成對象。
//創建圖像對象 private function getFromImage($file, $type) { //通過類型gif、jpg、png三種類型,創建對象 switch ($type) { case 1 : $image = imagecreatefromgif($file); break; case 2 : $image = imagecreatefromjpeg($file); break; case 3 : $image = imagecreatefrompng($file); break; default : exit('警告:此圖片類型,系統不支持!'); } //返回圖片對象 return $image; }
四.按比例縮略
1.通過百分比來等比例縮放圖片。
//按等比例縮放圖片 public function thumbScale($pct = 1) { //比例值必須是數字,不能爲空,不可大於1,否則原圖比例 if (!is_numeric($pct) || empty($pct) || $pct > 1) { $pct = 1; } //新的長度和高度 $newWidth = $this->origWidth * $pct; $newHeight = $this->origHeight * $pct; //創建一個新畫布 $this->newImage = imagecreatetruecolor($newWidth, $newHeight); //縮略後的新圖 imagecopyresampled($this->newImage, $this->origImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->origWidth, $this->origHeight); //生成圖片並清理 $this->output(); }
五.生成並清理
1.生成一個新的圖像,並清理掉對象。
//生成圖片並清理對象 private function output() { //獲取新圖片的地址 $this->newPath = $this->setPath(); //生成PNG圖片 imagepng($this->newImage, $this->newPath); //清理原圖和新圖對象 imagedestroy($this->origImage); imagedestroy($this->newImage); } //對外獲取圖片地址 public function getPath() { return $this->newPath; } //設置一個以年月日時分秒創建的名稱 private function setPath() { return date('YmdHis').'.png'; } }
最終image.php代碼:
<?php //圖像處理類 class Image { //原圖片的地址 private $origFile; //原圖片的長度 private $origWidth; //原圖片的寬度 private $origHeight; //原圖片的類型 private $origType; //原圖片的對象 private $origImage; //新圖片的對象 private $newImage; //新圖片的地址 private $newPath; //構造方法,初始化 public function __construct($file) { //保存原圖片地址 $this->origFile = $file; //保存原圖片的長度、高度和類型 list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile); //創建原圖的對象 $this->origImage = $this->getFromImage($this->origFile, $this->origType); } //創建圖像對象 private function getFromImage($file, $type) { //通過類型gif、jpg、png 三種類型,創建對象 switch ($type) { case 1 : $image = imagecreatefromgif($file); break; case 2 : $image = imagecreatefromjpeg($file); break; case 3 : $image = imagecreatefrompng($file); break; default : exit('警告:此圖片類型,系統不支持!'); } //返回圖片對象 return $image; } //按等比例縮放圖片 public function thumbScale($pct = 1) { //比例值必須是數字,不能爲空,不可大於1,否則原圖比例 if (!is_numeric($pct) || empty($pct) || $pct > 1) { $pct = 1; } //新的長度和高度 $newWidth = $this->origWidth * $pct; $newHeight = $this->origHeight * $pct; //創建一個新畫布 $this->newImage = imagecreatetruecolor($newWidth, $newHeight); //縮略後的新圖 imagecopyresampled($this->newImage, $this->origImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->origWidth, $this->origHeight); //生成圖片並清理 $this->output(); } //生成圖片並清理對象 private function output() { //獲取新圖片的地址 $this->newPath = $this->setPath(); //生成PNG圖片 imagepng($this->newImage, $this->newPath); //清理原圖和新圖對象 imagedestroy($this->origImage); imagedestroy($this->newImage); } //對外獲取圖片地址 public function getPath() { return $this->newPath; } //設置一個以年月日時分秒創建的名稱 private function setPath() { return date('YmdHis').'.png'; } }
2.php創建新圖片位置和新圖片縮放的數值引入圖像
<?php //引入圖像類 require 'image.php'; //生成的新圖片的位置 $image = new Image('img.png'); //生成的新圖片等比例縮放數值 $image->thumbScale(0.3); ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>圖像處理類</title> </head> <body> <!--獲取原圖片地址--> <img src="<?=$image->getPath();?>" alt=""> </body> </html>
圖像類-創建類之固值縮放
一.思路概述
1.很多時候,我們需要上傳的圖片轉換成固定的長寬;
2.固定後,需要通過一定比例的縮放和裁剪才能保證不失真,不變形;
3.可以計算固定長寬的等比例因子,然後進行等比例縮放;
4.然後,通過計算溢出的部分除以2,來偏移到指定的座標裁剪;
5.一般來說這種做法現在是通過前端技術來裁切的,後端並不需要這麼費力。
二.imge.php代碼實現
<?php //圖像處理類 class Image { //原圖片的地址 private $origFile; //原圖片的長度 private $origWidth; //原圖片的寬度 private $origHeight; //原圖片的類型 private $origType; //原圖片的對象 private $origImage; //新圖片的對象 private $newImage; //新圖片的地址 private $newPath; //構造方法,初始化 public function __construct($file) { //保存原圖片地址 $this->origFile = $file; //保存原圖片的長度、高度和類型 list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile); //創建原圖的對象 $this->origImage = $this->getFromImage($this->origFile, $this->origType); } //創建圖像對象 private function getFromImage($file, $type) { //通過類型gif、jpg、png 三種類型,創建對象 switch ($type) { case 1 : $image = imagecreatefromgif($file); break; case 2 : $image = imagecreatefromjpeg($file); break; case 3 : $image = imagecreatefrompng($file); break; default : exit('警告:此圖片類型,系統不支持!'); } //返回圖片對象 return $image; } //固定長高等比例縮放 //思路流程: //固定長高還必須等比例,保證不變形、不失真(但新圖大小超過原圖會模糊) public function thumbWH($newWidth = 0, $newHeight = 0) { //判斷新長度是否合法 if (!is_numeric($newWidth) || empty($newWidth) || !is_numeric($newHeight) || empty($newHeight)) { exit('警告:固定長高必須傳入有效的值!'); } //創建一個新圖的容器 $newX = $newWidth; $newY = $newHeight; //創建一個新圖 $this->newImage = imagecreatetruecolor($newX, $newY); //求出長度的等比例因子(現在長度除以原始長度) $scaleX = $newWidth / $this->origWidth; //求出高度的等比例因子(現在高度除以原始高度) $scaleY = $newHeight / $this->origHeight; //裁剪點 $cutX = $cutY = 0; //判斷等比例因子誰大就按照它來縮放 if ($scaleX < $scaleY) { //如果長度因子小於高度因子時 //長度就按照高度的等比因子縮放 $newWidth = $this->origWidth * $scaleY; //得到現圖溢出的部分的一半,作爲X 軸的裁切點 $cutX = ($newWidth - $newX) / 2; } else { $newHeight = $this->origHeight * $scaleX; $cutY = ($newHeight - $newY) / 2; } //縮略後的新圖 imagecopyresampled($this->newImage, $this->origImage, 0, 0, $cutX, $cutY, $newWidth, $newHeight, $this->origWidth, $this->origHeight); //生成圖片並清理 $this->output(); } //生成圖片並清理對象 private function output() { //獲取新圖片的地址 $this->newPath = $this->setPath(); //生成PNG圖片 imagepng($this->newImage, $this->newPath); //清理原圖和新圖對象 imagedestroy($this->origImage); imagedestroy($this->newImage); } //對外獲取圖片地址 public function getPath() { return $this->newPath; } //設置一個以年月日時分秒創建的名稱 private function setPath() { return date('YmdHis').'.png'; } }
2.php引入固定值縮放大小爲300,150圖像:
<?php //引入圖像類 require 'image.php'; $image = new Image('img.png'); //固定值縮放 $image->thumbWH(300, 150); ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>圖像處理類</title> </head> <body> <!--獲取圖片地址--> <img src="<?=$image->getPath();?>" alt=""> </body> </html>
圖像類-創建類之添加水印
一.思路概述
1.水印是一張小圖片,我們可以將它轉換成字符串讀取,圖片也是一串信息;
2.將水印信息添加到生成的圖片上,即可。
二.代碼實現
1.等比例縮放image.php代碼
<?php //圖像處理類 class Image { //原圖片的地址 private $origFile; //原圖片的長度 private $origWidth; //原圖片的寬度 private $origHeight; //原圖片的類型 private $origType; //原圖片的對象 private $origImage; //新圖片的對象 private $newImage; //新圖片的地址 private $newPath; //水印圖片的地址 private $waterMark; //構造方法,初始化 public function __construct($file) { //保存原圖片地址 $this->origFile = $file; //保存水印圖片地址 $this->waterMark = 'logo.png'; //保存原圖片的長度、高度和類型 list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile); //創建原圖的對象 $this->origImage = $this->getFromImage($this->origFile, $this->origType); } //創建圖像對象 private function getFromImage($file, $type) { //通過類型gif、jpg、png 三種類型,創建對象 switch ($type) { case 1 : $image = imagecreatefromgif($file); break; case 2 : $image = imagecreatefromjpeg($file); break; case 3 : $image = imagecreatefrompng($file); break; default : exit('警告:此圖片類型,系統不支持!'); } //返回圖片對象 return $image; } //按等比例縮放圖片 public function thumbScale($pct = 1, $waterMark = false) { //比例值必須是數字,不能爲空,不可大於1,否則原圖比例 if (!is_numeric($pct) || empty($pct) || $pct > 1) { $pct = 1; } //新的長度和高度 $newWidth = $this->origWidth * $pct; $newHeight = $this->origHeight * $pct; //創建一個新畫布 $this->newImage = imagecreatetruecolor($newWidth, $newHeight); //縮略後的新圖 imagecopyresampled($this->newImage, $this->origImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->origWidth, $this->origHeight); //判斷是否添加水印 if ($waterMark) { //添加水印 $this->addWaterMark($newWidth, $newHeight); } //生成圖片並清理 $this->output(); } //添加水印效果 private function addWaterMark($newX, $newY) { //file_get_contents 從文件中讀取數據,也可以讀取圖像流信息 //imagecreatefromstring 從圖像流信息創建一個圖像 $waterMarkObject = imagecreatefromstring(file_get_contents($this->waterMark)); //得到水印的大小 $waterMarkWidth = imagesx($waterMarkObject); $waterMarkHeight = imagesy($waterMarkObject); //設置右下角的位置 $WaterMarkX = $newX - $waterMarkWidth; $WaterMarkY = $newY - $waterMarkHeight; //拷貝圖像的一部分 //參數1,2 是圖像和水印對象 //參數3,4 是水印座標 //參數5,6 是拷貝圖像的座標 //參數7,8 是水印的大小 imagecopy($this->newImage, $waterMarkObject, $WaterMarkX, $WaterMarkY, 0, 0, $waterMarkWidth, $waterMarkHeight); //清理 imagedestroy($waterMarkObject); } //生成圖片並清理對象 private function output() { //獲取新圖片的地址 $this->newPath = $this->setPath(); //生成PNG圖片 imagepng($this->newImage, $this->newPath); //清理原圖和新圖對象 imagedestroy($this->origImage); imagedestroy($this->newImage); } //對外獲取圖片地址 public function getPath() { return $this->newPath; } //設置一個以年月日時分秒創建的名稱 private function setPath() { return date('YmdHis').'.png'; } }
等比例縮放0.7後添加水印1.php代碼
<?php //引入圖像類 require 'image.php'; $image = new Image('img.png'); //等比例縮放 $image->thumbScale(0.7, true); ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>圖像處理類</title> </head> <body> <img src="<?=$image->getPath();?>" alt=""> </body> </html>
2.固定長高等比例縮放添加水印image.php代碼
<?php //圖像處理類 class Image { //原圖片的地址 private $origFile; //原圖片的長度 private $origWidth; //原圖片的寬度 private $origHeight; //原圖片的類型 private $origType; //原圖片的對象 private $origImage; //新圖片的對象 private $newImage; //新圖片的地址 private $newPath; //水印圖片的地址 private $waterMark; //構造方法,初始化 public function __construct($file) { //保存原圖片地址 $this->origFile = $file; //保存水印圖片地址 $this->waterMark = 'logo.png'; //保存原圖片的長度、高度和類型 list($this->origWidth, $this->origHeight, $this->origType) = getimagesize($this->origFile); //創建原圖的對象 $this->origImage = $this->getFromImage($this->origFile, $this->origType); } //創建圖像對象 private function getFromImage($file, $type) { //通過類型gif、jpg、png 三種類型,創建對象 switch ($type) { case 1 : $image = imagecreatefromgif($file); break; case 2 : $image = imagecreatefromjpeg($file); break; case 3 : $image = imagecreatefrompng($file); break; default : exit('警告:此圖片類型,系統不支持!'); } //返回圖片對象 return $image; } //固定長高等比例縮放 //思路流程: //固定長高還必須等比例,保證不變形、不失真(但新圖大小超過原圖會模糊) public function thumbWH($newWidth = 0, $newHeight = 0, $waterMark = false) { //判斷新長度是否合法 if (!is_numeric($newWidth) || empty($newWidth) || !is_numeric($newHeight) || empty($newHeight)) { exit('警告:固定長高必須傳入有效的值!'); } //創建一個新圖的容器 $newX = $newWidth; $newY = $newHeight; //創建一個新圖 $this->newImage = imagecreatetruecolor($newX, $newY); //求出長度的等比例因子 $scaleX = $newWidth / $this->origWidth; //求出高度的等比例因子 $scaleY = $newHeight / $this->origHeight; //裁剪點 $cutX = $cutY = 0; //判斷等比例因子誰大就按照它來縮放 //比如$scaleX 0.4,$scaleY 0.83 if ($scaleX < $scaleY) { //如果長度因子小於高度因子時 //長度就按照高度的等比因子縮放 $newWidth = $this->origWidth * $scaleY; //得到現圖溢出的部分的一半,作爲X 軸的裁切點 $cutX = ($newWidth - $newX) / 2; } else { $newHeight = $this->origHeight * $scaleX; $cutY = ($newHeight - $newY) / 2; } //縮略後的新圖 imagecopyresampled($this->newImage, $this->origImage, 0, 0, $cutX, $cutY, $newWidth, $newHeight, $this->origWidth, $this->origHeight); //判斷是否添加水印 if ($waterMark) { //添加水印 $this->addWaterMark($newX, $newY); } //生成圖片並清理 $this->output(); } //添加水印效果 private function addWaterMark($newX, $newY) { //file_get_contents 從文件中讀取數據,也可以讀取圖像流信息 //imagecreatefromstring 從圖像流信息創建一個圖像 $waterMarkObject = imagecreatefromstring(file_get_contents($this->waterMark)); //得到水印的大小 $waterMarkWidth = imagesx($waterMarkObject); $waterMarkHeight = imagesy($waterMarkObject); //設置右下角的位置 $WaterMarkX = $newX - $waterMarkWidth; $WaterMarkY = $newY - $waterMarkHeight; //拷貝圖像的一部分 //參數1,2 是圖像和水印對象 //參數3,4 是水印座標 //參數5,6 是拷貝圖像的座標 //參數7,8 是水印的大小 imagecopy($this->newImage, $waterMarkObject, $WaterMarkX, $WaterMarkY, 0, 0, $waterMarkWidth, $waterMarkHeight); //清理 imagedestroy($waterMarkObject); } //生成圖片並清理對象 private function output() { //獲取新圖片的地址 $this->newPath = $this->setPath(); //生成PNG圖片 imagepng($this->newImage, $this->newPath); //清理原圖和新圖對象 imagedestroy($this->origImage); imagedestroy($this->newImage); } //對外獲取圖片地址 public function getPath() { return $this->newPath; } //設置一個以年月日時分秒創建的名稱 private function setPath() { return date('YmdHis').'.png'; } }
固定長高等比例縮放爲300,150後添加水印1.php代碼
<?php //引入圖像類 require 'image.php'; $image = new Image('img.png'); //固定值縮放 $image->thumbWH(300, 150, true); ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>圖像處理類</title> </head> <body> <img src="<?=$image->getPath();?>" alt=""> </body> </html>
圖像類-創建驗證碼類
一.成員屬性
class Vcode { //驗證碼的隨機因子 private $charset = 'ABCDEFGHKMNPRSTUVWXYZ23456789'; //驗證碼的字符 private $code; //驗證碼的長度 private $length; //驗證碼圖形的長度 private $width; //驗證碼圖形的高度 private $height; //驗證碼的字體 private $font; //驗證碼的字體大小 private $fontSize; //驗證碼的圖形對象 private $image; }
二.構造方法
//構造方法,初始化 public function __construct() { //默認驗證碼字符長度 $this->length = 4; //默認驗證碼圖形長度 $this->width = 120; //默認驗證碼圖形高度 $this->height = 40; //默認驗證碼字體大小 $this->fontSize = 20; //默認驗證碼字體 $this->font = 'elephant.ttf'; }
三.隨機碼
//生成一組隨機碼4個 private function createCode() { //得到驗證碼字符集的個數 $charsetLength = strlen($this->charset); //隨機出指定長度的一組字符串 for ($i = 0; $i < $this->length; $i ++) { $this->code .= $this->charset[mt_rand(0, $charsetLength - 1)]; } }
四.生成背景
//生成背景 private function createBG() { //創建一個圖形對象 $this->image = imagecreatetruecolor($this->width, $this->height); //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); //將顏色填充到圖像上去 //2,3 參數爲左上角的座標覆蓋區域 //4,5 參數爲右下角的座標覆蓋區域 imagefilledrectangle($this->image, 0, $this->height, $this->width, 0, $color); }
五.輸出驗證碼
//生成驗證碼 public function output() { //生成隨機碼 $this->createCode(); //生成背景 $this->createBG(); //輸出PNG圖像 header('Content-type: image/png'); //輸出圖像 imagepng($this->image); //清理圖像 imagedestroy($this->image); }
六.生成線條雪花
//生成雪花和線條 private function createLine() { //生成六根線條 for ($i = 0; $i < 6; $i++) { //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); //從x1,y1軸到x2,y2周的一條直線 imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); } //生成十個雪花 for ($i = 0; $i < 10; $i++) { //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); //雪花 imagestring($this->image, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); } }
七.生成文字
//生成文字 private function createFont() { //求出每一個字符所佔的比例位置 $x = $this->width / $this->length; //循環出每個字符,比如4 個 for ($i = 0; $i < $this->length; $i++) { //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); //在畫布上添加字符 //第2 個參數是字體大小,比如20 //第3 個參數是字體角度,可以是負值 //第4 個參數是字符出現的x 周,倍數* 第幾個字符+ 1-5 的隨機值 //第5 個參數是字符出現的y 周,高度/ 1.4,這裏1.4 剛好得到偏離中間的位置 //第6 個參數是顏色 //第7 個參數是字體 //第8 個參數是設置每個字符 imagettftext($this->image, $this->fontSize, mt_rand(-30, 30), $x * $i + mt_rand(1,5), $this->height / 1.4, $color, $this->font, $this->code[$i]); } }
最終vcode.php代碼:
<?php //驗證碼類 class Vcode { //驗證碼的隨機因子 private $charset = 'ABCDEFGHKMNPRSTUVWXYZ23456789'; //驗證碼的字符 private $code; //驗證碼的長度 private $length; //驗證碼圖形的長度 private $width; //驗證碼圖形的高度 private $height; //驗證碼的字體 private $font; //驗證碼的字體大小 private $fontSize; //驗證碼的圖形對象 private $image; //構造方法,初始化 public function __construct() { //默認驗證碼字符長度 $this->length = 4; //默認驗證碼圖形長度 $this->width = 120; //默認驗證碼圖形高度 $this->height = 40; //默認驗證碼字體大小 $this->fontSize = 20; //默認驗證碼字體 $this->font = 'elephant.ttf'; } //生成一組隨機碼 private function createCode() { //得到驗證碼字符集的個數 $charsetLength = strlen($this->charset); //隨機出指定長度的一組字符串 for ($i = 0; $i < $this->length; $i ++) { $this->code .= $this->charset[mt_rand(0, $charsetLength - 1)]; } //寫入session session_start(); $_SESSION['code'] = $this->code; } //生成背景 private function createBG() { //創建一個圖形對象 $this->image = imagecreatetruecolor($this->width, $this->height); //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); //將顏色填充到圖像上去 //2,3 參數爲左上角的座標覆蓋區域 //4,5 參數爲右下角的座標覆蓋區域 imagefilledrectangle($this->image, 0, $this->height, $this->width, 0, $color); } //生成雪花和線條 private function createLine() { //生成六根線條 for ($i = 0; $i < 6; $i++) { //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); //從x1,y1軸到x2,y2周的一條直線 imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); } //生成十個雪花 for ($i = 0; $i < 10; $i++) { //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); //雪花 imagestring($this->image, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); } } //生成文字 private function createFont() { //求出每一個字符所佔的比例位置 $x = $this->width / $this->length; //循環出每個字符,比如4 個 for ($i = 0; $i < $this->length; $i++) { //創建一個顏色 $color = imagecolorallocate($this->image, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); //在畫布上添加字符 //第2 個參數是字體大小,比如20 //第3 個參數是字體角度,可以是負值 //第4 個參數是字符出現的x 周,倍數* 第幾個字符+ 1-5 的隨機值 //第5 個參數是字符出現的y 周,高度/ 1.4,這裏1.4 剛好得到偏離中間的位置 //第6 個參數是顏色 //第7 個參數是字體 //第8 個參數是設置每個字符 imagettftext($this->image, $this->fontSize, mt_rand(-30, 30), $x * $i + mt_rand(1,5), $this->height / 1.4, $color, $this->font, $this->code[$i]); } } //生成驗證碼 public function output() { //生成隨機碼 $this->createCode(); //生成背景 $this->createBG(); //生成線條和雪花 $this->createLine(); //生成文字 $this->createFont(); //輸出PNG圖像 header('Content-type: image/png'); //輸出圖像 imagepng($this->image); //清理圖像 imagedestroy($this->image); } } $vcode = new Vcode(); $vcode->output();
創建一個3.php載入vcode.php驗證
<!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <img src="vcode.php" alt=""> </body> </html>
創建一個4.php載入session
<?php session_start(); echo $_SESSION['code'];
執行3.php返回隨機四位驗證碼
執行4.php返回3.php輸出的四位驗證碼驗證是否同步: