Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * PROJECT CLEVERWEB GENERAL LESSER LICENSE INFORMATION v3.1
- * ------------------------------------------------------------
- * Copyright (C) 2011-2012 Nicholas Jordon
- * All rights reserved
- * Package: Project CleverWeb
- * http://ProjectCleverWeb.com
- *
- * License:
- * GNU LESSER GENERAL PUBLIC LICENSE VERSION 3
- *
- * This software is free software: you can redistribute it
- * and/or modify it under the terms of the GNU Lesser
- * General Public License as published by the Free Software
- * Foundation, version 3 of the License, You may not use this
- * software except in compliance with the license.
- *
- * You may obtain a copy of the license at:
- * http://www.gnu.org/licenses/lgpl-3.0.html
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the license is distributed on
- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.
- *
- * Verfication Key: e450edcbe7703e40d9edcf19a18fb4bc
- */
- /**
- * Multi-Line Text-To-Image Function
- * ---------------------------------------
- * Takes a text string and converts it to
- * an image.
- * @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 = bcmul($lheight,$linecount);
- $img = imagecreate($width,$height);
- function hextorgb($hex){
- $hex=str_replace('#','',$hex);
- preg_match("/^#{0,1}([0-9a-f]{1,6})$/i",$hex,$match);
- if(!isset($match[1])){return false;}
- if(strlen($match[1])==6){
- list($r, $g, $b) = array($hex[0].$hex[1],$hex[2].$hex[3],$hex[4].$hex[5]);
- }
- elseif(strlen($match[1])==3){
- list($r, $g, $b) = array($hex[0].$hex[0],$hex[1].$hex[1],$hex[2].$hex[2]);
- }
- else if(strlen($match[1])==2){
- list($r, $g, $b) = array($hex[0].$hex[1],$hex[0].$hex[1],$hex[0].$hex[1]);
- }
- else if(strlen($match[1])==1){
- list($r, $g, $b) = array($hex.$hex,$hex.$hex,$hex.$hex);
- }
- else {
- return false;
- }
- $color = array();
- $color['r'] = hexdec($r);
- $color['g'] = hexdec($g);
- $color['b'] = hexdec($b);
- return $color;
- }
- $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 = bcmul($key,$lheight);
- }
- imagestring($img,$font_size, 0,$ypos,$string, $color);
- }
- header("Content-Type: image/png"); // loads faster
- imagepng($img);
- imagedestroy($img);
- }
- // example
- txtimg(
- "This is your IP: ".$_SERVER['REMOTE_ADDR'].PHP_EOL.
- "This is your User-Agent: ".$_SERVER['HTTP_USER_AGENT'],
- 4,'#ff0000','#444444');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment