【发布时间】:2016-05-02 22:32:52
【问题描述】:
我制作了一个工具,用户可以在其中输入文本,脚本会生成该文本的图像,但是遇到文本阴影问题,我想使用文本阴影制作图像,但 GB 库 shadowImage 函数只放置图片阴影不适用于文字,
这是我现在得到的图像
但我想要那样
有没有人知道任何方法可以让我制作这样的图像?
这是我用来创建它的方法,
public function render()
{
$this->_image = new Imagick;
if(!$this->text)
{
$this->_image->newImage(1, 1, 'white');
return;
}
$draw = new ImagickDraw();
$color = new ImagickPixel($this->color);
$background = new ImagickPixel($this->surfaceColor);
/* Font properties */
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$draw->setFontSize($this->fontSize);
$draw->setFillColor($color);
$draw->setFont(Model_Tool_Font::find($this->font_id)->getFontPath($this->bold, $this->italic));
/* Border ? */
if ($this->borderWidth)
{
$draw->setStrokeColor($this->borderColor);
$draw->setStrokeWidth($this->borderWidth * $this->fontSize * self::BORDER_WIDTH_MULTIPLIER );
}
/* Get font metrics */
$metrics = $this->_image->queryFontMetrics($draw, $this->text);
/* Sizing calculations */
$width = $metrics['textWidth'];
$height = $metrics['textHeight'];
//respect custom proportions
if(!$this->maintainProportions && $this->customHeight > 0 && $this->customWidth > 0)
{
//stretching is better than shrinking (possibly for quality)
//size limits should be used to constrain size
$aspect = $this->customWidth / $this->customHeight;
if($this->customHeight / $height > $this->customWidth / $width)
$height = $width / $aspect;
else
$width = $height * $aspect;
}
$this->_limitSize($width, $height);
/* Sizing calculations end */
//appears to result in bigger image than before sizing?
if($height > $this->fontSize)
{
//update the metrics
$draw->setFontSize($this->fontSize = $height);
$metrics = $this->_image->queryFontMetrics($draw, $this->text);
}
/* Create text */
$draw->annotation(0, $metrics['ascender'], $this->text);
/* Create image */
$this->_image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$this->_image->drawImage($draw);
if ($this->reverseCut == 1) {
$this->_image->flopImage();
}
/* Shadow */
if ($this->shadowOffset)
{
$this->shadowOffset = abs((int)$this->shadowOffset);
$x = $y = 0;
switch($this->shadowOrient)
{
case self::ORIENT_TOP:
$x = 0;
$y = -3 - ($this->shadowOffset);
break;
case self::ORIENT_TOPRIGHT:
$x = 2 + ($this->shadowOffset);
$y = -3 - ($this->shadowOffset);
break;
case self::ORIENT_RIGHT:
$x = 2 + ($this->shadowOffset);
$y = 0;
break;
case self::ORIENT_BOTTOMRIGHT:
$x = 2 + ($this->shadowOffset);
$y = 2 + ($this->shadowOffset);
break;
case self::ORIENT_BOTTOM:
$x = 0;
$y = 2 + ($this->shadowOffset);
break;
case self::ORIENT_BOTTOMLEFT:
$x = -2 - ($this->shadowOffset);
$y = 2 + ($this->shadowOffset);
break;
case self::ORIENT_LEFT:
$x = -3 - ($this->shadowOffset);
$y = 0;
break;
case self::ORIENT_TOPLEFT:
$x = -3 - ($this->shadowOffset);
$y = -3 - ($this->shadowOffset);
break;
}
//transform logical sizes into pixels
$r = $this->fontSize * self::BORDER_WIDTH_MULTIPLIER;
$x *= $r;
$y *= $r;
$shadow = clone $this->_image;
$shadow->setImageBackgroundColor( new ImagickPixel( $this->shadowColor ) );
$shadow->shadowImage( 96, 0.5, 0, 0);
if($x || $y) {
$geo = $this->_image->getImageGeometry();
$currentImage = $this->_image->getImage();
$this->_image->newImage($geo['width'] + abs($x), $geo['height'] + abs($y), 'none');
$shift_x = $shift_y = 0;
if($x < 0) {
$shift_x = -$x;
$x = 0;
}
if($y < 0) {
$shift_y = -$y;
$y = 0;
}
$this->_image->compositeImage( $currentImage, Imagick::COMPOSITE_OVER , $shift_x, $shift_y);
}
$this->_image->compositeImage( $shadow, Imagick::COMPOSITE_DSTOVER , $x, $y);
}
$this->_image->resizeImage($width, $height, imagick::FILTER_LANCZOS, 0.9, $this->maintainProportions);
}
【问题讨论】:
-
在打印带有黄色边框的文本之前打印相同的文本,但没有黄色边框并稍微向右和向下移动。
-
非常感谢您的评论,您能否为我分享任何代码示例,以便我明白您的意思,如果您想展示我的代码,那么我想在这里分享我的功能。跨度>
-
是的,使用您已有的代码会更容易。
-
我已经编辑了问题并添加了我对图像进行分级的方法。
-
@KubaWyrostek 有什么可以帮助我的吗?
标签: php image gd imagick drawimage