CleverWebAdmin

Multi-Line Text-To-Image Function v2

Apr 20th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?PHP
  2. /**
  3.  * PROJECT CLEVERWEB GENERAL LESSER LICENSE INFORMATION v2
  4.  * ------------------------------------------------------------
  5.  * Copyright (C) 2012 Nicholas Jordon
  6.  * http://projcleverweb.com
  7.  *
  8.  * Licenses:
  9.  *   GNU LESSER GENERAL PUBLIC LICENSE VERSION 3
  10.  *   APACHE LICENSE, VERSION 2.0
  11.  *
  12.  * This software is free software: you can redistribute it
  13.  * and/or modify it under both the terms of the GNU Lesser
  14.  * General Public License as published by the Free Software
  15.  * Foundation, version 3 of the License, and the Apache
  16.  * License, version 2.0 of the License; you may not use this
  17.  * software except in compliance with both licenses.
  18.  *
  19.  * You may obtain a copy of the licenses at:
  20.  * @Apache v2.0: http://www.apache.org/licenses/LICENSE-2.0
  21.  * @GNU LGPL v3: http://www.gnu.org/licenses/lgpl-3.0.html
  22.  *
  23.  * Unless required by applicable law or agreed to in writing,
  24.  * software distributed under the licenses is distributed on
  25.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  26.  * KIND, either express or implied. See the licenses for the
  27.  * specific language governing permissions and other details.
  28.  *
  29.  * Verfication Key: e450edcbe7703e40d9edcf19a18fb4bc
  30.  */
  31.  
  32. /**
  33.  * Multi-Line Text-To-Image Function v2
  34.  * ---------------------------------------
  35.  * Summery: Takes a text string and
  36.  *   converts it to an image.
  37.  * Notice: Converts \r to \n regardless
  38.  *   if it is actual EOL delimiter.
  39.  *
  40.  * @Param: (string) $string
  41.  *   The string to be converted.
  42.  * @Param: (int) $font_size
  43.  *   Size of the output text.
  44.  * @Param: (int) $font_color
  45.  *   Hexdecimal color of the font.
  46.  * @Param: (int) $bg_color
  47.  *   Hexdecimal color of the background.
  48.  */
  49.  
  50. function txtimg($string,$font_size=5,$font_color='0',$bg_color='f'){
  51.   if(strpos($string,"\r")){ // unix/windows compatible
  52.     $string = str_replace("\r","\n",$string);
  53.   }
  54.   $lines=explode("\n",$string);
  55.   foreach($lines as $key=>$line){
  56.     $linecount++;
  57.     if(strlen($line)>strlen($longest)){
  58.       $longest = $line;
  59.     }
  60.     // now lets make it easier to work with
  61.     $linearray[$key]=array('txt'=>$line,'len'=>strlen($line));
  62.   }
  63.   $width = imagefontwidth($font_size)*strlen($longest);
  64.   $lheight = imagefontheight($font_size)+2; // 2px between each line
  65.   $height = $lheight*$linecount;
  66.   $img = imagecreate($width,$height);
  67.   function hextorgb($hex){
  68.     $hex=str_replace('#','',$hex);
  69.     $length = strlen($hex);
  70.     if(!$length){return false;} // if null return false
  71.     switch($length){ // full
  72.       case 6:
  73.         $r=$hex[0].$hex[1];$g=$hex[2].$hex[3];$b=$hex[4].$hex[5];
  74.         break;
  75.       case 3: // 1/2
  76.         $r=$hex[0].$hex[0];$g=$hex[1].$hex[1];$b=$hex[2].$hex[2];
  77.         break;
  78.       case 2: // partial
  79.         $r=$hex[0].$hex[1];$g=$hex[0].$hex[1];$b=$hex[0].$hex[1];
  80.         break;
  81.       case 1: // single
  82.         $r=$hex.$hex;$g=$hex.$hex;$b=$hex.$hex;
  83.         break;
  84.     default:
  85.       return false; // if incorrect # of char return false
  86.     }
  87.     return array('r'=>hexdec($r),'g'=>hexdec($g),'b'=>hexdec($b));
  88.   }
  89.   $font_color = hextorgb($font_color);
  90.   $bg_color = hextorgb($bg_color);
  91.   $bg = imagecolorallocate($img,$bg_color['r'],$bg_color['g'],$bg_color['b']);
  92.   $color = imagecolorallocate($img,$font_color['r'],$font_color['g'],$font_color['b']);
  93.   foreach($linearray as $key=>$value){
  94.     $len = $value['len'];
  95.     $string = $value['txt'];
  96.     $ypos = 0;
  97.     if($key){
  98.       $ypos = $key*$lheight;
  99.     }
  100.     imagestring($img,$font_size, 0,$ypos,$string, $color);
  101.   }
  102.   header("Content-Type: image/png"); // loads faster
  103.   imagepng($img);
  104.   imagedestroy($img);
  105. }
  106.  
  107. // example
  108. foreach($_SERVER as $key => $value){
  109.   $str .= $key." = ".$value.PHP_EOL;
  110. }
  111. txtimg($str,4,'#ff0000');
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment