Skip to content

Instantly share code, notes, and snippets.

@enijar
Created September 21, 2014 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enijar/35063ccdf01d3a9f4f0e to your computer and use it in GitHub Desktop.
Save enijar/35063ccdf01d3a9f4f0e to your computer and use it in GitHub Desktop.
Wrapper class for Imagick extension for PHP.
<?php
class ImagickServiceProvider {
// Image objects
protected $image;
protected $header;
protected $imagick;
protected $imagickDraw;
protected $imagickPixel;
// Font settings
protected $text;
protected $textOffset;
protected $fontFamily;
protected $textSize = 20;
protected $textColor = '#fff';
protected $textShadow = 'transparent';
protected $textContainer = [
'width' => null,
'height' => 20
];
protected $textAlign = [
'left' => 1,
'center' => 2,
'right' => 3
];
protected $textPositions = [
'top_left' => Imagick::GRAVITY_NORTHWEST,
'top' => Imagick::GRAVITY_NORTH,
'top_right' => Imagick::GRAVITY_NORTHEAST,
'left' => Imagick::GRAVITY_WEST,
'center' => Imagick::GRAVITY_CENTER,
'right' => Imagick::GRAVITY_EAST,
'bottom_left' => Imagick::GRAVITY_SOUTHWEST,
'bottom' => Imagick::GRAVITY_SOUTH,
'bottom_right' => Imagick::GRAVITY_SOUTHEAST
];
public function __construct($image, $header = null)
{
$this->image = $image;
$this->header = $header;
$this->imagick = new Imagick($image);
$this->imagickDraw = new ImagickDraw;
$this->imagickPixel = new ImagickPixel;
$this->textContainer['width'] = $this->imagick->getimagewidth() / 2;
$this->textOffset = [
'x' => 0,
'y' => $this->textSize / 2
];
}
public function text($text)
{
$this->text = $text;
return $this;
}
public function textColor($color)
{
$this->textColor = $color;
$this->imagickDraw->setFillColor($this->textColor);
return $this;
}
public function textSize($size)
{
$this->textSize = $size;
$this->imagickDraw->setFontSize($this->textSize);
return $this;
}
public function textPosition($position)
{
if(!array_key_exists($position, $this->textPositions)) {
$this->imagickDraw->setGravity($this->textPositions['center']);
} else {
$this->imagickDraw->setGravity($this->textPositions[$position]);
}
return $this;
}
public function textContainer($width)
{
if(is_string($width)) {
$width = (int) $width;
$width = $this->percent($width);
}
$this->textContainer['width'] = $width;
return $this;
}
public function textOffset($x = 0, $y = 0)
{
$this->textOffset = [
'x' => $x,
'y' => $y
];
return $this;
}
public function textShadow($color, $size)
{
$this->textShadow = $color;
$this->imagickDraw->setStrokeColor(new ImagickPixel($color));
$this->imagickDraw->setStrokeWidth($size);
return $this;
}
public function fontFamily($path)
{
$this->fontFamily = $path;
$this->imagickDraw->setFont($this->fontFamily);
return $this;
}
public function textAlign($position)
{
$this->imagickDraw->setTextAlignment($this->textAlign[$position]);
return $this;
}
protected function percent($value)
{
return ($value / 100) * $this->imagick->getimagewidth();
}
protected function containText()
{
$words = explode(' ', $this->text);
$lines = [];
$i = 0;
$lineHeight = 0;
while($i < count($words)) {
$currentLine = $words[$i];
if($i+1 >= count($words)) {
$lines[] = $currentLine;
break;
}
$metrics = $this->imagick->queryFontMetrics($this->imagickDraw, $currentLine . ' ' . $words[$i+1]);
while($metrics['textWidth'] <= $this->textContainer['width']) {
$currentLine .= ' ' . $words[++$i];
if($i+1 >= count($words)) break;
$metrics = $this->imagick->queryFontMetrics($this->imagickDraw, $currentLine . ' ' . $words[$i+1]);
}
$lines[] = $currentLine;
$i++;
if($metrics['textHeight'] > $lineHeight) {
$lineHeight = $metrics['textHeight'];
}
}
for($i = 0; $i < count($lines); $i++) {
$verticalAlign = -((count($lines) * $this->textSize / 2)) + $this->textOffset['y'] + $i*$lineHeight;
$this->imagick->annotateImage($this->imagickDraw, $this->textOffset['x'], $verticalAlign, 0, $lines[$i]);
}
return $this;
}
protected function getImageType()
{
if(is_null($this->header)) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $this->image);
finfo_close($finfo);
return $mime;
}
return $this->header;
}
public function output()
{
header('Content-type: ' . $this->getImageType());
$this->containText($this->text, $this->textContainer['width'], $this->textContainer['height'], $this->textOffset['x'], $this->textOffset['y']);
echo $this->imagick;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment