3

使用这个“有点”丑陋的类,我正在使用 .ttf 字体文件将文本渲染到图像中。使用 ImageFlip() 函数时,我得到一个黑色的全黑图像(尽管尺寸合适)。当我用 imagecreate() 替换所有出现的imagecreatetruecolor ( )时,一切都按预期工作。

渲染为JPEG而不是PNG没有区别。我没有收到任何 PHP 错误。

<?php

define ( 'IMAGE_FLIP_HORIZONTAL', 1 );
define ( 'IMAGE_FLIP_VERTICAL', 2 );
define ( 'IMAGE_FLIP_BOTH', 3 );

/**
 * Renders text to an image using a TrueType font
 *
 * @author peterrus
 */
class ImageTTFRenderer {

    //defaults
    private $size = 60;
    private $rot = 0; // rotation in degrees. 
    private $pad = 10; // padding. 
    private $transparent = 0; // gives a black image in combination with flipping
    private $red = 0; // white text... 
    private $grn = 0;
    private $blu = 0;
    private $bg_red = 255; // on black background. 
    private $bg_grn = 255;
    private $bg_blu = 255;
    private $flipped = true;
    private $fontFile;

    function __construct($options) {
        if (isset($options['fontfile'])) {
            $this->fontFile = $options['fontfile'];
        } else {
            throw new Exception("No font selected");
            die();
        }
        if (isset($options['size'])) {
            $this->size = $options['size'];
        }
        if (isset($options['red'])) {
            $this->size = $options['red'];
        }
        if (isset($options['grn'])) {
            $this->size = $options['grn'];
        }
        if (isset($options['blu'])) {
            $this->size = $options['blu'];
        }
        if (isset($options['bg_red'])) {
            $this->size = $options['bg_red'];
        }
        if (isset($options['bg_grn'])) {
            $this->size = $options['bg_grn'];
        }
        if (isset($options['bg_blu'])) {
            $this->size = $options['bg_blu'];
        }
    }

function ImageFlip($imgsrc, $type)
{
    $width = imagesx($imgsrc);
    $height = imagesy($imgsrc);

    $imgdest = imagecreatetruecolor($width, $height);

    switch( $type )
        {
        case IMAGE_FLIP_HORIZONTAL:
            for( $y=0 ; $y<$height ; $y++ )
                imagecopy($imgdest, $imgsrc, 0, $height-$y-1, 0, $y, $width, 1);
            break;

        case IMAGE_FLIP_VERTICAL:
            for( $x=0 ; $x<$width ; $x++ )
                imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);
            break;

        case IMAGE_FLIP_BOTH:
            for( $x=0 ; $x<$width ; $x++ )
                imagecopy($imgdest, $imgsrc, $width-$x-1, 0, $x, 0, 1, $height);

            $rowBuffer = imagecreatetruecolor($width, 1);
            for( $y=0 ; $y<($height/2) ; $y++ )
                {
                imagecopy($rowBuffer, $imgdest  , 0, 0, 0, $height-$y-1, $width, 1);
                imagecopy($imgdest  , $imgdest  , 0, $height-$y-1, 0, $y, $width, 1);
                imagecopy($imgdest  , $rowBuffer, 0, $y, 0, 0, $width, 1);
                }

            imagedestroy( $rowBuffer );
            break;
        }

    return( $imgdest );
}

public function draw($message) {

// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

$width = 0;
$height = 0;
$offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";

// determine font height. 
$bounds = ImageTTFBBox($this->size, $this->rot, $this->fontFile, "W");
if ($bounds == false) {
    throw new Exception("imagettfbbox() returned false");
    die();
}
if ($this->rot < 0) {
    $font_height = abs($bounds[7] - $bounds[1]);
} else if ($this->rot > 0) {
    $font_height = abs($bounds[1] - $bounds[7]);
} else {
    $font_height = abs($bounds[7] - $bounds[1]);
}

// determine bounding box. 
$bounds = ImageTTFBBox($this->size, $this->rot, $this->fontFile, $message);
if ($bounds == false) {
    throw new Exception("imagettfbbox() returned false");
    die();
}
if ($this->rot < 0) {
    $width = abs($bounds[4] - $bounds[0]);
    $height = abs($bounds[3] - $bounds[7]);
    $offset_y = $font_height;
    $offset_x = 0;
} else if ($this->rot > 0) {
    $width = abs($bounds[2] - $bounds[6]);
    $height = abs($bounds[1] - $bounds[5]);
    $offset_y = abs($bounds[7] - $bounds[5]) + $font_height;
    $offset_x = abs($bounds[0] - $bounds[6]);
} else {
    $width = abs($bounds[4] - $bounds[6]);
    $height = abs($bounds[7] - $bounds[1]);
    $offset_y = $font_height;
    $offset_x = 0;
}

$image = imagecreatetruecolor($width + ($this->pad * 2) + 1, $height + ($this->pad * 2) + 1);
if ($image == false) {
    throw new Exception("imagecreatetruecolor() returned false");
    die();
}

$background = imagecolorallocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
$foreground = imagecolorallocate($image, $this->red, $this->grn, $this->blu);
if ($background === false || $foreground === false) {
    throw new Exception("imagecolorallocate() returned false");
    die();
}

if ($this->transparent)
    imagecolortransparent($image, $background);
imageinterlace($image, false);

// render it. 
imageTTFText($image, $this->size, $this->rot, $offset_x + $this->pad, $offset_y + $this->pad, $foreground, $this->fontFile, $message);

if($this->flipped) { $image = $this->ImageFlip($image, IMAGE_FLIP_VERTICAL); }

// output PNG object. 
imagePNG($image);
}

}

?>
4

1 回答 1

2

如果你想要一个完全透明的背景,你可以在 imagecreatetruecolor 之后使用以下内容。

imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $trans_colour);

您要做的是创建一个真彩色图像,确保 alpha 保存状态处于打开状态,然后使用已将其 alpha 级别设置为完全透明 (127) 的颜色填充图像。

另外,请注意,第一次调用 imagecolorallocate 会用该颜色填充背景。只是觉得你也应该注意这一点。

于 2015-02-20T17:28:03.853 回答