Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * PROJECT CLEVERWEB GENERAL LESSER LICENSE INFORMATION v2
- * ------------------------------------------------------------
- * Copyright (C) 2012 Nicholas Jordon
- * http://projcleverweb.com
- *
- * Licenses:
- * GNU LESSER GENERAL PUBLIC LICENSE VERSION 3
- * APACHE LICENSE, VERSION 2.0
- *
- * This software is free software: you can redistribute it
- * and/or modify it under both the terms of the GNU Lesser
- * General Public License as published by the Free Software
- * Foundation, version 3 of the License, and the Apache
- * License, version 2.0 of the License; you may not use this
- * software except in compliance with both licenses.
- *
- * You may obtain a copy of the licenses at:
- * @Apache v2.0: http://www.apache.org/licenses/LICENSE-2.0
- * @GNU LGPL v3: http://www.gnu.org/licenses/lgpl-3.0.html
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the licenses is distributed on
- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the licenses for the
- * specific language governing permissions and other details.
- *
- * Verfication Key: e450edcbe7703e40d9edcf19a18fb4bc
- */
- /**
- * Multi-Line Text-To-Image Function v2
- * ---------------------------------------
- * Summery: Takes a text string and
- * converts it to an image.
- * Notice: Converts \r to \n regardless
- * if it is actual EOL delimiter.
- *
- * @Param: (string) $string
- * The string to be converted.
- * @Param: (int) $font_size
- * Size of the output text.
- * @Param: (int) $font_color
- * Hexdecimal color of the font.
- * @Param: (int) $bg_color
- * Hexdecimal color of the background.
- */
- function txtimg($string,$font_size=5,$font_color='0',$bg_color='f'){
- if(strpos($string,"\r")){ // unix/windows compatible
- $string = str_replace("\r","\n",$string);
- }
- $lines=explode("\n",$string);
- foreach($lines as $key=>$line){
- $linecount++;
- if(strlen($line)>strlen($longest)){
- $longest = $line;
- }
- // now lets make it easier to work with
- $linearray[$key]=array('txt'=>$line,'len'=>strlen($line));
- }
- $width = imagefontwidth($font_size)*strlen($longest);
- $lheight = imagefontheight($font_size)+2; // 2px between each line
- $height = $lheight*$linecount;
- $img = imagecreate($width,$height);
- function hextorgb($hex){
- $hex=str_replace('#','',$hex);
- $length = strlen($hex);
- if(!$length){return false;} // if null return false
- switch($length){ // full
- case 6:
- $r=$hex[0].$hex[1];$g=$hex[2].$hex[3];$b=$hex[4].$hex[5];
- break;
- case 3: // 1/2
- $r=$hex[0].$hex[0];$g=$hex[1].$hex[1];$b=$hex[2].$hex[2];
- break;
- case 2: // partial
- $r=$hex[0].$hex[1];$g=$hex[0].$hex[1];$b=$hex[0].$hex[1];
- break;
- case 1: // single
- $r=$hex.$hex;$g=$hex.$hex;$b=$hex.$hex;
- break;
- default:
- return false; // if incorrect # of char return false
- }
- return array('r'=>hexdec($r),'g'=>hexdec($g),'b'=>hexdec($b));
- }
- $font_color = hextorgb($font_color);
- $bg_color = hextorgb($bg_color);
- $bg = imagecolorallocate($img,$bg_color['r'],$bg_color['g'],$bg_color['b']);
- $color = imagecolorallocate($img,$font_color['r'],$font_color['g'],$font_color['b']);
- foreach($linearray as $key=>$value){
- $len = $value['len'];
- $string = $value['txt'];
- $ypos = 0;
- if($key){
- $ypos = $key*$lheight;
- }
- imagestring($img,$font_size, 0,$ypos,$string, $color);
- }
- header("Content-Type: image/png"); // loads faster
- imagepng($img);
- imagedestroy($img);
- }
- // example
- foreach($_SERVER as $key => $value){
- $str .= $key." = ".$value.PHP_EOL;
- }
- txtimg($str,4,'#ff0000');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment