示例#1
0
if ($area >= 16000000 || $width > 9999 || $height > 9999) {
    //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.
}
//Let's round the dimensions to 3 decimal places for aesthetics
$width = round($width, 3);
$height = round($height, 3);
$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'));
$fg_color = imageColorAllocate($img, $foreground->get_rgb('r'), $foreground->get_rgb('g'), $foreground->get_rgb('b'));
if ($_GET['text']) {
    $_GET['text'] = preg_replace("#(0x[0-9A-F]{2})#e", "chr(hexdec('\\1'))", $_GET['text']);
    $lines = substr_count($_GET['text'], '|');
    $text = preg_replace('/\\|/i', "\n", $_GET['text']);
} else {
    $lines = 1;
    $text = $width . " × " . $height;
    //This is the default text string that will go right in the middle of the rectangle. × is the multiplication sign, it is not an 'x'.
}
//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.
$fontsize = max(min($width / strlen($text) * 1.15, $height * 0.5), 5);
$textBox = imagettfbbox_t($fontsize, $text_angle, $font, $text);
//Pass these variable to a function that calculates the position of the bounding box.
$textWidth = ceil(($textBox[4] - $textBox[1]) * 1.07);
示例#2
0
function addBorderpng($im, $border = 1, $color = 'eee')
{
    $width = imagesx($im);
    $height = imagesy($im);
    $img_adj_width = $width - 2 * $border;
    $img_adj_height = $height - 2 * $border;
    $newimage = imagecreatetruecolor($width, $height);
    $bg_color = new color();
    $bg_color->set_hex($color);
    $background = imageColorAllocate($newimage, $bg_color->get_rgb('r'), $bg_color->get_rgb('g'), $bg_color->get_rgb('b'));
    imagefilledrectangle($newimage, 0, 0, $width, $height, $background);
    imagecopyresampled($newimage, $im, $border, $border, 0, 0, $img_adj_width, $img_adj_height, $width, $height);
    imagedestroy($im);
    return $newimage;
}