2015-08-21 6 views
0

Я использую PHP-версию imagick для создания изображения из базы данных/выбора пользователя.imagick/Imagemagick - Экспортированное изображение выглядит размытым/мягким

Я пробовал различные варианты восстановления/сжатия, но каждый из них вышел мягким или очень слегка размытым.

например. setImageResolution и setImageCompressionQuality

/* 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); 
} 
+0

Получает ли текст мягкий/размытый или изображение? Если изображение размыто, вы можете удалить все текстовые манипуляции и опубликовать более простой пример без текста. Также отправьте пример ваших входных и выходных изображений, пожалуйста. –

ответ

0

Что происходит в том, что при загрузке изображения и его просмотра в Mac OSX «Предварительный просмотр» текст и края были пикселизированным (немного). Просмотр того же изображения в Chrome было прекрасным!

Точно такой же файл изображения, но 2 разных результата - странный!

Смежные вопросы