0

我正在尝试使用 Imagick annotateImage 在图像中呈现文本。我的文本可以是常规、粗体、斜体、下划线类型,我在下面的示例代码中为其定义了标志。根据 PHP 手册,我尝试使用这些函数来设置文本属性。但是粗体和斜体似乎不起作用。只有将我的字体设置为:

$txtfont ='ROBOTO-BOLD'; 

或者我只有在使用时才会得到斜体文本:

$txtfont ='ROBOTO-ITALIC';

$draw->setFontWeight(800); 和 $draw->setFontStyle(\Imagick::STYLE_ITALIC); 在以下示例代码中似乎没有按预期工作。

下划线也是一条细线,有没有办法设置它的粗细?

我犯了什么错误。在这方面的任何帮助将不胜感激。在下面添加我的示例代码:


/* Create some objects */      
$image = new Imagick();      
$image->setResolution(300, 300);      
$draw = new ImagickDraw();      
$pixel = new ImagickPixel( 'gray' );      
$txtbold =0;      
$txtitalic = 1;      
$txtunderline = 1;      

/* New image */      
$image->newImage(1000, 100, $pixel);      

/* Black text */      
$draw->setFillColor('black');      

/* Font properties */      
$txtfont ='ROBOTO';      

try{      
    $draw->setFont($txtfont);      
    echo "font loaded";       
    
}      
catch(\Exception $e)      
{      
    echo "error loading font".$e;      
}      

$draw->setFontSize( 12 );      
$draw->setResolution(300, 300);      


$draw->setTextAlignment (Imagick::ALIGN_CENTER);

if($txtbold) // if text is bold      
{      
    $draw->setFontWeight(800);      
    echo "text is bold";      
}      

if($txtitalic) // if text is italic      
{      
    $draw->setFontStyle(\Imagick::STYLE_ITALIC);      
    echo "text is italic";      
}      

if($txtunderline) // if text has underline      
{      
    $draw->setTextDecoration(2);      
    echo "text has underline";      
}      
                    
$text = 'The quick brown fox jumps over the lazy ';      
$metrics = $image->queryFontMetrics ($draw, $text);      

$xpos = ($image->width - $metrics['textWidth'])/2;      
$ypos = ($image->height - $metrics['textHeight'])/2 + $metrics['ascender'];      

/* Create text */      
$image->annotateImage($draw, $xpos, $ypos, 0, $text);      

/* Give image a format */      
$image->setImageFormat('jpeg');      

// SAVE FINAL page image      
    file_put_contents ("./testplaintext.jpg", $image);      
4

0 回答 0