画像処理
ウェブサイトの開発は、多くの場合、非常に単純なプロセスイメージにPHPを使用して、などもちろんのサムネイル、ユーザアバター、などの画像を扱う必要があります。
設定環境
PHP GDの画像処理ライブラリをサポートするために必要。
Windowsシステムでphp.iniファイルを変更し、拡張子= php_gd2.dllを前面削除;
開いている画像処理のサポートを。
CentOSの使用 yum install php-gd*
Ubuntuのシステムの使用 apt-get install php7.3-gd
検出GDライブラリがロードされています
$has = extension_loaded('GD');
var_dump($has);
使用
PHPは、画像ステップを作成します
- HTTPヘッダ情報、文の映像コンテンツを送信
- キャンバスの作成
- 必要に応じてカラー描画を作成します。
- (充填されたキャンバス、円形、正方形絵、線を描く、キャンバスの書き込み)を描きます
- 出力画像
- キャンバスリリースリソース
ヘッダ
ヘッダによって()関数は、ブラウザに指示し、出力は代わりにテキストまたはHTMLの画像であるので、ブラウザは、画像を適切に表示することができます。
- ヘッダ( 'コンテンツタイプ:image / gifの')。
- ヘッダ( 'コンテンツタイプ:image / jpegの')。
- ヘッダ( 'コンテンツタイプ:image / pngの')。
header('Content-type:image/jpeg');
readfile('user.jpeg');
キャンバスの作成
imageCreateTrueColor(幅、高さ)
- 2つのパラメータは、描画時には表示されませんキャンバスの幅と高さ、幅及び高さ超える部分を通過しており、このサイズは、サイズ生成された画像ファイルです。
- 戻り値はリソースタイプです。
色の設定
imageColorAllocate(img_resource、R、G、B)
- 画像リソースが存在する色に属します。
- 色は、実際には整数値です。
- 0から255までの着信色値については、次の三つのパラメータの範囲
塗りつぶしの色
imageFill(img_resource、X、Y、色)
- (X、Y)は、から塗りつぶしの色座標始点を表します
- キャンバスを埋めないで、デフォルトは黒です
header('Content-type:image/jpeg');
$res = imagecreatetruecolor(1000, 500);
$red = imagecolorallocate($res, 255, 0, 0);
imagefill($res, 0, 0, $red);
imagejpeg($res);
四角形を描きます
中空の矩形を描画
- imageRectangle(img_res、X1、Y1、X2、Y2、色)
header('Content-type:image/jpeg');
$res = imagecreatetruecolor(1000, 500);
$red = imagecolorallocate($res, 255, 0, 0);
$green = imagecolorallocate($res, 0, 255, 0);
imagefill($res, 0, 0, $red);
imagerectangle($res, 100, 100, 200, 200, $green);
imagejpeg($res);
塗りつぶされた矩形が満たされた描画します
imageFilledRectangle(img_res、X1、Y1、X2、Y2、色)
- 右下隅の座標の左上隅の座標(X1、Y1)、(X2、Y2)
header('Content-type:image/jpeg');
$res = imagecreatetruecolor(1000, 500);
$red = imagecolorallocate($res, 255, 0, 0);
$green = imagecolorallocate($res, 0, 255, 0);
imagefill($res, 0, 0, $red);
imagefilledrectangle($res, 100, 100, 300, 300, $green);
imagejpeg($res);
円を描きます
中空ラウンド引き分け
- imageEllipse(img_res、X、Y、W、H、色)
良い固体ラウンド引き分けでいっぱい
- imageFilledEllipse(img_res、X、Y、W、H、色)
説明:
- 中心として(X、Y)座標。幅wは、hは高さであります
header('Content-type:image/jpeg');
$res = imagecreatetruecolor(500, 500);
$red = imagecolorallocate($res, 255, 0, 0);
$green = imagecolorallocate($res, 0, 255, 0);
imagefill($res, 0, 0, $red);
imageellipse($res, 250, 250, 100, 100, $green);
imagejpeg($res);
描画ライン
imageLine(img_res、X1、Y1、X2、Y2、色)
- 始点座標として(X1、Y1)
- (X2、Y2)が終了点の座標であります
$res = imagecreatetruecolor(500, 500);
$red = imagecolorallocate($res, 255, 0, 0);
$green = imagecolorallocate($res, 0, 255, 0);
imagefill($res, 0, 0, $red);
imageline($res, 0, 0, 499, 499, $green);
imagepng($res);
描画ピクセル
ブールimagesetpixel(リソース$画像、INT $ Xを、INT $ yを、INT $色)
- 座標(X1、Y1)
header('Content-type:image/jpeg');
$res = imagecreatetruecolor(500, 500);
$red = imagecolorallocate($res, 255, 0, 0);
$green = imagecolorallocate($res, 0, 255, 0);
imagefill($res, 0, 0, $red);
for ($i = 0; $i < 600; $i++) {
imagesetpixel($res, mt_rand(0, 500), mt_rand(0, 500), $green);
}
imagepng($res);
出力画像
異なる方法で異なるフォーマットで出力画像:
- imagegif(img_resource [ファイル名])
- imagejpeg(img_resource [ファイル名])
- imagepng(img_resource [ファイル名])
- imagebmp(img_resource [ファイル名])
- 二番目のパラメータを格納するときに同じ名前が上書き存在する場合は、ファイルを示し
リリース画像
imageDestroy(img_resource)
- 映像出力は、リソースのタイムリーなリリースでは、プログラムのニーズのためのより多くのメモリ空間を完了しています。
テキストを入力します。
アレイimagettftext(リソース$画像は、$サイズフロート$角度、INT $ Xを、INT $ yを、INT $色、文字列$ fontfile、文字列$テキストフロート)
- パラメータ:画像リソース、フォントサイズ、角度、最初の文字(文字のおおよそ左下隅)の基本的な点、Y座標(フォントベースラインの位置)、色、フォントファイル、テキスト文字列(UTF-8符号化)
header('Content-type:image/png');
$res = imagecreatetruecolor(500, 500);
$red = imagecolorallocate($res, 255, 0, 0);
$green = imagecolorallocate($res, 0, 255, 0);
imagefill($res, 0, 0, $red);
$font = realpath('source.otf');
imagettftext($res, 50, 0, 0, 50, $green, $font, 'houdunren.com');
imagepng($res);
imagedestroy($res);
配列imagettfbbox(フロート$サイズ、フロート$角度、文字列$ fontfile、文字列$テキスト)
- テキストボックスのサイズの範囲は、簡単にテキスト出力の位置を制御することができます
- これは、テキストの4つのフレームを表す8つの単位を含む配列を返します。
変数 | 場所 |
---|---|
0 | 左下隅のX位置 |
1 | 左下隅のY位置 |
2 | 右下のX位置 |
3 | 右下隅のY位置 |
4 | 右上隅のX位置 |
5 | 右上のY位置 |
6 | 左上隅のX位置 |
7 | 左上隅のY位置 |
キャンバスのテキスト表示の右上に
$font = realpath('source.otf');
$text = 'houdunren.com';
$size = 16;
$box = imagettfbbox($size, 0, $font, 'houdunren.com');
$width = $box[2] - $box[0];
$height = $box[0] - $box[7];
imagettftext($res, $size, 0, 500 - $width, $height, $green, $font, 'houdunren.com');
テキストはキャンバスの中央に表示しました
...
imagettftext($res, $size, 0, 250 - $width / 2, 250 - $height / 2, $green, $font, 'houdunren.com');
...
外部画像
画像ファイルを開きます
- imageCreateFromgif(ファイル名/ URL)
- imageCreateFromjpeg(ファイル名/ URL)
- imageCreateFrompng(ファイル名/ URL)
- imageCreateFrombmp(ファイル名/ URL)
- リソースタイプを返します。
情報へのアクセス
imagesx(img_resource)
- 画像の幅を取得します。
imagesy(img_resource)
- 画像の高さを取得します。
getimagesize(img_file)
- 配列getimagesize(文字列$ファイル名[、配列&$のimageinfo])
コピー画像
コピーされた画像の一部
- ブールIMAGECOPY(リソースの$ dst_im、リソース$ src_im、int型$ dst_x、int型$ dst_y、int型$ src_x、int型$ src_y、int型$のsrc_w、int型$ src_h)
コピーとマージ一部画像
- ブールimagecopymerge(リソースの$ dst_im、リソース$ src_im、int型$ dst_x、int型$ dst_y、int型$ src_x、int型$ src_y、int型$のsrc_w、int型$ src_h、int型$ PCT)
画像ズーム
部分画像とリサイズをコピーします
- BOOL imagecopyresampled(リソースの$ dst_image、リソースの$ 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)
検証コード
使用
# 后台code.php
include 'Captcha.php';
$code = new Captcha(100, 30, 20);
$code->make();
# 前台
<img src="code.php" alt="">
クラスコード
<?php
class Captcha
{
protected $width;
protected $height;
protected $res;
protected $len = 4;
protected $font;
protected $size;
public function __construct(int $width = 100, int $height = 30, $size = 20)
{
$this->width = $width;
$this->height = $height;
$this->font = realpath('source.otf');
$this->size = $size;
}
public function make()
{
$res = imagecreatetruecolor($this->width, $this->height);
imagefill($this->res = $res, 0, 0, imagecolorallocate($res, 200, 200, 200));
$this->text();
$this->line();
$this->pix();
$this->render();
}
protected function text()
{
$text = 'abcdefghigk123456789';
for ($i = 0; $i < $this->len; $i++) {
$x = $this->width / $this->len * $i;
$box = imagettfbbox($this->size, 0, $this->font, $text[$i]);
imagettftext(
$this->res,
$this->size,
mt_rand(-20, 20),
$x,
$this->height / 2 + ($box[0] - $box[7]) / 2,
$this->textColor(),
$this->font,
strtoupper($text[$i])
);
}
}
protected function pix()
{
for ($i = 0; $i < 300; $i++) {
imagesetpixel(
$this->res,
mt_rand(0, $this->width),
mt_rand(0, $this->height),
$this->color()
);
}
}
protected function line()
{
for ($i = 0; $i < 6; $i++) {
imagesetthickness($this->res, mt_rand(1, 3));
imageline(
$this->res,
mt_rand(0, $this->width),
mt_rand(0, $this->height),
mt_rand(0, $this->width),
mt_rand(0, $this->height),
$this->color()
);
}
}
protected function color()
{
return imagecolorallocate($this->res, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));
}
protected function textColor()
{
return imagecolorallocate($this->res, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150));
}
protected function render()
{
header('Content-type:image/png');
imagepng($this->res);
}
}
透かしクラス
呼ばれます
<?php
include "Water.php";
$water = new Water("logo.png");
$water->make('sun.png', '2.png', 5);
透かしソースのカテゴリ
<?php
class Water
{
//水印资源
protected $water;
//水印图片
public function __construct(string $water)
{
$this->water = $water;
}
//入口方法
public function make(string $image, string $filename = null, int $pos = 9)
{
$res = $this->resource($image);
$water = $this->resource($this->water);
$postion = $this->position($res, $water, $pos);
imagecopy($res, $water, $postion['x'], $postion['y'], 0, 0, imagesx($water), imagesy($water));
$this->showAction($image)($res, $filename ?: $image);
}
//获取资源对象
protected function resource($image)
{
$info = getimagesize($image);
$function = [1 => 'imagecreatefromgif', 2 => 'imagecreatefromjpeg', 3 => 'imagecreatefrompng'];
$call = $function[$info[2]];
return $call($image);
}
//根据类型输出图片
protected function showAction(string $image)
{
$info = getimagesize($image);
$function = [1 => 'imagegif', 2 => 'imagejpeg', 3 => 'imagepng'];
return $function[$info[2]];
}
//位置
protected function position($des, $res, int $pos)
{
$info = ['x' => 0, 'y' => 0];
switch ($pos) {
case 1:
break;
case 2:
$info['x'] = (imagesx($des) - imagesx($res)) / 2;
break;
case 3:
$info['x'] = (imagesx($des) - imagesx($res));
break;
case 4:
$info['y'] = (imagesy($des) - imagesy($res)) / 2;
break;
case 5:
$info['x'] = (imagesx($des) - imagesx($res)) / 2;
$info['y'] = (imagesy($des) - imagesy($res)) / 2;
break;
}
return $info;
}
}
サムネイル
呼ばれます
include 'Resize.php';
$image = new Resize;
$image->make('sun.png', '1.jpeg', 200, 200, 3);
サムネイルソースカテゴリ
<?php
class Resize
{
public function make(string $file, string $to = null, int $width = 200, int $height = 200, int $type = 3)
{
$image = $this->resource($file);
$info = $this->size($width, $height, imagesx($image), imagesy($image), $type);
$res = imagecreatetruecolor($info[0], $info[1]);
imagecopyresampled($res, $image, 0, 0, 0, 0, $info[0], $info[1], $info[2], $info[3]);
header('Content-type:image/jpeg');
imagejpeg($res);
}
protected function size($rw, $rh, $iw, $ih, int $type)
{
switch ($type) {
case 1:
//固定宽度,高度自动
$rh = $rw / $iw * $ih;
break;
case 2:
//高度固定,宽度自动
$rw = $rh / $ih * $iw;
break;
case 3:
//固定宽度,高度裁切
$ih = $iw / $rw * $rh;
break;
default:
if (($iw / $rw) > ($ih / $rh)) {
$iw = $ih / $rh * $rh;
} else {
$ih = $iw / $rw * $rw;
}
}
return [$rw, $rh, $iw, $ih];
}
protected function resource(string $image)
{
if (!file_exists($image) || getimagesize($image) === false) {
throw new Exception("file dont exists or it's not an image file");
}
$functions = [1 => 'imagecreatefromgif', 2 => 'imagecreatefromjpeg', 3 => 'imagecreatefrompng'];
$info = getimagesize($image);
$call = $functions[$info[2]];
return $call($image);
}
}