<commit_before><?php
class mtmdImage {

    /** Format constants. */
    const PORTRAIT = 1;
    const LANDSCAPE = 2;

    /** EXIF orientation constants. */
    const ORIENTATION_ORIGINAL                        = 1;
    const ORIENTATION_FLIP_HORIZONTAL                 = 2;
    const ORIENTATION_ROTATE_180_LEFT                 = 3;
    const ORIENTATION_FLIP_VERTICAL                   = 4;
    const ORIENTATION_FLIP_VERTICAL_ROTATE_90_RIGHT   = 5;
    const ORIENTATION_ROTATE_90_RIGHT                 = 6;
    const ORIENTATION_FLIP_HORIZONTAL_ROTATE_90_RIGHT = 7;
    const ORIENTATION_ROTATE_90_LEFT                  = 8;

    /** File info properties. */
    protected $fileName      = '';
    protected $size          = 0;
    protected $width         = 0;
    protected $height        = 0;
    protected $imageType     = null;
    protected $mimeType      = 0;
    protected $bitDepth      = 0;
    protected $channels      = 0;
    protected $format        = null;
    protected $thumbFileName = '';
    protected $thumbWidth    = 0;
    protected $thumbHeight   = 0;
    protected $exifData      = array();
    protected $orientation   = self::ORIENTATION_ORIGINAL;


    /**
     * Constructor.
     *
     * @param string $file
     *
     * @throws Exception
     */
    public function __construct($file)
    {
        if (empty($file)) {
            throw new Exception('Please provide a file!');
        }
        if (!file_exists($file)) {
            throw new Exception('File does not exist!');
        }
        $this->fileName = $file;

        $this->initialize();

    }


    private function initialize()
    {
        $this->getFileInfo();

    }


    /**
     * Determine all file info.
     *
     * @return void
     */
    private function getFileInfo()
    {
        $info            = getimagesize($this->fileName);
        $this->size      = filesize($this->fileName);
        if ($info !== false) {
            $this->width     = $info[0];
            $this->height    = $info[1];
            $this->imageType = $info[2];
            $this->bitDepth  = $info['bits'];
            if (array_key_exists('channels', $info)) {
                $this->channels  = $info['channels'];
            }
        }
        $this->mimeType  = $info['mime'];
        $this->format    = $this->determineFormat();

        if ($this->getType() === IMAGETYPE_JPEG) {
            $this->exifData = $this->readExifData();
        }

    }


    /**
     * Rotates/Flips image resource if necessary using EXIF data. If no EXIF data present nothing will be done.
     *
     * @param resource $imageResource
     *
     * @return resource
     */
    private function rotateOrFlip($imageResource)
    {
        $retResource = $imageResource;
        if (!array_key_exists('Orientation', $this->exifData)) {
            return $retResource;
        }

        switch ($this->exifData['Orientation']) {
            case self::ORIENTATION_FLIP_HORIZONTAL:
                $this->orientation = self::ORIENTATION_FLIP_HORIZONTAL;
                $this->flip($imageResource);
                break;
            case self::ORIENTATION_ROTATE_180_LEFT:
                $this->orientation = self::ORIENTATION_ROTATE_180_LEFT;
                $retResource = imagerotate($imageResource, 180, -1);
                break;
            case self::ORIENTATION_FLIP_VERTICAL:
                $this->orientation = self::ORIENTATION_FLIP_VERTICAL;
                $this->flip($imageResource);
                break;
            case self::ORIENTATION_FLIP_VERTICAL_ROTATE_90_RIGHT:
                $this->orientation = self::ORIENTATION_FLIP_VERTICAL_ROTATE_90_RIGHT;
                $this->flip($imageResource);
                $retResource = imagerotate($imageResource, -90, -1);
                break;
            case self::ORIENTATION_ROTATE_90_RIGHT:
                $this->orientation = self::ORIENTATION_ROTATE_90_RIGHT;
                $retResource = imagerotate($imageResource, -90, -1);
                break;
            case self::ORIENTATION_FLIP_HORIZONTAL_ROTATE_90_RIGHT:
                $this->orientation = self::ORIENTATION_FLIP_HORIZONTAL_ROTATE_90_RIGHT;
                $this->flip($imageResource);
                $retResource = imagerotate($imageResource, -90, -1);
                break;
            case self::ORIENTATION_ROTATE_90_LEFT:
                $this->orientation = self::ORIENTATION_ROTATE_90_LEFT;
                $retResource = imagerotate($imageResource, 90, -1);
                break;
        }

        if ($retResource === false) {
            return $this->fixBrokenOrientation($imageResource);
        }
        return $retResource;

    }


    /**
     * Fixes the orientation of some broken images. Those seem to be landscape but are portrait in reality.
     *<commit_msg>Removed tried fix for re-orientation.<commit_after><?php
class mtmdImage {

    /** Format constants. */
    const PORTRAIT = 1;
    const LANDSCAPE = 2;

    /** EXIF orientation constants. */
    const ORIENTATION_ORIGINAL                        = 1;
    const ORIENTATION_FLIP_HORIZONTAL                 = 2;
    const ORIENTATION_ROTATE_180_LEFT                 = 3;
    const ORIENTATION_FLIP_VERTICAL                   = 4;
    const ORIENTATION_FLIP_VERTICAL_ROTATE_90_RIGHT   = 5;
    const ORIENTATION_ROTATE_90_RIGHT                 = 6;
    const ORIENTATION_FLIP_HORIZONTAL_ROTATE_90_RIGHT = 7;
    const ORIENTATION_ROTATE_90_LEFT                  = 8;

    /** File info properties. */
    protected $fileName      = '';
    protected $size          = 0;
    protected $width         = 0;
    protected $height        = 0;
    protected $imageType     = null;
    protected $mimeType      = 0;
    protected $bitDepth      = 0;
    protected $channels      = 0;
    protected $format        = null;
    protected $thumbFileName = '';
    protected $thumbWidth    = 0;
    protected $thumbHeight   = 0;
    protected $exifData      = array();
    protected $orientation   = self::ORIENTATION_ORIGINAL;


    /**
     * Constructor.
     *
     * @param string $file
     *
     * @throws Exception
     */
    public function __construct($file)
    {
        if (empty($file)) {
            throw new Exception('Please provide a file!');
        }
        if (!file_exists($file)) {
            throw new Exception('File does not exist!');
        }
        $this->fileName = $file;

        $this->initialize();

    }


    private function initialize()
    {
        $this->getFileInfo();

    }


    /**
     * Determine all file info.
     *
     * @return void
     */
    private function getFileInfo()
    {
        $info            = getimagesize($this->fileName);
        $this->size      = filesize($this->fileName);
        if ($info !== false) {
            $this->width     = $info[0];
            $this->height    = $info[1];
            $this->imageType = $info[2];
            $this->bitDepth  = $info['bits'];
            if (array_key_exists('channels', $info)) {
                $this->channels  = $info['channels'];
            }
        }
        $this->mimeType  = $info['mime'];
        $this->format    = $this->determineFormat();

        if ($this->getType() === IMAGETYPE_JPEG) {
            $this->exifData = $this->readExifData();
        }

    }


    /**
     * Rotates/Flips image resource if necessary using EXIF data. If no EXIF data present nothing will be done.
     *
     * @param resource $imageResource
     *
     * @return resource
     */
    private function rotateOrFlip($imageResource)
    {
        $retResource = $imageResource;
        if (!array_key_exists('Orientation', $this->exifData)) {
            return $retResource;
        }

        switch ($this->exifData['Orientation']) {
            case self::ORIENTATION_FLIP_HORIZONTAL:
                $this->orientation = self::ORIENTATION_FLIP_HORIZONTAL;
                $this->flip($imageResource);
                break;
            case self::ORIENTATION_ROTATE_180_LEFT:
                $this->orientation = self::ORIENTATION_ROTATE_180_LEFT;
                $retResource = imagerotate($imageResource, 180, -1);
                break;
            case self::ORIENTATION_FLIP_VERTICAL:
                $this->orientation = self::ORIENTATION_FLIP_VERTICAL;
                $this->flip($imageResource);
                break;
            case self::ORIENTATION_FLIP_VERTICAL_ROTATE_90_RIGHT:
                $this->orientation = self::ORIENTATION_FLIP_VERTICAL_ROTATE_90_RIGHT;
                $this->flip($imageResource);
                $retResource = imagerotate($imageResource, -90, -1);
                break;
            case self::ORIENTATION_ROTATE_90_RIGHT:
                $this->orientation = self::ORIENTATION_ROTATE_90_RIGHT;
                $retResource = imagerotate($imageResource, -90, -1);
                break;
            case self::ORIENTATION_FLIP_HORIZONTAL_ROTATE_90_RIGHT:
                $this->orientation = self::ORIENTATION_FLIP_HORIZONTAL_ROTATE_90_RIGHT;
                $this->flip($imageResource);
                $retResource = imagerotate($imageResource, -90, -1);
                break;
            case self::ORIENTATION_ROTATE_90_LEFT:
                $this->orientation = self::ORIENTATION_ROTATE_90_LEFT;
                $retResource = imagerotate($imageResource, 90, -1);
                break;
        }

        if ($retResource === false) {
            return $imageResource;
            #return $this->fixBrokenOrientation($imageResource);
        }
        return $retResource;

    }


    /**
     * Fixes the orientation of some broken images. Those seem to be landscape but are portrait in reality.
     *
{'fraction_non_alphanumeric': 0.10901402640264027, 'fraction_numerical': 0.014438943894389438, 'mean_word_length': 1.824643169239732, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4300616', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2767, 'n_words': 542}