CleverWebAdmin

Multi-Line Text-To-Image

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