function imagettftextbox($size_pts, $angle, $left, $top, $color, $font, $raw_text, $max_width, $align = 'left', $lineheight = 1.0)
{
    global $FLIR;
    if ($lineheight < -1) {
        // will cause text to disappear off canvas
        $lineheight = 1.0;
    }
    $raw_textlines = explode("\n", $raw_text);
    $formatted_lines = $formatted_widths = array();
    $max_values = bounding_box(HBOUNDS_TEXT);
    $previous_bounds = array('width' => 0);
    $longest_line = 0;
    $spaces = ' ' . str_repeat(' ', defined('SPACING_GAP') ? SPACING_GAP : 0);
    foreach ($raw_textlines as $text) {
        $bounds = bounding_box($text);
        if ($bounds['height'] > $max_lineheight) {
            $max_lineheight = $bounds['height'];
        }
        if ($bounds['belowBasepoint'] > $max_baseheight) {
            $max_baseheight = $bounds['belowBasepoint'];
        }
        if ($bounds['xOffset'] > $max_leftoffset) {
            $max_leftoffset = $bounds['xOffset'];
        }
        if ($bounds['yOffset'] > $max_rightoffset) {
            $max_rightoffset = $bounds['yOffset'];
        }
        if ($bounds['width'] < $max_width) {
            // text doesn't require wrapping
            $formatted_lines[] = $text;
            $formatted_widths[$text] = $longest_line = $bounds['width'];
        } else {
            // text requires wrapping
            $words = explode($spaces, trim($text));
            $wordcnt = count($words);
            $test_line = '';
            for ($i = 0; $i < count($words); $i++) {
                // test words one-by-one to see if they put the width over
                $prepend = $i == 0 ? '' : $test_line . $spaces;
                // add space if not the first word
                $working_line = $prepend . $words[$i];
                $bounds = bounding_box($working_line);
                if ($bounds['width'] > $max_width) {
                    // if working line is too big previous line isn't, use that
                    if ($wordcnt == 1) {
                        // cannot wrap a single word that is longer than bounding box
                        return false;
                    }
                    $formatted_lines[] = $test_line;
                    $formatted_widths[$test_line] = $previous_bounds['width'];
                    $test_line = $words[$i];
                    $bounds = bounding_box($test_line);
                } else {
                    // keep adding
                    $test_line = $working_line;
                }
                if ($bounds['width'] > $longest_line) {
                    $longest_line = $bounds['width'];
                }
                $previous_bounds = $bounds;
            }
            if ($test_line != '') {
                // if words are finished and there is something left in the buffer add it
                $bounds = bounding_box($test_line);
                $formatted_lines[] = $test_line;
                $formatted_widths[$test_line] = $bounds['width'];
            }
        }
    }
    $longest_line += abs($max_leftoffset);
    $max_lineheight = $max_values['height'] * $lineheight;
    if ($lineheight < 0) {
        $max_lineheight += $max_values['height'];
    }
    $image = imagecreatetruecolor($FStyle['notrim'] == 'true' ? $max_width : $longest_line, $max_lineheight * (count($formatted_lines) - 1) + $max_values['yOffset'] + $max_values['belowBasepoint']);
    gd_alpha($image);
    imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), gd_bkg($image));
    for ($i = 0; $i < count($formatted_lines); $i++) {
        if ($i == 0) {
            $offset_top = $max_values['yOffset'];
        } else {
            $offset_top = $max_lineheight * $i + $max_values['yOffset'];
        }
        switch (strtolower($align)) {
            default:
            case 'left':
                $offset_left = 0;
                break;
            case 'center':
                $offset_left = ($max_width - $formatted_widths[$formatted_lines[$i]]) / 2;
                break;
            case 'right':
                $offset_left = $max_width - $formatted_widths[$formatted_lines[$i]] - 1;
                break;
        }
        imagettftext($image, $size_pts, $angle, $offset_left, $offset_top, gd_color($image), $font, $formatted_lines[$i]);
        $bounds = array('xOffset' => $offset_left, 'yOffset' => $offset_top);
        //        render_text($bounds, $formatted_lines[$i], $image, $bounds);
    }
    return $image;
}
示例#2
0
require "_config.php";
$line_colors = preg_split("/,\\s*?/", CODE_LINE_COLORS);
$char_colors = preg_split("/,\\s*?/", CODE_CHAR_COLORS);
$fonts = collect_files(PATH_TTF, "ttf");
$img = imagecreatetruecolor(CODE_WIDTH, CODE_HEIGHT);
imagefilledrectangle($img, 0, 0, CODE_WIDTH - 1, CODE_HEIGHT - 1, gd_color(CODE_BG_COLOR));
//$fonts = imageloadfont($fonts);
// Draw lines
for ($i = 0; $i < CODE_LINES_COUNT; $i++) {
    imageline($img, rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), gd_color($line_colors[rand(0, count($line_colors) - 1)]));
}
// Draw code
$code = "";
$y = CODE_HEIGHT / 2 + CODE_FONT_SIZE / 2;
for ($i = 0; $i < CODE_CHARS_COUNT; $i++) {
    $color = gd_color($char_colors[rand(0, count($char_colors) - 1)]);
    $angle = rand(-30, 30);
    $char = substr(CODE_ALLOWED_CHARS, rand(0, strlen(CODE_ALLOWED_CHARS) - 1), 1);
    $font = PATH_TTF . "/" . $fonts[rand(0, count($fonts) - 1)];
    $x = intval(CODE_WIDTH / CODE_CHARS_COUNT * $i) + CODE_FONT_SIZE / 2;
    $code .= $char;
    imagettftext($img, CODE_FONT_SIZE, $angle, $x, $y, $color, $font, $char);
}
$_SESSION['__img_code__'] = md5($code);
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
function gd_color($html_color)
{
    return preg_match('/^#?([\\dA-F]{6})$/i', $html_color, $rgb) ? hexdec($rgb[1]) : false;
}
示例#3
0
文件: generate.php 项目: palmic/lbox
             $REAL_HEIGHT_BOUNDS = $bounds;
         }
         $offset_left = 0;
         $nsize = $FLIR['size_pts'];
         while (($REAL_HEIGHT_BOUNDS['height'] > $FLIR['maxheight'] || $bounds['width'] > $FLIR['maxwidth']) && $nsize > 2) {
             $nsize -= 0.5;
             $bounds = bounding_box($FLIR['text'], NULL, $nsize);
             $REAL_HEIGHT_BOUNDS = $FStyle['realFontHeight'] == 'true' ? bounding_box(HBOUNDS_TEXT, NULL, $nsize) : $bounds;
         }
         $FLIR['size_pts'] = $nsize;
         if (false === @($image = imagecreatetruecolor($bounds['width'], $REAL_HEIGHT_BOUNDS['height']))) {
             err('COULD_NOT_CREATE');
         }
         gd_alpha();
         imagefilledrectangle($image, $offset_left, 0, imagesx($image), imagesy($image), gd_bkg());
         imagettftext($image, $FLIR['size_pts'], 0, $bounds['xOffset'], $REAL_HEIGHT_BOUNDS['yOffset'], gd_color(), $FLIR['font'], $FLIR['text']);
         render_text($bounds);
         break;
     case 'spacer':
         if (false === @($image = imagecreatetruecolor($SPACE_BOUNDS['width'] * $SPACES_COUNT, 1))) {
             err('COULD_NOT_CREATE');
         }
         imagesavealpha($image, true);
         imagealphablending($image, false);
         imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), gd_bkg());
         break;
 }
 if ($FLIR['postscript']) {
     imagepsfreefont($FLIR['ps']['font']);
 }
 if (false !== $image) {
 function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
 {
     $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
     foreach ($defaults as $key => $val) {
         if (!is_array($data)) {
             if (!isset(${$key}) or ${$key} == '') {
                 ${$key} = $val;
             }
         } else {
             ${$key} = !isset($data[$key]) ? $val : $data[$key];
         }
     }
     if ($img_path == '' or $img_url == '') {
         return FALSE;
     }
     if (!@is_dir($img_path)) {
         return FALSE;
     }
     if (!is_writable($img_path)) {
         return FALSE;
     }
     if (!extension_loaded('gd')) {
         return FALSE;
     }
     // -----------------------------------
     // Remove old images
     // -----------------------------------
     list($usec, $sec) = explode(" ", microtime());
     $now = (double) $usec + (double) $sec;
     $current_dir = @opendir($img_path);
     while ($filename = @readdir($current_dir)) {
         if ($filename != "." and $filename != ".." and $filename != "index.html") {
             $name = str_replace(".jpg", "", $filename);
             if ($name + $expiration < $now) {
                 @unlink($img_path . $filename);
             }
         }
     }
     @closedir($current_dir);
     $line_colors = preg_split("/,\\s*?/", CODE_LINE_COLORS);
     $char_colors = preg_split("/,\\s*?/", CODE_CHAR_COLORS);
     $fonts = collect_files(PATH_TTF, "ttf");
     $img = imagecreatetruecolor(CODE_WIDTH, CODE_HEIGHT);
     imagefilledrectangle($img, 0, 0, CODE_WIDTH - 1, CODE_HEIGHT - 1, gd_color(CODE_BG_COLOR));
     // Draw lines
     for ($i = 0; $i < CODE_LINES_COUNT; $i++) {
         imageline($img, rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), gd_color($line_colors[rand(0, count($line_colors) - 1)]));
     }
     // Draw code
     $code = "";
     $y = CODE_HEIGHT / 2 + CODE_FONT_SIZE / 2;
     for ($i = 0; $i < CODE_CHARS_COUNT; $i++) {
         $color = gd_color($char_colors[rand(0, count($char_colors) - 1)]);
         $angle = rand(-30, 30);
         $char = substr(CODE_ALLOWED_CHARS, rand(0, strlen(CODE_ALLOWED_CHARS) - 1), 1);
         $font = PATH_TTF . "/" . $fonts[rand(0, count($fonts) - 1)];
         $x = intval(CODE_WIDTH / CODE_CHARS_COUNT * $i) + CODE_FONT_SIZE / 2;
         $code .= $char;
         imagettftext($img, CODE_FONT_SIZE, $angle, $x, $y, $color, $font, $char);
     }
     // -----------------------------------
     //  Generate the image
     // -----------------------------------
     $img_name = $now . '.jpg';
     ImageJPEG($img, $img_path . $img_name);
     $img_width = CODE_WIDTH;
     $img_height = CODE_HEIGHT;
     $img = "<img src=\"{$img_url}{$img_name}\" width=\"{$img_width}\" height=\"{$img_height}\" style=\"border:1px solid #021a40;\" alt=\" \" />";
     //ImageDestroy($img);
     return array('word' => $code, 'time' => $now, 'image' => $img);
 }
示例#5
0
文件: inc-flir.php 项目: palmic/lbox
function render_text($bounds, $text = NULL, $img = NULL, $realheight = NULL)
{
    global $FLIR;
    if (!is_null($realheight)) {
        $REAL_HEIGHT_BOUNDS = $realheight;
    } else {
        global $REAL_HEIGHT_BOUNDS;
    }
    if (is_null($img)) {
        global $image;
    } else {
        $image = $img;
    }
    if (is_null($text)) {
        $text = $FLIR['postscript'] ? $FLIR['original_text'] : $FLIR['text'];
    }
    if ($FLIR['postscript']) {
        imagepstext($image, $text, $FLIR['ps']['font'], $FLIR['size'], gd_color($image), imagecolorallocatealpha($image, $FLIR['bkgcolor']['red'], $FLIR['bkgcolor']['green'], $FLIR['bkgcolor']['blue'], 127), $bounds['xOffset'], $REAL_HEIGHT_BOUNDS['yOffset'], $FLIR['ps']['space'], $FLIR['ps']['kerning'], 0, $FLIR['size'] < 20 ? 16 : 4);
    } else {
        imagettftext($image, $FLIR['size_pts'], 0, $bounds['xOffset'], $REAL_HEIGHT_BOUNDS['yOffset'], gd_color($image), $FLIR['font'], $text);
    }
}