PHP Image with random quotes from text file
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <?php /*########### About This File ##################*\ # Script : PHP Quote Image # Version : 1.0 # Author : Tanin -TCorp- E # Website : TCorp - http://magnablog.0xtc.com/ # Last modified : 3:07 AM 4/18/2005 # # Comments? : /story318/ \*################################################*/ /*################################################*\ Create a list of image files \*################################################*/ $selectedImage = "/bases2/orange2.png"; $foldername = "/bases2"; // relative path if ($handle = opendir( dirname(__FILE__).$foldername)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (eregi(".jpg$",$file) or eregi(".png$",$file) or eregi(".gif$",$file)) { $filelist[] = $file; } } } closedir($handle); } /*################################################*\ Select a random image file \*################################################*/ $selectedImage=$filelist[rand(0, count($filelist)-1)]; $selectedImage = $foldername ."/".$selectedImage; /*################################################*\ create a copy of the image in memory \*################################################*/ if (eregi(".jpg$",$selectedImage)) $image = imagecreatefromjpeg(dirname(__FILE__).$selectedImage); if (eregi(".png$",$selectedImage)) $image = imagecreatefrompng (dirname(__FILE__).$selectedImage); if (eregi(".gif$",$selectedImage)) $image = imagecreatefromgif (dirname(__FILE__).$selectedImage); /*################################################*\ Set text color \*################################################*/ $text_color = "DDF1FF"; sscanf($text_color, "%2x%2x%2x", $red, $green, $blue); $text_color = ImageColorAllocate($image,$red,$green,$blue); $text_color = -$text_color; // We don't want antialising for this image. $text = "This is along string that will go inside a text box. It'll automatically wrap the way I want it...or ELSE!! | TCorp"; //default quote line /*################################################*\ Open and read quote file. One quote per line. Format: QUOTE | NAME \*################################################*/ $qfile = "quotes.txt"; if (file_exists($qfile)){ $handle = fopen($qfile, "r"); $qfile_contents = fread($handle, filesize($qfile)); $lines_array = explode("\n",$qfile_contents); fclose($handle); $qline_index = (mt_rand(1, sizeof($lines_array)) - 1); if (strlen($lines_array[$qline_index])>1) { $text = $lines_array[$qline_index]; } } $text2 = explode(" | ", $text); $text = "\"".$text2[0]."\""."\n -".$text2[1]; /*################################################*\ Settings \*################################################*/ $maxwidth = 47; $text_v_align="middle"; // Sets how the text is vertically aligned - top / middle / bottom $font_face = "slkscr.ttf"; // Good ol' Silkscreen font $font_size = 6; // no need to change this. $font_height = 11; // Is actually the height per line $linecount = 1; // Default (no need to change this) $textarray = explode("\n",$text); // make a line list $lines = Array(); // Empty array $brcount = 0; // Default (no need to change this) $longest =0; // Default (no need to change this) /*################################################*\ Create wrapped lines of text \*################################################*/ foreach ($textarray as $textline) { $linearray = explode(" ",$textline); foreach ($linearray as $word){ if (strlen($lines[$linecount].$word." ")>$maxwidth) $linecount; $lines[$linecount] .= $word ." "; } $linecount; } /*################################################*\ Calc v-alignment \*################################################*/ if ($text_v_align=="middle"){ //predict top of first line for vertical "middle" alignment. $firstTop = round ( ( ( imagesy($image) ) /2 ) - ( ( ( ( ( sizeof($lines) $brcount ) ) * $font_height ) $font_size )/2 ) ); } elseif ($text_v_align=="bottom"){ //predict top of first line for vertical "bottom" alignment. $firstTop = round((imagesy( $image )) - (( sizeof( $lines ) $brcount)*$font_height ) $font_size ); } else { //predict top of first line for vertical "top" alignment. $firstTop = 0; } /*################################################*\ Write text to image, set the correct content type in the head, output image data and get the hell outta here! \*################################################*/ $text_top = 0; // default (no need to change this) foreach ($lines as $line){ ImageTTFText ($image, $font_size, 0, 6 $textXFirst, $text_top $firstTop $font_height, $text_color, $_SERVER['DOCUMENT_ROOT'] . "/sig/".$font_face, $line); $text_top = $text_top $font_height; } if (eregi(".jpg$",$selectedImage)) { header("Content-type: image/jpeg"); imagejpeg($image); } if (eregi(".png$",$selectedImage)) { header("Content-type: image/png"); imagepng($image); } if (eregi(".gif$",$selectedImage)) { header("Content-type: image/gif"); imagegif($image); } imagedestroy($image); exit; ?> |
Comment on this
0xTC.com is powered by MagnaBlog (C) Written by Tanin,
provided in valid XHTML1.0 Strict,
shown in CSS v2. It is viewed best in a real browser with javascript enabled
and is also availible in RSS format. 0xTC (C) 2004 - 2007 Tanin Ehrami.
