<commit_before><?php

if(! class_exists("Photo")) {
class Photo {

    private $image;
    private $ext;

    /**
     * Put back gd stuff, not everybody have Imagick
     */
    private $imagick;
    private $width;
    private $height;
    private $valid;
    private $type;
    private $types;

    /**
     * supported mimetypes and corresponding file extensions
     */
    static function supportedTypes() {
        if(class_exists('Imagick')) {
            /**
             * Imagick::queryFormats won't help us a lot there...
             * At least, not yet, other parts of friendica uses this array
             */
            $t = array(
                'image/jpeg' => 'jpg',
                'image/png' => 'png',
                'image/gif' => 'gif'
            );
        } else {
            $t = array();
            $t['image/jpeg'] ='jpg';
            if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
        }

        return $t;
    }

    public function __construct($data, $type=null) {
        $this->imagick = class_exists('Imagick');

        if($this->is_imagick()) {
            $this->image = new Imagick();
            $this->image->readImageBlob($data);

            /**
             * Setup the image to the format of the one we just loaded,
             * we'll change it to something else if we need to at the time we save it
             */
            $this->image->setFormat($this->image->getImageFormat());

            // If it is a gif, it may be animated, get it ready for any future operations
            if($this->image->getFormat() !== "GIF") $this->image = $this->image->coalesceImages();

            $this->ext = strtolower($this->image->getImageFormat());
        } else {
            $this->types = $this->supportedTypes();
            if (!array_key_exists($type,$this->types)){
                $type='image/jpeg';
            }
            $this->valid = false;
            $this->type = $type;
            $this->image = @imagecreatefromstring($data);
            if($this->image !== FALSE) {
                $this->width  = imagesx($this->image);
                $this->height = imagesy($this->image);
                $this->valid  = true;
                imagealphablending($this->image, false);
                imagesavealpha($this->image, true);
            }
        }
    }

    public function __destruct() {
        if($this->image) {
            if($this->is_imagick()) {
                $this->image->clear();
                $this->image->destroy();
                return;
            }
            imagedestroy($this->image);
        }
    }

    public function is_imagick() {
        return $this->imagick;
    }

    public function is_valid() {
        if($this->is_imagick())
            return ($this->image !== FALSE);
        return $this->valid;
    }

    public function getWidth() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick())
            return $this->image->getImageWidth();
        return $this->width;
    }

    public function getHeight() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick())
            return $this->image->getImageHeight();
        return $this->height;
    }

    public function getImage() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick()) {
            /* Clean it */
            $this->image = $this->image->deconstructImages();
            return $this->image;
        }
        return $this->image;
    }

    public function getType() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick()) {
            return $this->image->getImageMimeType();
        }
        return $this->type;
    }

    public function getExt() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick())
            return $this->ext;
        return $this->types[$this->type];
    }

    public function scaleImage($max) {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick()) {
            /**
             * If it is not animated, there will be only one iteration here,
             * so don't bother checking
             */
            // Don't forget to go back to the first frame
            $this->image->setFirstIterator();
            do {
                $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true);
            } while ($this->image->nextImage());
            return;
        }

        $width = $this->width;
        $height = $this->height;

        $dest_width = $dest_height = 0;<commit_msg>getExt should not use Imagick format, mapping ext and mime type is more accurate.<commit_after><?php

if(! class_exists("Photo")) {
class Photo {

    private $image;

    /**
     * Put back gd stuff, not everybody have Imagick
     */
    private $imagick;
    private $width;
    private $height;
    private $valid;
    private $type;
    private $types;

    /**
     * supported mimetypes and corresponding file extensions
     */
    static function supportedTypes() {
        if(class_exists('Imagick')) {
            /**
             * Imagick::queryFormats won't help us a lot there...
             * At least, not yet, other parts of friendica uses this array
             */
            $t = array(
                'image/jpeg' => 'jpg',
                'image/png' => 'png',
                'image/gif' => 'gif'
            );
        } else {
            $t = array();
            $t['image/jpeg'] ='jpg';
            if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
        }

        return $t;
    }

    public function __construct($data, $type=null) {
        $this->imagick = class_exists('Imagick');
        $this->types = $this->supportedTypes();

        if($this->is_imagick()) {
            $this->image = new Imagick();
            $this->image->readImageBlob($data);

            /**
             * Setup the image to the format of the one we just loaded,
             * we'll change it to something else if we need to at the time we save it
             */
            $this->image->setFormat($this->image->getImageFormat());

            // If it is a gif, it may be animated, get it ready for any future operations
            if($this->image->getFormat() !== "GIF") $this->image = $this->image->coalesceImages();
        } else {
            if (!array_key_exists($type,$this->types)){
                $type='image/jpeg';
            }
            $this->valid = false;
            $this->type = $type;
            $this->image = @imagecreatefromstring($data);
            if($this->image !== FALSE) {
                $this->width  = imagesx($this->image);
                $this->height = imagesy($this->image);
                $this->valid  = true;
                imagealphablending($this->image, false);
                imagesavealpha($this->image, true);
            }
        }
    }

    public function __destruct() {
        if($this->image) {
            if($this->is_imagick()) {
                $this->image->clear();
                $this->image->destroy();
                return;
            }
            imagedestroy($this->image);
        }
    }

    public function is_imagick() {
        return $this->imagick;
    }

    public function is_valid() {
        if($this->is_imagick())
            return ($this->image !== FALSE);
        return $this->valid;
    }

    public function getWidth() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick())
            return $this->image->getImageWidth();
        return $this->width;
    }

    public function getHeight() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick())
            return $this->image->getImageHeight();
        return $this->height;
    }

    public function getImage() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick()) {
            /* Clean it */
            $this->image = $this->image->deconstructImages();
            return $this->image;
        }
        return $this->image;
    }

    public function getType() {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick()) {
            return $this->image->getImageMimeType();
        }
        return $this->type;
    }

    public function getExt() {
        if(!$this->is_valid())
            return FALSE;

        return $this->types[$this->getType()];
    }

    public function scaleImage($max) {
        if(!$this->is_valid())
            return FALSE;

        if($this->is_imagick()) {
            /**
             * If it is not animated, there will be only one iteration here,
             * so don't bother checking
             */
            // Don't forget to go back to the first frame
            $this->image->setFirstIterator();
            do {
                $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true);
            } while ($this->image->nextImage());
            return;
        }

        $width = $this->width;
        $height = $this->height;

        $dest_width = $dest_height = 0;
{'fraction_non_alphanumeric': 0.14535009755040104, 'fraction_numerical': 0.00043355733795794494, 'mean_word_length': 1.5824237335572349, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 162, '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': '5785668', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2525, 'n_words': 593}