【问题标题】:imagick / Imagemagick - Exported image looks blurry/softimagick / Imagemagick - 导出的图像看起来模糊/柔和
【发布时间】:2015-08-21 20:54:02
【问题描述】:

我正在使用 imagick 的 PHP 版本从数据库/用户选择中生成图像。

我尝试了多种分辨率/压缩选项,但每个选项都变得柔和或非常模糊。

例如setImageResolutionsetImageCompressionQuality

/* VARIABLES 
================================================== */
$title      = $_POST['input_title'];
$date       = $_POST['input_text1'];
$format     = $_POST['input_format'];

$bgimg      = $_POST['bgImage'];
$theme      = $_POST['theme'];


$width      = '564px';
$height     = '296px';



// 1. GET BACKGROUND IMAGE
$image      = new Imagick('images/highres/bg/' . $bgimg . '.jpg');
$theme      = new Imagick('images/highres/themes/' . $theme . '.png');

// Lets create a canvas to set the width and height
$canvas     = new Imagick();
$canvas->newImage($width, $height, 'white', 'jpg' );        

// ARTBOX
$draw       = new ImagickDraw();
$draw2      = new ImagickDraw();

// SET DPI
$canvas->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$canvas->setImageResolution(72,72);


// Add the images to the canvas
// Re-order these for images to appear on top of eachother
$canvas->compositeImage($image, Imagick::COMPOSITE_DEFAULT, 0, 0);
$canvas->compositeImage($theme, Imagick::COMPOSITE_DEFAULT, 0, 0);

// Set font properties for each text area
$draw->setFont( config::get('url/clienttemplateurlabsolute') . '/styles/fonts/fredokaone-regular-webfont.ttf');


// CREATE WORD WIDTH FUNCTION
function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
{
    $words = explode(" ", $text);
    $lines = array();
    $i = 0;
    $lineHeight = 0;
    while($i < count($words) )
    {
        $currentLine = $words[$i];
        if($i+1 >= count($words))
        {
            $lines[] = $currentLine;
            break;
        }
        //Check to see if we can add another word to this line
        $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
        while($metrics['textWidth'] <= $maxWidth)
        {
            //If so, do it and keep doing it!
            $currentLine .= ' ' . $words[++$i];
            if($i+1 >= count($words))
                break;
            $metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
        }
        //We can't add the next word to this line, so loop to the next line
        $lines[] = $currentLine;
        $i++;
        //Finally, update line height
        if($metrics['textHeight'] > $lineHeight)
            $lineHeight = $metrics['textHeight'];
    }
    return array($lines, $lineHeight);
}


function drawText($artbox, $textalign = \Imagick::ALIGN_RIGHT, $fontsize, $color, $xpos, $ypos, $rotation = 0, $txt, $font, $maxWidth) {

    // NOTE - Check for <br/> tags and swap for line breaks
    $breaks = array("<br />","<br>","<br/>");  
    $txt = str_ireplace($breaks, "\r\n", $txt);

    global $canvas;
    $artbox->setFont( config::get('url/clienttemplateurlabsolute') . '/styles/fonts/' . $font );
    $artbox->setTextAlignment($textalign);                              // TEXT ALIGN
    $artbox->setFontSize($fontsize);                                    // FONT SIZE
    $artbox->setFillColor($color);                                      // FONT COLOUR                                  

    // SET WIDTH
    list($lines, $lineHeight) = wordWrapAnnotation($canvas, $artbox, $txt, $maxWidth);
    for($i = 0; $i < count($lines); $i++)
        $canvas->annotateImage($artbox, $xpos, $ypos + $i*$lineHeight, $rotation, $lines[$i]);

}

// ARTBOX 1 - PAGE TITLE

drawText(
    $draw,                          // ARTBOX
    \Imagick::ALIGN_RIGHT,          // TEXT ALIGN
    '28',                           // FONT SIZE
    'white',                        // COLOR
    540,                            // X
    200,                            // Y
    -9,                             // ROTATION
    $title,                         // TXT
    'fredokaone-regular-webfont.ttf',// FONT
    340                             // MAX WIDTH IN PIXELS
);

// ARTBOX 2 - DATE
drawText(
    $draw2,
    \Imagick::ALIGN_RIGHT,
    '16',
    'white',
    550,
    280,
    0,
    $date,
    'arial.ttf',
    250                             // max width in pixels
);          


// Set output image format (JPG / PNG) from the user selection
$canvas->setImageFormat($format);

if ( $format == 'jpg' ) {
    $canvas->setImageCompressionQuality(100);
}

【问题讨论】:

  • 文字是柔和/模糊还是图像?如果图像模糊,您可以删除所有文本操作并发布一个没有文本的更简单示例。还请发布您的输入和输出图像的示例。

标签: php imagemagick imagick


【解决方案1】:

发生的情况是,当下载图像并在 Mac OSX“预览”中查看它时,文本和边缘被像素化(轻微)。在 Chrome 中查看相同的图像,图像很好!!

完全相同的图像文件,但 2 个不同的结果 - 奇怪!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多