jdeniau
10/31/2014 - 2:08 PM

CropFace.php

<?php

use stojg\crop\CropFace as BaseCropFace;

/**
 * CropFace
 */
class CropFace extends BaseCropFace
{
    /**
     * Resize and crop the image so it dimensions matches $targetWidth and $targetHeight
     *
     * @param string $imagePath
     * @param int $targetWidth
     * @param int $targetHeight
     * @return boolean|\Imagick
     */
    public function drawInformations($targetWidth, $targetHeight)
    {
        //safe zone list

        // First get the size that we can use to safely trim down the image without cropping any sides
        $crop = $this->getSafeResizeOffset($this->originalImage, $targetWidth, $targetHeight);

        // Resize the image
        $this->originalImage->resizeImage($crop['width'], $crop['height'], \Imagick::FILTER_CUBIC, .5);
        //ld($this->originalImage->getImageWidth(), $this->originalImage->getImageHeight());
        $safeZoneList = $this->getSafeZoneList();

        foreach ($safeZoneList as $safeZone) {
            $draw = new \ImagickDraw();
            $draw->setFillColor('rgba(0, 200, 0, 0.4)');

            $draw->rectangle($safeZone['left'], $safeZone['top'], $safeZone['right'], $safeZone['bottom']);
            $this->originalImage->drawImage($draw);
        }

        // Get the offset for cropping the image further
        $offset = $this->getSpecialOffset($this->originalImage, $targetWidth, $targetHeight);


        $imageWidth = $this->originalImage->getImageWidth();
        $imageHeight = $this->originalImage->getImageHeight();
        $rectangles = [
            [0, 0, $offset['x'], $imageHeight],
            [0, 0, $imageWidth, $offset['y']],
            [$offset['x'] + $targetWidth, 0, $imageWidth, $imageHeight],
            [0, $offset['y'] + $targetHeight, $imageWidth, $imageHeight],
        ];

        foreach ($rectangles as $rectangle) {
            $draw = new \ImagickDraw();
            $draw->setFillColor('rgba(0, 0, 0, 0.8)');

            $draw->rectangle($rectangle[0], $rectangle[1], $rectangle[2], $rectangle[3]);
            $this->originalImage->drawImage($draw);
        }

        // Crop the image
        //$this->originalImage->cropImage($targetWidth, $targetHeight, $offset['x'], $offset['y']);
        return $this->originalImage;
    }
}