set_hex($bg_color);
//Find the foreground color which is always after the 3rd slash in the url.
$fg_color = explode('/',$x);
$fg_color = explode('.',$fg_color[2]);
$fg_color = $fg_color[0];
if (!$fg_color) {
$fg_color = 000; //defaults to black if no foreground color is set.
}
$foreground = new color();
$foreground->set_hex($fg_color);
//Determine the file format. This can be anywhere in the URL.
$file_format = 'gif';
preg_match_all('/(png|jpg|jpeg)/', $x, $result);
if ($result[0][0]) {
$file_format = $result[0][0];
}
//Find the image dimensions
$dimensions = explode('/',$x);
$dimensions = explode('x',$dimensions[0]); //dimensions are always the first paramter in the URL.
$width = preg_replace('/[^\d]/i', '',$dimensions[0]);
$height = $width;
if ($dimensions[1]) {
$height = preg_replace('/[^\d]/i', '',$dimensions[1]);
}
$area = $width * $height;
if ($area >= 16000000) { //Limit the size of the image to no more than an area of 16,000,000.
die("Too big of an image!"); //If it is too big we kill the script.
}
$text_angle = 0; //I don't use this but if you wanted to angle your text you would change it here.
$font = "mplus-1c-medium.ttf"; // If you want to use a different font simply upload the true type font (.ttf) file to the same directory as this PHP file and set the $font variable to the font file name. I'm using the M+ font which is free for distribution -> http://www.fontsquirrel.com/fonts/M-1c
$img = imageCreate($width,$height); //Create an image.
$bg_color = imageColorAllocate($img, $background->get_rgb('r'), $background->get_rgb('g'), $background->get_rgb('b')); //Set the color gray for the background color. Hex value = #CCCCCC
$fg_color = imageColorAllocate($img, $foreground->get_rgb('r'), $foreground->get_rgb('g'), $foreground->get_rgb('b')); //Set the black color for the text
if ($_GET['text']) {
<<<<<<< .mine
$text = $_GET['text']; //
=======
$text = $_GET['text'];
$text = preg_replace('/\|/i', "\n", $text);
>>>>>>> .r101
}
else {
<<<<<<< .mine
$text = $width." × ".$height; //This is the default text string that will go right in the middle of the rectangle. × is the multiplication sign.
=======
$text = $width." × ".$height; //This is the default text string that will go right in the middle of the rectangle. × is the multiplication sign.
>>>>>>> .r101
}
<<<<<<< .mine
//Ric Ewing: I modified this to behave better with long or narrow images and condensed the resize code to a single line.
$fontsize = max(min($width/8, $height/2),5); //scale the text size based on the smaller of width/8 or hieght/2 with a minimum size of 5.
=======
//Ric Ewing: I modified this to behave better with long or narrow images and condensed the resize code to a single line.
//$fontsize = max(min($width/strlen($text), $height/strlen($text)),5); //scale the text size based on the smaller of width/8 or hieght/2 with a minimum size of 5.
>>>>>>> .r101
<<<<<<< .mine
echo 'Test: ' . strlen($text);
=======
$fontsize = max(min($width/strlen($text)*1.15, $height*0.5) ,5);
>>>>>>> .r101
$textBox = imagettfbbox_t($fontsize, $text_angle, $font, $text); //Pass these variable to a function that calculates the position of the bounding box.
<<<<<<< .mine
$textWidth = $textBox[4] - $textBox[1]; //Calculates the width of the text box by subtracting the Upper Right "X" position with the Lower Left "X" position.
$textHeight = abs($textBox[7])+abs($textBox[1]); //Calculates the height of the text box by adding the absolute value of the Upper Left "Y" position with the Lower Left "Y" position.
$textX = ($width - $textWidth)/2; //Determines where to set the X position of the text box so it is centered.
$textY = ($height - $textHeight)/2 + $textHeight; //Determines where to set the Y position of the text box so it is centered.
=======
$textWidth = ceil( ($textBox[4] - $textBox[1]) * 1.07 ); //Calculates the width of the text box by subtracting the Upper Right "X" position with the Lower Left "X" position.
>>>>>>> .r101
<<<<<<< .mine
imageFilledRectangle($img, 0, 0, $width, $height, $bg_color); //Creates the rectangle with the specified background color. http://us2.php.net/manual/en/function.imagefilledrectangle.php
=======
$textHeight = ceil( (abs($textBox[7])+abs($textBox[1])) * 1 ); //Calculates the height of the text box by adding the absolute value of the Upper Left "Y" position with the Lower Left "Y" position.
>>>>>>> .r101
<<<<<<< .mine
imagettftext($img, $fontsize, $text_angle, $textX, $textY, $fg_color, $font, $text); //Create and positions the text http://us2.php.net/manual/en/function.imagettftext.php
/*header('Content-type: image/'.$file_format); //Set the header so the browser can interpret it as an image and not a bunch of weird text.
//Create the final image based on the provided file format.
=======
$textX = ceil( ($width - $textWidth)/2 ); //Determines where to set the X position of the text box so it is centered.
$textY = ceil( ($height - $textHeight)/2 + $textHeight ); //Determines where to set the Y position of the text box so it is centered.
/* Debugging
echo 'Fontsize: ' . $fontsize . '
';
echo 'Text width: ' . $textWidth . ' (' . $textBox[4] . ' - ' . $textBox[1] . ')
';
echo 'Text height: ' . $textHeight . ' (' . abs($textBox[7]) . ' + ' . abs($textBox[1]) . ')
';
echo 'Text X: ' . $textX . '
';
echo 'Text Y: ' . $textY . '
';
*/
imageFilledRectangle($img, 0, 0, $width, $height, $bg_color); //Creates the rectangle with the specified background color. http://us2.php.net/manual/en/function.imagefilledrectangle.php
imagettftext($img, $fontsize, $text_angle, $textX, $textY, $fg_color, $font, $text); //Create and positions the text http://us2.php.net/manual/en/function.imagettftext.php
header('Content-type: image/'.$file_format); //Set the header so the browser can interpret it as an image and not a bunch of weird text.
>>>>>>> .r101
//Create the final image based on the provided file format.
switch ($file_format) {
case 'gif':
imagegif($img);
break;
case 'png':
imagepng($img);
break;
case 'jpg':
imagejpeg($img);
break;
case 'jpeg':
imagejpeg($img);
break;
}
*/
imageDestroy($img);//Destroy the image to free memory.
//Ruquay K Calloway http://ruquay.com/sandbox/imagettf/ made a better function to find the coordinates of the text bounding box so I used it.
function imagettfbbox_t($size, $text_angle, $fontfile, $text){
// compute size with a zero angle
$coords = imagettfbbox($size, 0, $fontfile, $text);
// convert angle to radians
$a = deg2rad($text_angle);
// compute some usefull values
$ca = cos($a);
$sa = sin($a);
$ret = array();
// perform transformations
for($i = 0; $i < 7; $i += 2){
$ret[$i] = round($coords[$i] * $ca + $coords[$i+1] * $sa);
$ret[$i+1] = round($coords[$i+1] * $ca - $coords[$i] * $sa);
}
return $ret;
}
?>