Generador de firmas
Escoge una (o varias) cita (frase) aleatoria de una lista y la transforma en imagen mediante las librerías GD.
Ejemplo: firma del usuario que me dio la idea, parmacenda.
Descargar código fuente: firma.zip
Código:
<?php
// Edit this file to set up your signature
$width=600; // tamaño (máximo de la imagen) en px (usar con sensatez)
$fontsize = 2; // tamaño del texto (1 es el más pequeño, 2 por defecto)
$spacing = 1; // filas en blanco entre citas (cero no deja espacio entre cada cita)
$totalstrings = 1; // número total de citas a mostrar a la vez
$left_margin = 5; // margen izquierdo (desplazamiento hacia la derecha de todas las citas).
$RGB_quote = array( 70, 70, 255); // color de la cita en formato RGB (rojo, verde, azul; del 0 al 255),
$RGB_author = array( 0, 100, 100); // color del autor en formato RGB (rojo, verde, azul; del 0 al 255),
// Editar la siguiente matriz añadiendo por fila una cita, la delimitación " - " (sin comillas) y el autor (delimitador y autor no obligatorios).
// La matriz no tiene una longitud fija (se puede acortar o alargar según la necesidad). Cuidado con mantener el formato indicado.
$avatar_strings = array(
"CITA1- AUTOR1",
"CITA2- AUTOR2",
"CITA3- AUTOR3",
"CITA_ETC - AUTOR_ETC");
include 'firma.php';
?>firma.php
<?php
// Make random-quotes signature!!!
// By filiptc - with passive contrib from php.net comments
// DO NOT EDIT THIS FILE (unless you know what you are doing)
function ImageStringWrap($imagevar, $font, $x, $y, $text, $color1, $color2, $maxwidth) { // linejumping adjustments (see line 149 at the bottom of the page)
if (preg_match("/ - /", $text)) {
$dividedtext = explode(" - ", $text);
$d_text = $dividedtext[0];
$d_author = " - ".$dividedtext[1];
} else {
$d_text = $text;
$d_author = "";
}
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
if ($maxwidth) {
$maxcharsperline = floor(($maxwidth) / $font_width);
$text = wordwrap($d_text, $maxcharsperline, "\n", 1);
}
$lines = explode("\n", $text);
foreach ($lines as $numline => $line) {
if ($dividedtext and $numline == count($lines)-1) {
$lastline = $lines[(count($lines)-1)];
if (strlen($lastline) == $maxcharsperline) {
$line_subsplit = str_split($lastline, $maxcharsperline*0.85);
ImageString($imagevar, $font, $x, $y, $line_subsplit[0], $color1);
$y = $y+$font_height;
ImageString($imagevar, $font, $x, $y, $line_subsplit[1], $color1);
$length_in_px = strlen($line_subsplit[1])* $font_width;
ImageString($imagevar, $font, $x+$length_in_px, $y, $d_author, $color2);
} elseif (strlen($lastline) + strlen($d_author) > $maxcharsperline) {
$lastline_maxlength = strlen($lastline) -1;
$lastline = wordwrap($line, $lastline_maxlength, "\n", 1);
$lastline = explode("\n", $lastline);
ImageString($imagevar, $font, $x, $y, $lastline[0], $color1);
$y = $y+$font_height;
ImageString($imagevar, $font, $x, $y, $lastline[1], $color1);
$length_in_px = strlen($lastline[1])* $font_width;
ImageString($imagevar, $font, $x+$length_in_px, $y, $d_author, $color2);
} else {
ImageString($imagevar, $font, $x, $y, $line, $color1);
$length_in_px = strlen($line)* $font_width;
ImageString($imagevar, $font, $x+$length_in_px, $y, $d_author, $color2);
}
} else {
ImageString($imagevar, $font, $x, $y, $line, $color1);
}
$y += $font_height;
}
}
$fontwidth = imagefontwidth($fontsize); // establish each character's width (in px) for later use
$fontheight = imagefontheight($fontsize); // establish each character's height (in px) for later use
$spacing = $spacing * $fontheight;
if ($totalstrings >= count($avatar_strings)) { // in case the user chooses to display more quotes than available
$totalstrings = count($avatar_strings); // set the amount of quotes displayed to the total number of quotes
}
if ($totalstrings > 1) {
while (count($rnd_set) < $totalstrings) { // we build an array of x rows (where x is the number of quotations intended to show) filled with random numbers (see below)
$random = rand(0, count($avatar_strings)-1); // generate a random number from zero to the total amount of quotes (minus one because row one is row zero)
$rnd_set[] = $random; // fill the curent row with a random number of citation
$rnd_set = array_values(array_unique($rnd_set)); // reorganize the array so that there are no duplicates
}
} else { // the if-else makes the code faster
$rnd_set[] = rand(0, count($avatar_strings)-1); // generate the random quote
}
for ($i = 0; $i <= $totalstrings; $i++) { // lets calculate how long the strings are (in px)
$rnd_str[] = $avatar_strings[$rnd_set[$i]]; // get the random string
$stringlenghts[] = strlen($rnd_str[$i]) * $fontwidth; // calculate string length in px
}
$longeststring = max($stringlenghts)+$left_margin; // check for the longest string and fetch its size (summed to the left margin)
if ($longeststring <=$width) {
$x_size = $longeststring; // if the longest string is shorter than the specified image width set its value as a new variable
} else {
$x_size = $width; // otherwise keep the specified image width
}
foreach ($rnd_str as $key => $strg) {
// the following divides the quote-author matrix into a new quote matrix with quotes and another one with authors (keeping same keys)
if (preg_match("/ - /", $strg)) {
$dividedtext = explode(" - ", $strg);
$d_text = $dividedtext[0];
$d_author = " - ".$dividedtext[1];
} else {
$d_text = $strg;
$d_author = "";
}
$maxcharsperline = floor(($width-$left_margin) / $fontwidth);
$strg2 = wordwrap($d_text, $maxcharsperline, "\n", 1);
$lines = explode("\n", $strg2);
// the following sets variables containing information about the linejumps that have been made for a later use in image-height calculation
foreach ($lines as $numline => $line) {
if (preg_match("/ - /", $strg) and $numline == count($lines)-1) {
$lastline = $lines[(count($lines)-1)];
if (strlen($lastline) == $maxcharsperline ) {
$smalljumps++;
} elseif ((strlen($lastline) + strlen($d_author)) > $maxcharsperline) {
$smalljumps++;
}
}
if ($lines[$numline+1]) {
$smalljumps++;
}
}
$linejumps[$key] = $smalljumps;
$smalljumps = 0;
}
unset($lines, $lastline, $dividedtext, $d_text, $d_authot, $font_width, $maxcharsperline); // lets free some memory
// the following continues to set variables containing information about the linejump
if (is_array($linejumps)) {
$linejump_sum = array_sum($linejumps);
} else {
$linejump_sum = 0;
}
$y_size = ($totalstrings) * $fontheight + ($totalstrings-1) * $spacing + $fontheight*$linejump_sum; // here we set the image-height
// now we declare the image variable and the colors of all elements (including background transparency)
$avatar_im = @ImageCreate ($x_size, $y_size);
$background_color = ImageColorAllocate ($avatar_im, 255, 255, 255); // color of the background...
$trans = imagecolortransparent($avatar_im,$background_color); //...which will be transparent (only when the imagetype is gif)
$color_str= ImageColorAllocate ($avatar_im, $RGB_quote[0], $RGB_quote[1], $RGB_quote[2]);
$color_aut = ImageColorAllocate ($avatar_im, $RGB_author[0], $RGB_author[1], $RGB_author[2]);
// now we create the image content and do al the linejumping adjustments by means of the function ImageStringWrap (line 6 and on)
$k = 0;
foreach ($rnd_str as $key2 => $string) {
ImageStringWrap($avatar_im, $fontsize, $left_margin, $k*($fontheight+$spacing) + $jumping*$fontheight,$string,$color_str,$color_aut,$width-$left_margin);
$jumping = $jumping + $linejumps[$key2];
$k++;
}
// set headers so that no browser would have the temptation to store the image in the cache
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Content-type: image/gif"); // set content-type to gif image
ImageGIF ($avatar_im); // release image content as gif image
?>
Publicado bajo Licencia Creative Commons GNU General Public


Comentarios
Enviar un comentario nuevo