📜 ⬆️ ⬇️

Generating stub banners


There is such a great placehold.it service. It makes it easy to generate stub images for banners. Not so long ago, they wrote about him on a habr , and since then I actively used it until it began to fall periodically. In the evening, a similar service was written on my knee, maybe someone will come in handy.


Structure:


_ cache - directory for saving images, you need write access to this directory.
_ fonts - fonts
___ Verdana.ttf
_ index.php
_ page.tpl.php - page template
_ .htaccess

.htaccess


We process URL in index.php
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


The logic is simple: check the GET parameters, if they are not - give the page with the information, if there is - check the parameters and give the banner.
Copy Source | Copy HTML
  1. // Check the parameters
  2. if ( empty ( $ _GET )) {
  3. get_page ();
  4. }
  5. elseif ( $ _GET [ 'q' ]) {
  6. $ nocache = isset ( $ _GET [ 'nocache' ])? TRUE: FALSE;
  7. $ arg = explode ( '/' , $ _GET [ 'q' ]);
  8. // Image Size
  9. $ size = get_size ( $ arg [ 0 ]);
  10. // Colour
  11. $ color = get_color ( $ arg [ 1 ], 'CCCCCC' );
  12. // Text
  13. $ text = isset ( $ arg [ 2 ])? $ arg [ 2 ]: $ size -> width. 'x' . $ size -> height;
  14. // Text color
  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. }

For myself, I left the nocache parameter - if it is set, the image will be re - saved anyway, otherwise it will be taken from the cache.

Functions Used


get_size ()

The function checks the image size and returns an object with dimensions.
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 )? $ size : $ default ;
  3. $ size = explode ( 'x' , $ clear );
  4. $ return = new stdClass ();
  5. $ return -> width = $ size [ 0 ] < 5000 ? $ size [ 0 ]: 5000 ;
  6. $ return -> height = $ size [ 1 ] < 5000 ? $ size [ 1 ]: 5000 ;
  7. return $ return ;
  8. }


get_color ()

The function checks the color and returns an object with colors.
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 ()

The function renders a page from a template.
Copy Source | Copy HTML
  1. function get_page ( $ variables = array ()) {
  2. extract ( $ 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 ()

The function generates an image and returns the path to it. If the file already exists, it is not recreated.
Copy Source | Copy HTML
  1. function get_image_path ( $ size , $ color , $ text , $ textcolor , $ nocache = FALSE) {
  2. // File
  3. $ dirname = 'cache /' . $ color -> color. '/' . $ size -> width. '/' . $ size -> height. '/' . $ textcolor -> color;
  4. $ filename = $ dirname . '/' .trim ( $ text ). '.png' ;
  5. if (! is_file ( $ filename ) || $ nocache ) {
  6. // Create a directory if necessary
  7. if (! is_dir ( $ dirname )) {
  8. mkdir ( $ dirname , 0777 , TRUE);
  9. }
  10. // Create an image
  11. $ image = imagecreatetruecolor ( $ size -> width, $ size -> height);
  12. // Colour
  13. $ rgb = imagecolorallocate ( $ image , $ color -> red, $ color -> green, $ color -> blue);
  14. // Fill with color
  15. imagefilledrectangle ( $ image , 0 , 0 , $ size -> width, $ size -> height, $ rgb );
  16. // Text Size
  17. $ textsize = 16 ;
  18. // Text coordinates
  19. $ coordinates = imageftbbox ( 16 , 0 , 'fonts / Arial.ttf' , $ text );
  20. $ x = ( $ size -> width - $ coordinates [ 2 ]) / 2 ;
  21. $ y = ( $ size -> height - $ coordinates [ 5 ]) / 2 ;
  22. // Writing Text
  23. $ textrgb = imagecolorallocate ( $ image , $ textcolor -> red, $ textcolor -> green, $ textcolor -> blue);
  24. imagefttext ( $ image , $ textsize , 0 , $ x , $ y , $ textrgb , 'fonts / Verdana.ttf' , $ text );
  25. // Save
  26. imagepng ( $ image , $ filename , 9 );
  27. }
  28. return $ 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. < head >
  4. < meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
  5. < title > BannerMaker Beta 1.0 </ title >
  6. </ head >
  7. < body >
  8. < p > Use link: < strong > http: // <? php print $ _SERVER [ 'HTTP_HOST' ]; ? > / WIDTHxHEIGHT / COLOR / TEXT / TEXTCOLOR </ strong > </ p >
  9. < p > Example: <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 >


Total


The link looks like example.com/WIDTHxHEIGHT/COLOR/TEXT/TEXTCOLOR , which is convenient and simple (you can omit COLOR / TEXT / TEXTCOLOR).

Of course, with live use, it is worth adding checks like “could create a directory / file”, remove “nocache”.

I would be glad if someone would need such a service.

Source: https://habr.com/ru/post/104724/


All Articles