スタブバナーの生成
このような優れた
placehold.itサービスがあります。 バナースタブイメージを簡単に生成できます。 少し前まで
、彼らはハブについて彼について書いていました 。それ以来、私は彼が定期的に倒れ始めるまで積極的に使ってきました。 夕方、同様の礼拝が私の膝の上に書かれました。誰かが役に立つかもしれません。
構造:
_
cache-画像を保存するディレクトリ。このディレクトリへの書き込み権限が必要です
_
フォント -フォント
___
Verdana.ttf
_
index.php
_
page.tpl.php-ページテンプレート
_
.htaccess
.htaccess
index.phpでURLを処理します
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
-
Copy Source | Copy HTML Options -Indexes Options +FollowSymLinks DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^( .* )$ index.php? q =$ 1 [L,QSA] </IfModule>
index.php
ロジックは単純です。GETパラメーターが存在しない場合は確認します-情報がある場合はページに情報を提供します-パラメーターを確認し、バナーを提供します。
Copy Source | Copy HTML
- //パラメータを確認します
- if ( empty ( $ _GET )){
- get_page();
- }
- elseif ( $ _GET [ 'q' ]){
- $ nocache = isset ( $ _GET [ 'nocache' ])? TRUE:FALSE
- $ arg = explode( '/' 、 $ _GET [ 'q' ]);
- //画像サイズ
- $サイズ = get_size( $ arg [ 0 ]);
- //色
- $ color = get_color( $ arg [ 1 ]、 'CCCCCC' );
- //テキスト
- $ text = isset ( $ arg [ 2 ])? $ arg [ 2 ]: $サイズ ->幅。 'x' 。 $サイズ ->高さ;
- //テキストの色
- $ textcolor = get_color( $ arg [ 3 ]、 '666666' );
- $ filename = get_image_path( $ size 、 $ color 、 $ text 、 $ textcolor 、 $ nocache );
- if ( $ nocache ){
- get_page( array ( 'image' => $ filename ));
- }
- header( "Content-Type:" 。mime_content_type( $ filename ));
- print file_get_contents( $ filename );
- }
nocacheパラメーターは
自分用に残しました-設定されている場合、イメージは
再保存されます-それ以外の場合は、キャッシュから取得されます。
使用する機能
get_size()
この関数は画像サイズをチェックし、寸法のあるオブジェクトを返します。
Copy Source | Copy HTML
- function get_size( $ size 、 $ default = '200x200' ){
- $ clear = isset ( $ size )&& preg_match( "/ ^ [1-9] [0-9] + x [1-9] [0-9] + $ /" 、 $ size )? $サイズ : $デフォルト ;
- $ size = explode( 'x' 、 $ clear );
- $ return = new stdClass ();
- $ return- > width = $ size [ 0 ] < 5000 ? $サイズ [ 0 ]: 5000 ;
- $ return- > height = $ size [ 1 ] < 5000 ? $サイズ [ 1 ]: 5000 ;
- return $ return ;
- }
get_color()
この関数は色をチェックし、色付きのオブジェクトを返します。
Copy Source | Copy HTML
- function get_color( $ color 、 $ default = 'CCCCCC' ){
- $ return = new stdClass ();
- $ rgb = isset ( $ color )&& preg_match( "/ ^ [0-9a-fA-F] {6} $ /" 、 $ color )? strtoupper( $ color ):strtoupper( $ default );
- $ return-> red = hexdec( $ rgb [ 0 ]。 $ rgb [ 1 ]);
- $ return-> green = hexdec( $ rgb [ 2 ]。 $ rgb [ 3 ]);
- $ return-> blue = hexdec( $ rgb [ 4 ]。 $ rgb [ 5 ]);
- $ return- > color = $ rgb ;
- return $ return ;
- }
get_page()
この関数は、テンプレートからページをレンダリングします。
Copy Source | Copy HTML
- 関数 get_page( $変数 = 配列 ()){
- 抽出( $ variables 、EXTR_SKIP);
- ob_start();
- include "./page.tpl.php" ;
- $ contents = ob_get_contents();
- ob_end_clean();
- exit ( $ contents );
- }
get_image_path()
この関数は画像を生成し、そのパスを返します。 ファイルが既に存在する場合、再作成されません。
Copy Source | Copy HTML
- function get_image_path( $ size 、 $ color 、 $ text 、 $ textcolor 、 $ nocache = FALSE){
- //ファイル
- $ dirname = 'cache /' 。 $カラー ->カラー。 '/' $サイズ ->幅。 '/' $サイズ ->高さ。 '/' $ textcolor- >色;
- $ filename = $ dirname 。 '/' .trim( $ text )。 '.png' ;
- if (!is_file( $ filename )|| $ nocache ){
- //必要に応じてディレクトリを作成します
- if (!is_dir( $ dirname )){
- mkdir( $ dirname 、 0777 、TRUE);
- }
- //画像を作成します
- $ image = imagecreatetruecolor( $サイズ ->幅、 $サイズ ->高さ);
- //色
- $ rgb = imagecolorallocate( $ image 、 $ color-> red、 $ color-> green、 $ color-> blue);
- //色で塗りつぶす
- imagefilledrectangle( $ image 、 0、0 、 $ size-> width、 $ size-> height、 $ rgb );
- //テキストサイズ
- $ textsize = 16 ;
- //テキスト座標
- $座標 = imageftbbox(16、0、 'fonts / Arial.ttf' 、 $ text );
- $ x =( $サイズ ->幅- $座標 [ 2 ])/ 2 ;
- $ y =( $サイズ ->高さ- $座標 [ 5 ])/ 2 ;
- //テキストを書く
- $ textrgb = imagecolorallocate( $ image 、 $ textcolor- >赤、 $ textcolor- >緑、 $ textcolor- >青);
- imagefttext( $ image 、 $ textsize 、 0 、 $ x 、 $ y 、 $ textrgb 、 'fonts / Verdana.ttf' 、 $ text );
- //保存
- imagepng( $ image 、 $ filename 、 9 );
- }
- $ filenameを 返し ます 。
- }
page.tpl.php
Copy Source | Copy HTML
- <! DOCTYPE html PUBLIC "-// W3C // DTD XHTML 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- < html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en" dir = "ltr">
- < 頭 >
- < meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
- < タイトル > BannerMaker Beta 1.0 </ / タイトル >
- </ 頭 >
- < 本体 >
- < p >リンクを使用: < strong > http:// <? php print $ _SERVER [ 'HTTP_HOST' ]; ? > / WIDTHxHEIGHT / COLOR / TEXT / TEXTCOLOR </ strong > </ p >
- < p >例: <a href = "http: // <? php print $ _SERVER ['HTTP_HOST'];?> / 468x60 / CCFFAA / Hello world!/ 000000 " > http:// <? php print $ _SERVER [ 'HTTP_HOST' ]; ? > / 468x60 / CCFFAA / Hello world!/ 000000 </ a > </ p >
- <? php if ($ image ):? >
- < p > < img src = "/ < ? php print $ image ;? > " alt = "" /> </ p >
- <? php endif ; ? >
- </ body >
- </ html >
まとめ
リンクは
example.com/WIDTHxHEIGHT/COLOR/TEXT/TEXTCOLORのように見え
ます 。これは便利でシンプルです(COLOR / TEXT / TEXTCOLORは省略可能です)。
もちろん、ライブで使用する場合は、「ディレクトリ/ファイルを作成できた」などのチェックを追加し、「nocache」を削除する価値があります。
このようなサービスが誰かに役立つとうれしいです。
All Articles