スタブバナーの生成







このような優れた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>



  1. 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>



  2. 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>



  3. 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>



  4. 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>



  5. 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>



  6. 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>



  7. 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>



  8. 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>



  9. 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>



  10. 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



  1. //パラメータを確認します
  2. ifempty$ _GET )){
  3. get_page();
  4. }
  5. elseif$ _GET [ 'q' ]){
  6. $ nocache = isset$ _GET [ 'nocache' ])? TRUE:FALSE
  7. $ arg = explode( '/'$ _GET [ 'q' ]);
  8. //画像サイズ
  9. $サイズ = get_size( $ arg [ 0 ]);
  10. //色
  11. $ color = get_color( $ arg [ 1 ]、 'CCCCCC' );
  12. //テキスト
  13. $ text = isset$ arg [ 2 ])? $ arg [ 2 ]: $サイズ ->幅。 'x'$サイズ ->高さ;
  14. //テキストの色
  15. $ textcolor = get_color( $ arg [ 3 ]、 '666666' );
  16. $ filename = get_image_path( $ size$ color$ text$ textcolor$ nocache );
  17. if$ nocache ){
  18. get_page( array'image' => $ filename ));
  19. }
  20. header( "Content-Type:" 。mime_content_type( $ filename ));
  21. print file_get_contents( $ filename );
  22. }


nocacheパラメーターは自分用に残しました-設定されている場合、イメージは再保存されます-それ以外の場合は、キャッシュから取得されます。



使用する機能



get_size()


この関数は画像サイズをチェックし、寸法のあるオブジェクトを返します。

Copy Source | Copy HTML



  1. function get_size( $ size$ default = '200x200' ){
  2. $ clear = isset$ size )&& preg_match( "/ ^ [1-9] [0-9] + x [1-9] [0-9] + $ /"$ size )? $サイズ$デフォルト ;
  3. $ size = explode( 'x'$ clear );
  4. $ return = new stdClass ();
  5. $ return- > width = $ size [ 0 ] < 5000$サイズ [ 0 ]: 5000 ;
  6. $ return- > height = $ size [ 1 ] < 5000$サイズ [ 1 ]: 5000 ;
  7. return $ return ;
  8. }




get_color()


この関数は色をチェックし、色付きのオブジェクトを返します。

Copy Source | Copy HTML



  1. function get_color( $ color$ default = 'CCCCCC' ){
  2. $ return = new stdClass ();
  3. $ rgb = isset$ color )&& preg_match( "/ ^ [0-9a-fA-F] {6} $ /"$ color )? strtoupper( $ color ):strtoupper( $ default );
  4. $ return-> red = hexdec( $ rgb [ 0 ]。 $ rgb [ 1 ]);
  5. $ return-> green = hexdec( $ rgb [ 2 ]。 $ rgb [ 3 ]);
  6. $ return-> blue = hexdec( $ rgb [ 4 ]。 $ rgb [ 5 ]);
  7. $ return- > color = $ rgb ;
  8. return $ return ;
  9. }




get_page()


この関数は、テンプレートからページをレンダリングします。

Copy Source | Copy HTML



  1. 関数 get_page( $変数 = 配列 ()){
  2. 抽出( $ variables 、EXTR_SKIP);
  3. ob_start();
  4. include "./page.tpl.php" ;
  5. $ contents = ob_get_contents();
  6. ob_end_clean();
  7. exit$ contents );
  8. }




get_image_path()


この関数は画像を生成し、そのパスを返します。 ファイルが既に存在する場合、再作成されません。

Copy Source | Copy HTML



  1. function get_image_path( $ size$ color$ text$ textcolor$ nocache = FALSE){
  2. //ファイル
  3. $ dirname = 'cache /'$カラー ->カラー。 '/' $サイズ ->幅。 '/' $サイズ ->高さ。 '/' $ textcolor- >色;
  4. $ filename = $ dirname'/' .trim( $ text )。 '.png' ;
  5. if (!is_file( $ filename )|| $ nocache ){
  6. //必要に応じてディレクトリを作成します
  7. if (!is_dir( $ dirname )){
  8. mkdir( $ dirname0777 、TRUE);
  9. }
  10. //画像を作成します
  11. $ image = imagecreatetruecolor( $サイズ ->幅、 $サイズ ->高さ);
  12. //色
  13. $ rgb = imagecolorallocate( $ image$ color-> red、 $ color-> green、 $ color-> blue);
  14. //色で塗りつぶす
  15. imagefilledrectangle( $ image0、0$ size-> width、 $ size-> height、 $ rgb );
  16. //テキストサイズ
  17. $ textsize = 16 ;
  18. //テキスト座標
  19. $座標 = imageftbbox(16、0、 'fonts / Arial.ttf'$ text );
  20. $ x =( $サイズ ->幅- $座標 [ 2 ])/ 2 ;
  21. $ y =( $サイズ ->高さ- $座標 [ 5 ])/ 2 ;
  22. //テキストを書く
  23. $ textrgb = imagecolorallocate( $ image$ textcolor- >赤、 $ textcolor- >緑、 $ textcolor- >青);
  24. imagefttext( $ image$ textsize0$ x$ y$ textrgb'fonts / Verdana.ttf'$ text );
  25. //保存
  26. imagepng( $ image$ filename9 );
  27. }
  28. $ filenameを 返し ます
  29. }




page.tpl.php



Copy Source | Copy HTML



  1. <! DOCTYPE html PUBLIC "-// W3C // DTD XHTML 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. < html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en" dir = "ltr">
  3. < >
  4. < meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
  5. < タイトル > BannerMaker Beta 1.0 </ / タイトル >
  6. </ >
  7. < 本体 >
  8. < p >リンクを使用: < strong > http:// <? php print $ _SERVER [ 'HTTP_HOST' ]; ? > / WIDTHxHEIGHT / COLOR / TEXT / TEXTCOLOR </ strong > </ p >
  9. < 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 >
  10. <? php if ($ image ):? >
  11. < p > < img src = "/ <php print $ image ;? > " alt = "" /> </ p >
  12. <? php endif ; ? >
  13. </ body >
  14. </ html >




まとめ



リンクはexample.com/WIDTHxHEIGHT/COLOR/TEXT/TEXTCOLORのように見えます 。これは便利でシンプルです(COLOR / TEXT / TEXTCOLORは省略可能です)。



もちろん、ライブで使用する場合は、「ディレクトリ/ファイルを作成できた」などのチェックを追加し、「nocache」を削除する価値があります。



このようなサービスが誰かに役立つとうれしいです。



All Articles