検索
ホームページphp教程php手册「PHP プログラミングを理解する最速の方法」講義 7: PHP 画像検証コードとサムネイル

例 22 画像検証のコア コード

コードをコピー コードは次のとおりです:


/ /header( "コンテンツタイプ:画像/png");
$num ='1234';
$imageheight=18; ($imagewidth ,$imageheight);
imagecolorallocate($numimage,240,240,240);
for($i=0;$i$x = mt_rand( 1,8 ) $imagewidth*$i/4;
$y = mt_rand(1,$imageheight/4);
$color=imagecolorallocate($numimage,mt_rand(0,150),mt_rand) (0,150) );
imagestring($numimage,5,$x,$y,$num[$i],$color);
}

for($i=0;$ i$randcolor=imagecolorallocate($numimage,rand(200,255),rand(200,255),rand(200,255));
imagesetpixel($numimage,rand()p,rand() ) ,$ randcolor);
imagepng($numimage);
?>これは 4 を出力する例です漢字の場合は、フォント ファイルと imagettftext 関数が必要です。これらを使用する場合は、オンラインで検索できます。乱数を生成したい場合は mt_rand 関数が必要で、乱数を utf-8 に変換する必要がある場合は session を使用する必要があります。

例 23 サムネイル



コードをコピー


コードは次のとおりです。


class SimpleImage { var $image; functionload($filename) { $this->image_type = $image_info [2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); elseif( $this->image_type == IMAGETYPE_GIF ) {

$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng( $filename );
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename); } elseif ( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename, $permissions );
}
}
関数出力($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image); 🎜> } elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image)
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image) ;
}
}
関数 getWidth() {
戻り画像 x($this->image);
}
関数 getHeight() {
戻り画像this- >image);
}
function raiseToHeight($height) {
$ratio = $height / $this->getHeight(); getWidth( ) * $ratio;
$this->resize($width,$height);
}
関数 raiseToWidth($width) {
$ratio = $width / $this- > getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height); ($ スケール) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() *
$this; -> ;resize($width,$height);
}
関数 raise($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($) new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); image = $new_image;
}
}

$newfile = UPLOAD_DIR."/icons/".md5($_SESSION['USER']->email).".jpg"; // アップロードされたファイルが保存されるディレクトリ
$image = new SimpleImage()
$image->load($_FILES['icons']['tmp_name']);// アップロードされた一時ファイル名
$image->resizeToWidth(80);幅を設定
$image->save($newfile);


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)