Imbo\Image\Transformation\AutoRotate::transform PHP Method

transform() public method

Transform the image
public transform ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The event instance
    public function transform(EventInterface $event)
    {
        $image = $event->getArgument('image');
        try {
            // Get orientation from exif data
            $orientation = $this->imagick->getImageOrientation();
            /**
             * Transform image if orientation is set and greater than 1
             * (Imagick::ORIENTATION_TOPLEFT)
             */
            if ($orientation > 1) {
                $flipHorizontally = false;
                $flipVertically = false;
                $rotate = 0;
                switch ($orientation) {
                    case Imagick::ORIENTATION_TOPRIGHT:
                        //2
                        $flipHorizontally = true;
                        break;
                    case Imagick::ORIENTATION_BOTTOMRIGHT:
                        //3
                        $rotate = 180;
                        break;
                    case Imagick::ORIENTATION_BOTTOMLEFT:
                        //4
                        $flipVertically = true;
                        break;
                    case Imagick::ORIENTATION_LEFTTOP:
                        //5
                        $rotate = 90;
                        $flipHorizontally = true;
                        break;
                    case Imagick::ORIENTATION_RIGHTTOP:
                        //6
                        $rotate = 90;
                        break;
                    case Imagick::ORIENTATION_RIGHTBOTTOM:
                        //7
                        $rotate = 90;
                        $flipVertically = true;
                        break;
                    case Imagick::ORIENTATION_LEFTBOTTOM:
                        //8
                        $rotate = 270;
                        break;
                }
                if ($rotate) {
                    $this->imagick->rotateImage('#000', $rotate);
                    /**
                     * Recalculate width and height if number of degrees are not
                     * dividable by 180, meaning height and width is changed.
                     */
                    if ($rotate % 180) {
                        $size = $this->imagick->getImageGeometry();
                        $image->setWidth($size['width'])->setHeight($size['height']);
                    }
                }
                if ($flipHorizontally) {
                    $this->imagick->flopImage();
                }
                if ($flipVertically) {
                    $this->imagick->flipImage();
                }
                if ($rotate || $flipHorizontally || $flipVertically) {
                    // Set the image orientation so it reflects the transformation that's been done
                    $this->imagick->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
                    $image->hasBeenTransformed(true);
                }
            }
        } catch (ImagickException $e) {
            throw new TransformationException($e->getMessage(), 400, $e);
        } catch (ImagickPixelException $e) {
            throw new TransformationException($e->getMessage(), 400, $e);
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @covers Imbo\Image\Transformation\AutoRotate::transform
  */
 public function testWillNotUpdateTheImageWhenNotNeeded()
 {
     $image = $this->getMock('Imbo\\Model\\Image');
     $imagick = $this->getMock('Imagick');
     $imagick->expects($this->once())->method('getImageOrientation')->will($this->returnValue(0));
     $imagick->expects($this->never())->method('setImageOrientation');
     $event = $this->getMock('Imbo\\EventManager\\Event');
     $event->expects($this->once())->method('getArgument')->with('image')->will($this->returnValue($image));
     $transformation = new AutoRotate();
     $transformation->setImagick($imagick);
     $transformation->transform($event);
 }