<commit_before><?php

namespace stojg\crop;

/**
 *
 * Base class for all Croppers
 *
 */
abstract class Crop
{
    /**
     * Timer used for profiler / debugging
     *
     * @var float
     */
    protected static $start_time = 0.0;

    /**
     *
     * @var \Imagick
     */
    protected $originalImage = null;

    /**
     * Filter to use when resizing:
     * http://php.net/manual/en/imagick.constants.php#imagick.constants.filters
     * @var int
     */
    protected $filter = \Imagick::FILTER_CUBIC;

    /**
     * Blur to use when resizing:
     * http://php.net/manual/en/imagick.resizeimage.php
     * @var float
     */
    protected $blur = 0.5;

    /**
     * baseDimension
     *
     * @var array
     * @access protected
     */
    protected $baseDimension;

    /**
     * Profiling method
     */
    public static function start()
    {
        self::$start_time = microtime(true);
    }

    /**
     * Profiling method
     *
     * @return string
     */
    public static function mark()
    {
        $end_time = (microtime(true) - self::$start_time) * 1000;

        return sprintf("%.1fms", $end_time);
    }

    /**
     *
     * @param string|\Imagick $imagePath - The path to an image to load or an
     *                        Imagick instance. Paths can include wildcards for
     *                        file names, or can be URLs.
     */
    public function __construct($imagePath = null)
    {
        if ($imagePath) {
            if(is_string($imagePath)) {
                $this->setImage(new \Imagick($imagePath));
            } else {
                $this->setImage($imagePath);
            }
        }
    }

    /**
     * Sets the object Image to be croped
     *
     * @param  \Imagick $image
     * @return Crop
     */
    public function setImage(\Imagick $image)
    {
        $this->originalImage = $image;

        // set base image dimensions
        $this->setBaseDimensions(
            $this->originalImage->getImageWidth(),
            $this->originalImage->getImageHeight()
        );
        return $this;
    }

    /**
     * Get the area in pixels for this image
     *
     * @param  \Imagick $image
     * @return int
     */
    protected function area(\Imagick $image)
    {
        $size = $image->getImageGeometry();

        return $size['height'] * $size['width'];
    }

    /**
     * Get the filter value to use for resizeImage call
     * http://php.net/manual/en/imagick.constants.php#imagick.constants.filters
     *
     * @return int
     */
    public function getFilter()
    {
        return $this->filter;
    }

    /**
     * Set the filter value to use for resizeImage call
     * http://php.net/manual/en/imagick.constants.php#imagick.constants.filters
     *
     * @return Crop
     */
    public function setFilter($filter)
    {
        $this->filter = $filter;
        return $this;
    }

    /**
     * Get the blur value to use for resizeImage call
     * http://php.net/manual/en/imagick.resizeimage.php
     *
     * @return float
     */
    public function getBlur()
    {
        return $this->blur;
    }

    /**
     * Set the blur value to use for resizeImage call:
     * http://php.net/manual/en/imagick.resizeimage.php
     *
     * @return Crop
     */
    public function setBlur($blur)
    {
        $this->blur = $blur;
        return $this;
    }

    /**
     * Resize and crop the image so it dimensions matches $targetWidth and $targetHeight
     *
     * @param  int              $targetWidth
     * @param  int              $targetHeight
     * @return boolean|\Imagick
     */
    public function resizeAndCrop($targetWidth, $targetHeight)
    {
        $this->autoOrient();

        // 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'], $this->getFilter(), $this->getBlur());
        // Get the offset for cropping the image further
        $offset = $this->getSpecialOffset($this->originalImage, $targetWidth, $targetHeight);
        // Crop the image
        $this->originalImage->cropImage($targetWidth, $targetHeight, $offset['x'], $offset['y']);

        return $this->originalImage;
    }<commit_msg>added autoOrient config option to crop library<commit_after><?php

namespace stojg\crop;

/**
 *
 * Base class for all Croppers
 *
 */
abstract class Crop
{
    /**
     * Timer used for profiler / debugging
     *
     * @var float
     */
    protected static $start_time = 0.0;

    /**
     *
     * @var \Imagick
     */
    protected $originalImage = null;

    /**
     * Filter to use when resizing:
     * http://php.net/manual/en/imagick.constants.php#imagick.constants.filters
     * @var int
     */
    protected $filter = \Imagick::FILTER_CUBIC;

    /**
     * Blur to use when resizing:
     * http://php.net/manual/en/imagick.resizeimage.php
     * @var float
     */
    protected $blur = 0.5;

    /**
     * Whether image data should be rotated according to EXIF metadata:
     * http://www.imagemagick.org/script/command-line-options.php#auto-orient
     * @var boolean
     */
    protected $autoOrient = true;

    /**
     * baseDimension
     *
     * @var array
     * @access protected
     */
    protected $baseDimension;

    /**
     * Profiling method
     */
    public static function start()
    {
        self::$start_time = microtime(true);
    }

    /**
     * Profiling method
     *
     * @return string
     */
    public static function mark()
    {
        $end_time = (microtime(true) - self::$start_time) * 1000;

        return sprintf("%.1fms", $end_time);
    }

    /**
     *
     * @param string|\Imagick $imagePath - The path to an image to load or an
     *                        Imagick instance. Paths can include wildcards for
     *                        file names, or can be URLs.
     */
    public function __construct($imagePath = null)
    {
        if ($imagePath) {
            if(is_string($imagePath)) {
                $this->setImage(new \Imagick($imagePath));
            } else {
                $this->setImage($imagePath);
            }
        }
    }

    /**
     * Sets the object Image to be croped
     *
     * @param  \Imagick $image
     * @return Crop
     */
    public function setImage(\Imagick $image)
    {
        $this->originalImage = $image;

        // set base image dimensions
        $this->setBaseDimensions(
            $this->originalImage->getImageWidth(),
            $this->originalImage->getImageHeight()
        );
        return $this;
    }

    /**
     * Get the area in pixels for this image
     *
     * @param  \Imagick $image
     * @return int
     */
    protected function area(\Imagick $image)
    {
        $size = $image->getImageGeometry();

        return $size['height'] * $size['width'];
    }

    /**
     * Get the filter value to use for resizeImage call
     * http://php.net/manual/en/imagick.constants.php#imagick.constants.filters
     *
     * @return int
     */
    public function getFilter()
    {
        return $this->filter;
    }

    /**
     * Set the filter value to use for resizeImage call
     * http://php.net/manual/en/imagick.constants.php#imagick.constants.filters
     *
     * @return Crop
     */
    public function setFilter($filter)
    {
        $this->filter = $filter;
        return $this;
    }

    /**
     * Get the blur value to use for resizeImage call
     * http://php.net/manual/en/imagick.resizeimage.php
     *
     * @return float
     */
    public function getBlur()
    {
        return $this->blur;
    }

    /**
     * Set the blur value to use for resizeImage call:
     * http://php.net/manual/en/imagick.resizeimage.php
     *
     * @return Crop
     */
    public function setBlur($blur)
    {
        $this->blur = $blur;
        return $this;
    }

    /**
     * Get whether image data should be rotated according to EXIF metadata:
     * http://www.imagemagick.org/script/command-line-options.php#auto-orient
     *
     * @return boolean
     */
    public function getAutoOrient()
    {
        return $this->autoOrient;
    }

    /**
     * Set whether image data should be rotated according to EXIF metadata:
     * http://www.imagemagick.org/script/command-line-options.php#auto-orient
     *
     * @return Crop
     */
    public function setAutoOrient($autoOrient)
    {
        $this->autoOrient = $autoOrient;
        return $this;
    }

    /**
     * Resize and crop the image so it dimensions matches $targetWidth and $targetHeight
     *
     * @param  int              $targetWidth
     * @param  int              $targetHeight
     * @return boolean|\Imagick
     */
    public function resizeAndCrop($targetWidth, $targetHeight)
    {
        if ($this->getAutoOrient()) {
            $this->autoOrient();
        }

        // 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'], $this->getFilter(), $this->getBlur());
        // Get the offset for cropping the image further
        $offset = $this->getSpecialOffset($this->originalImage, $targetWidth, $targetHeight);
        // Crop the image
        $this->originalImage->cropImage($targetWidth, $targetHeight, $offset['x'], $offset['y']);

        return $this->originalImage;
    }
{'fraction_non_alphanumeric': 0.12490944841146642, 'fraction_numerical': 0.0018627755355479665, 'mean_word_length': 1.8727705112960762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3928512', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2863, 'n_words': 686}