Skip to content

Commit

Permalink
performance tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
oo12 committed Apr 23, 2014
1 parent fe86433 commit 692edb2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function apply(ImageInterface $image) {

$doRotate = !$isGmagick && $p['angle'] % 360;
if ($doRotate && $p['bgcolor'] === null && $isRImage) { // if text will be rotated, use a transparent bg for better positioning
$p['bgcolor'] = '#ffffff';
$p['bgcolor'] = array(255, 255, 255);
$p['bgopacity'] = 0;
}
if ($p['bgcolor']) { // if we have a bg color, add a bg box
Expand All @@ -106,7 +106,7 @@ public function apply(ImageInterface $image) {
);
$wmbg->draw()->text($p['text'], $font, new Point($paddingX, $paddingY), 0); // add text
if ($doRotate) {
$wmbg->rotate($p['angle'], self::$rgb->color('#fff', 100));
$wmbg->rotate($p['angle'], self::$rgb->color(array(255, 255, 255), 100));
$wmbgSize = $wmbg->getSize();
$wmbgWidth = $wmbgSize->getWidth();
$wmbgHeight = $wmbgSize->getHeight();
Expand Down Expand Up @@ -198,16 +198,20 @@ public function apply(ImageInterface $image) {
}
}

// Opacity
if (!$isGmagick && $p['opacity'] < 100) {
$a = round((100 - $p['opacity']) * 2.55); // calculate alpha (0:opaque - 255:transparent)
$mask = $imagine->create($wmSize, self::$rgb->color(array($a, $a, $a)));
$wm->applyMask($isRImage ? $mask->getImage() : $mask);
if ($isRImage && !$isGmagick && $p['opacity'] < 100) { // for Imagick we can easily reduce transparency
try {
$wm->fade($p['opacity'] / 100);
}
catch (\Exception $e) { // maybe. fallback for ImageMagick < 6.3.1
$a = round((100 - $p['opacity']) * 2.55); // calculate alpha (0:opaque - 255:transparent)
$mask = $imagine->create($wmSize, self::$rgb->color(array($a, $a, $a)));
$wm->applyMask($mask->getImage());
}
}

// Rotation
if ($doRotate) {
$wm->rotate($p['angle'], self::$rgb->color('#fff', 100));
$wm->rotate($p['angle'], self::$rgb->color(array(255, 255, 255), 100));
$wmSize = $wm->getSize();
if ($wmSize->getWidth() > $imgWidth || $wmSize->getHeight() > $imgHeight) { // one more check. Shouldn't be necessary, but it sometimes is
$wm = $wm->thumbnail(new Box($imgWidth, $imgHeight));
Expand Down Expand Up @@ -250,7 +254,12 @@ public function apply(ImageInterface $image) {

}

$image->paste($isRImage ? $wm->getImage() : $wm, $wmStartPoint);
if ($isRImage) {
$image->paste($wm->getImage(), $wmStartPoint);
}
else { // GD
imagecopymerge($image->getGdResource(), $wm->getGdResource(), $wmStartPoint->getX(), $wmStartPoint->getY(), 0, 0, $wmWidth, $wmHeight, $p['opacity']);
}
}
catch(\Exception $e) {
$this->debugmessages[] = '*** Image Watermark Error: ' . $e->getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ public function rotate($angle, \Imagine\Image\Palette\Color\ColorInterface $back
}


protected function load($size = null) {
$handle = fopen($this->filename, 'r');
public function fade($opacity) {
$this->image->getImagick()->setImageOpacity($opacity);
}


protected function load($size = null) {
try {
$magick = new \Imagick();
if ($this->format === IMG_JPG && $size !== null) {
$magick->setOption('jpeg:size', $size[0] . 'x' . $size[1]); // some versions of Imagick only respond to this...
$magick->setSize($size[0], $size[1]); // ...and others to this
}
$magick->readImageFile($handle);
$magick->setImageFilename($this->filename);
fclose($handle);
$magick->readImage($this->filename);
}
catch (\Exception $e) {
fclose($handle);
throw new \Imagine\Exception\RuntimeException("Imagick: Unable to open image {$this->filename}. {$e->getMessage()}", $e->getCode(), $e);
}
if ($this->format === IMG_JPG && $size !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public function processImage($input, $output, $options = array()) {
$this->width = $this->height = null;
if (is_string($options)) { $options = parse_str($options); } // convert an options string to an array if needed
$inputParams = array('options' => $options);
$outputIsJpg = strncasecmp('jp', pathinfo($output, PATHINFO_EXTENSION), 2) === 0;
$outputType = pathinfo($output, PATHINFO_EXTENSION);
$outputIsJpg = strncasecmp('jp', $outputType, 2) === 0;

try {
/* initial dimensions */
Expand Down Expand Up @@ -406,7 +407,7 @@ public function processImage($input, $output, $options = array()) {
$bgColor = explode('/', $options['bg']);
$bgColor[1] = isset($bgColor[1]) ? $bgColor[1] : 100;
}
else { $bgColor = array('ffffff', 100); }
else { $bgColor = array(array(255, 255, 255), 100); }

$backgroundColor = self::$palette->color($bgColor[0], 100 - $bgColor[1]);
if (isset($cropBox)) { $bgBox = $cropBox; }
Expand Down Expand Up @@ -468,8 +469,10 @@ public function processImage($input, $output, $options = array()) {
}

/* save */
$outputOpts = array('quality' => empty($options['q']) ? $this->defaultQuality : (int) $options['q']); // change 'q' to 'quality', or use default
if (!empty($options['f'])) { $outputOpts['format'] = $options['f']; }
$outputOpts = array(
'jpeg_quality' => empty($options['q']) ? $this->defaultQuality : (int) $options['q'], // change 'q' to 'quality', or use default
'format' => empty($options['f']) ? $outputType : $options['f']
);
$image->save($output, $outputOpts);
if (!$this->width) { $this->width = $width; }
if (!$this->height) { $this->height = $height; }
Expand Down

0 comments on commit 692edb2

Please sign in to comment.