Ignore:
Timestamp:
05/15/08 22:25:54 (4 years ago)
Author:
hverton
Message:

Merged trunk revisions 1342:1407 into the PLi-Fusion branch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PLi-Fusion/includes/font2image.php

    r1343 r1408  
    11<?php 
    22/* 
    3     Dynamic Heading Generator 
    4     Original code by Stewart Rosenberger, http://www.stewartspeak.com/headings/ 
    5  
    6     Converted by WanWizard to work in am ExiteCMS environment 
    7  
    8     usage: $image_resource = font2image($f2i_array); 
    9  
    10     $f2i_array = array(); 
    11     $f2i_array['image'] = "png";                       // Type of image to generate. If not specified, defaults to PNG 
    12     $f2i_array['font_text'] = "Text to convert";       // Text to convert to an image. Required. 
    13     $f2i_array['font_file'] = "font2use.ttf";          // FQN of the TTF font file to use. Required. 
    14     $f2i_array['font_size'] = 10;                      // Font size. Optional. Valid values are 4 to 96. Default = 10. 
    15     $f2i_array['font_color'] = "#000000";              // Font color (html style). Default = black. 
    16     $f2i_array['font_spacing'] = false;                // Use the standard font spacing. Default = false.  
    17                                                        // Disables kerning, outline and shadows!! 
    18     $f2i_array['font_kerning'] = 0;                    // Additional space between characters in pixels. Default = 0, Max = 20. 
    19     $f2i_array['background_color'] = "#FFFFFF";        // Image background color (html style). Default = white. 
    20     $f2i_array['background_shadow_color'] = "#000000"; // Image shadow background color for a 3D look (html style). Default black. 
    21     $f2i_array['background_shadow_width'] = 0;         // Width of the background shadow of the image. Default = 0, Max = 10. 
    22     $f2i_array['outline_color'] = "";                  // Outline color (html style). If not specified, width is set to 0. 
    23     $f2i_array['outline_width'] = 0;                   // Width of the outline of the character in pixels. Default = 0, Max = 10. 
    24     $f2i_array['shadow_color'] = "";                   // Shadow color (html style). If not specified, width is set to 0. 
    25     $f2i_array['shadow_width'] = 0;                    // Width of the shadow of the character in pixels. Default = 0, Max = 10. 
    26     $f2i_array['background_transparent'] = false;      // Should the background color be marked transparent? Boolean, default = false; 
    27     $f2i_array['background_transparent_color'] = "";   // Optional. If defined, this color will be used for transparancy instead of  
    28                                                        // the specified background color. Required if a background shadow is defined 
    29     $f2i_array['cache_images'] = false;                // Cache the image after generation? Boolean, default = false. 
    30     $f2i_array['cache_folder'] = "";                   // Directory to store the cached images in. 
    31                                                        // If not defined or not valid, cache_images will be set to false. 
    32     $f2i_array['cache_prefix'] = false;                // If defined, this will be prepended to the filename of the cached file     
    33     $f2i_array['cache_hash'] = false;                  // Boolean. If false, the text will be used as filename, otherwise a hash is calculated     
    34  
    35     Notes: 
    36     * if the background has a shadow, background transparency is used to create the 3D effect. The text background 
    37       itself can therefore not be transparent. 
     3    Original code by Stewart Rosenberger, http://www.stewartspeak.com/headings/ 
     4     
     5    Converted and extended by WanWizard to work in am ExiteCMS environment 
     6     
     7    usage: $image_resource = font2image($f2i_array); 
     8     
     9    $f2i_array = array(); 
     10    $f2i_array['image'] = "png";                       // Type of image to generate. If not specified, defaults to PNG 
     11    $f2i_array['font_text'] = "Text to convert";       // Text to convert to an image. Required. 
     12    $f2i_array['font_file'] = "font2use.ttf";          // FQN of the TTF font file to use. Required. 
     13    $f2i_array['font_size'] = 10;                      // Font size. Optional. Valid values are 4 to 96. Default = 10. 
     14    $f2i_array['font_color'] = "#000000";              // Font color (html style). Default = black. 
     15    $f2i_array['font_spacing'] = false;                // Use the standard font spacing. Default = false.  
     16                                                       // Disables kerning, outline and shadows!! 
     17    $f2i_array['font_kerning'] = 0;                    // Additional space between characters in pixels. Default = 0, Max = 20. 
     18    $f2i_array['background_color'] = "#FFFFFF";        // Image background color (html style). Default = white. 
     19    $f2i_array['background_shadow_color'] = "#000000"; // Image shadow background color for a 3D look (html style). Default black. 
     20    $f2i_array['background_shadow_width'] = 0;         // Width of the background shadow of the image. Default = 0, Max = 10. 
     21    $f2i_array['outline_color'] = "";                  // Outline color (html style). If not specified, width is set to 0. 
     22    $f2i_array['outline_width'] = 0;                   // Width of the outline of the character in pixels. Default = 0, Max = 10. 
     23    $f2i_array['shadow_color'] = "";                   // Shadow color (html style). If not specified, width is set to 0. 
     24    $f2i_array['shadow_width'] = 0;                    // Width of the shadow of the character in pixels. Default = 0, Max = 10. 
     25    $f2i_array['background_transparent'] = false;      // Should the background color be marked transparent? Boolean, default = false; 
     26    $f2i_array['background_transparent_color'] = "";   // Optional. If defined, this color will be used for transparancy instead of  
     27                                                       // the specified background color. Required if a background shadow is defined 
     28    $f2i_array['cache_images'] = false;                // Cache the image after generation? Boolean, default = false. 
     29    $f2i_array['cache_folder'] = "";                   // Directory to store the cached images in. 
     30                                                       // If not defined or not valid, cache_images will be set to false. 
     31    $f2i_array['cache_prefix'] = false;                // If defined, this will be prepended to the filename of the cached file     
     32    $f2i_array['cache_hash'] = false;                  // Boolean. If false, the text will be used as filename, otherwise a hash is calculated     
     33    $f2i_array['return_link'] = false;                 // If true, the function returns a relative link to the cached image instead of the 
     34                                                       // image itself. If true, cache_images MUST be set to true as well!!! 
     35     
     36    Notes: 
     37    * if the background has a shadow, background transparency is used to create the 3D effect. The text background 
     38      itself can therefore not be transparent. 
    3839 
    3940*/ 
    40 if (!defined("INIT_CMS_OK")) { header("Location: ../index.php"); exit; }  
     41if (eregi("font2image.php", $_SERVER['PHP_SELF']) || !defined('INIT_CMS_OK')) die(); 
    4142 
    4243function font2image($font2image) { 
     
    9394    if (!isset($font2image['background_transparent'])) $font2image['background_transparent'] = false; 
    9495    if (!isset($font2image['cache_images'])) $font2image['cache_images'] = false; 
     96    if (!isset($font2image['return_link'])) $font2image['return_link'] = false; 
    9597    if (!isset($font2image['cache_folder'])) $font2image['cache_images'] = false; 
    96     if ($font2image['cache_folder'] !="" && !is_dir($font2image['cache_folder'])) fatal_error('Cache folder does not exist.'); 
     98    if ($font2image['cache_folder'] !="" && !is_dir($font2image['cache_folder'])) fatal_error("Cache folder does not exist."); 
    9799    if (!isset($font2image['cache_prefix']) || $font2image['cache_prefix'] == false) $font2image['cache_prefix'] = ""; 
    98100    if (!isset($font2image['cache_hash'])) $font2image['cache_hash'] = false; 
     101    if ($font2image['return_link'] && !$font2image['cache_images']) fatal_error("Can't link without cache."); 
    99102 
    100103    if (!isset($font2image['font_spacing'])) $font2image['font_spacing'] = false; 
     
    121124    } 
    122125 
     126    // Create the filename for this image, use hash if requested 
     127    if ($font2image['cache_hash']) { 
     128        $hash = ""; 
     129        foreach ($font2image as $element) { 
     130            $hash .= $element; 
     131        } 
     132        $hash = md5($hash); 
     133    } else { 
     134        $hash = str_replace(' ', '_', $font2image['font_text']); 
     135    } 
     136    $cache_filename = $font2image['cache_folder'] . '/' . $font2image['cache_prefix'] . $hash . '.' . $font2image['image'] ; 
     137     
    123138    // Do we have caching enabled for this image? 
    124139    if ($font2image['cache_images']) { 
    125140        // look for cached copy, if it exists, convert it to a resource and return it 
    126         if ($font2image['cache_hash']) { 
    127             $hash = ""; 
    128             foreach ($font2image as $element) { 
    129                 $hash .= $element; 
    130             } 
    131             $hash = md5($hash); 
    132         } else { 
    133             $hash = str_replace(' ', '_', $font2image['font_text']); 
    134         } 
    135         $cache_filename = $font2image['cache_folder'] . '/' . $font2image['cache_prefix'] . $hash . '.' . $font2image['image'] ; 
    136141        if($font2image['cache_images'] && is_readable($cache_filename)) { 
    137142            // convert the file to a resource 
     
    140145                fatal_error("Cached image file '".$cache_filename."' is not a valid ".strtoupper($font2image['image'])." image!"); 
    141146            } 
    142             switch ($font2image['image']) { 
    143                 case "png": 
    144                     $image = imagecreatefrompng($cache_filename); 
    145                     break; 
    146                 case "jpg": 
    147                     $image = imagecreatefromjpeg($cache_filename); 
    148                     break; 
    149                 case "gif": 
    150                     $image = imagecreatefromgif($cache_filename); 
    151                     break; 
     147            if ($font2image['return_link']) { 
     148                // check if the cache path is inside the webroot 
     149                if (substr($cache_filename,0,strlen(PATH_ROOT)) != PATH_ROOT) { 
     150                    fatal_error("Cached image is not inside the webroot!"); 
     151                } else { 
     152                    return substr($cache_filename, strlen(PATH_ROOT)-1); 
     153                } 
     154            } else { 
     155                switch ($font2image['image']) { 
     156                    case "png": 
     157                        $image = imagecreatefrompng($cache_filename); 
     158                        break; 
     159                    case "jpg": 
     160                        $image = imagecreatefromjpeg($cache_filename); 
     161                        break; 
     162                    case "gif": 
     163                        $image = imagecreatefromgif($cache_filename); 
     164                        break; 
     165                } 
     166                return $image; 
    152167            } 
    153             return $image; 
    154168        } 
    155169    } 
     
    182196        fatal_error('The server could not create this image.') ; 
    183197    } 
    184  
    185198    // give the image a background color 
    186199    if (isset($font2image['background_transparent_color'])) { 
     
    191204    $font2image['background_transparent_color'] = @ImageColorAllocate($image, $background_rgb['red'], $background_rgb['green'], $background_rgb['blue']) ; 
    192205    imagefill($image, 0,0, $font2image['background_transparent_color']); 
     206    // set transparency 
     207    if ($font2image['background_transparent']) { 
     208        ImageColorTransparent($image, $font2image['background_transparent_color']) ; 
     209    } 
     210    // background color and background shadow color 
    193211    $background_rgb = hex_to_rgb($font2image['background_shadow_color']) ; 
    194212    $font2image['background_shadow_color'] = @ImageColorAllocate($image, $background_rgb['red'], $background_rgb['green'], $background_rgb['blue']) ; 
     
    250268    } 
    251269 
    252     // set transparency 
    253     if ($font2image['background_transparent']) { 
    254         ImageColorTransparent($image, $font2image['background_transparent_color']) ; 
    255     } 
    256  
    257270    // write it to the cache directory if needed 
    258271    if ($font2image['cache_images']) { 
     
    269282        } 
    270283    } 
    271 //  imagePNG($image);die(); 
    272     return $image; 
     284    if ($font2image['return_link']) { 
     285        // check if the cache path is inside the webroot 
     286        if (substr($cache_filename,0,strlen(PATH_ROOT)) != PATH_ROOT) { 
     287            fatal_error("Cached image is not inside the webroot!"); 
     288        } else { 
     289            return substr($cache_filename, strlen(PATH_ROOT)-1); 
     290        } 
     291    } else { 
     292        return $image; 
     293    } 
    273294} 
    274295 
Note: See TracChangeset for help on using the changeset viewer.