我正在使用PHP版本的imagick从数据库/用户选择中生成图像。
我尝试过各种各样的resoultion /压缩选项,但每种选择都会变得柔和或略微模糊。
e.g。 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);
}
uidValues
答案 0 :(得分:0)
发生的事情是,当下载图像并在Mac OSX“预览”中查看时,文本和边缘会像素化(略微)。在Chrome中查看相同的图像,图像很好!!
完全相同的图像文件,但有两个不同的结果 - 奇怪!