HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/files/image_cropper.class.php
<?php

/*
    Handles all tasks regarding the cropping and recreating of an image
*/

class Image_Cropper
{
    private $_src;

    private $_name;

    private $_uploadTarget;

    private $_manual;

    private $_trgtW;

    private $_trgtH;

    private $_srcW;

    private $_srcH;

    private $_srcX;

    private $_srcY;

    private $_dstW;

    private $_dstH;

    private $_dstX;

    private $_dstY;

    public function __construct($src, $name, $manual = false)
    {
        $this->_src = $src;
        $this->_name = $name;
        $this->_manual = $manual;

        list($this->_srcW, $this->_srcH) = getimagesize($this->_src);
        $this->_uploadTarget = DOCUMENT_UPLOADS_ROOT;

        $this->_srcX = $this->_srcY = $this->_dstX = $this->_dstY = $this->_dstW = $this->_dstH = 0;
    }

    /*

        GET / SET

    */

    /**
     * Sets the name of the image
     *
     * @param string
     * @return null
     */
    public function setName($input)
    {
        if (is_string($input)) {
            $this->_name = $input;
        }
    }

    /**
     * Sets the variable to crop manual or automatic
     *
     * @param bool
     * @return null
     */
    public function setManual($input)
    {
        if (is_bool($input)) {
            $this->_manual = $input;
        }
    }

    /**
     * Sets the width and height of the destination image
     *
     * @param int, int
     * @return null
     */
    public function setSides($intW, $intH)
    {
        $this->_trgtW = $intW;
        $this->_trgtH = $intH;
    }

    public function setSrcSides($intW, $intH)
    {
        $this->_srcW = $intW;
        $this->_srcH = $intH;
    }

    /**
     * Sets the x and y of the source image
     *
     * @param int, int
     * @return null
     */
    public function setSrcPosition($intX, $intY)
    {
        if (is_numeric($intX) && is_numeric($intY)) {
            $this->_srcX = $intX;
            $this->_srcY = $intY;
        }
    }

    /**
     * Sets the x and y of the destination image
     *
     * @param int, int
     * @return null
     */
    public function setDstPosition($intX, $intY)
    {
        if (is_numeric($intX) && is_numeric($intY)) {
            $this->_dstX = $intX;
            $this->_dstY = $intY;
        }
    }

    /*

        IMAGE HANDLING

    */

    /**
     * Creates the image
     *
     * @param
     * @return string
     */
    public function createImage()
    {
        if (! $this->_manual) {
            $this->calculateCrop();
        } else {
            $this->_dstW = $this->_trgtW;
            $this->_dstH = $this->_trgtH;
        }

        $temp = explode('.', $this->_src);
        $fileExt = strtolower($temp[(count($temp) - 1)]);

        switch($fileExt) {
            case 'jpg':
            case 'jpeg':
                $img_raw = imagecreatefromjpeg($this->_src);
                break;
            case 'png':
                $img_raw = imagecreatefrompng($this->_src);
                break;
            case 'gif':
                $img_raw = imagecreatefromgif($this->_src);
                break;
        }

        $dst_r = imagecreatetruecolor($this->_trgtW, $this->_trgtH);
        imagealphablending($dst_r, false);
        imagesavealpha($dst_r, true);

        //echo $dst_r.', '.$img_raw.', dstX:'. $this->_dstX.', dstY:'. $this->_dstY.', srcX:'. $this->_srcX.', srcY:'. $this->_srcY.', dstW:'. $this->_dstW.', dstH:'. $this->_dstH.', srcW:'. $this->_srcW.', srcH:'. $this->_srcH;

        imagecopyresampled($dst_r, $img_raw, $this->_dstX, $this->_dstY, $this->_srcX, $this->_srcY, $this->_dstW, $this->_dstH, $this->_srcW, $this->_srcH);

        $path = $this->_uploadTarget.urlencode($this->_name);

        $valid = false;
        if ($fileExt == 'png' || $fileExt == 'gif') {
            if (imagepng($dst_r, $path, 8)) {
                $valid = true;
            }
        } elseif (imagejpeg($dst_r, $path, 90)) {
            $valid = true;
        }

        return $valid;
    }

    private function calculateCrop()
    {
        // option 1; crop by width and height

        if (! empty($this->_trgtW) && ! empty($this->_trgtH)) {
            if (($this->_srcW / $this->_srcH) > ($this->_trgtW / $this->_trgtH)) {
                // dst image is less wide then src image
                if ($this->_trgtH <= $this->_srcH) {
                    $this->_dstH = $this->_trgtH;
                    $this->_dstW = ceil(($this->_dstH * $this->_srcW) / $this->_srcH);
                } else {
                    // if target is larger then source, don't scale up

                    // don't scale height
                    $this->_dstH = $this->_trgtH = $this->_srcH;

                    if ($this->_trgtW > $this->_srcW) {
                        // don't scale with
                        $this->_dstW = $this->_trgtW = $this->_srcW;
                    } else {
                        $this->_dstW = ceil(($this->_dstH * $this->_srcW) / $this->_srcH);
                    }
                }
                $this->_dstX = ceil(($this->_dstW - $this->_trgtW) / -2);
                $this->_dstY = 0;
            } else {
                // dst image is wider then src image

                if ($this->_trgtW <= $this->_srcW) {
                    $this->_dstW = $this->_trgtW;
                    $this->_dstH = ceil(($this->_dstW * $this->_srcH) / $this->_srcW);
                } else {
                    // if target is larger then source, don't scale up

                    // don't scale width
                    $this->_dstW = $this->_trgtW = $this->_srcW;

                    if ($this->_trgtH > $this->_srcH) {
                        // don't scale height
                        $this->_dstH = $this->_trgtH = $this->_srcH;
                    } else {
                        $this->_dstH = ceil(($this->_dstW * $this->_srcH) / $this->_srcW);
                    }
                }

                $this->_dstX = 0;
                $this->_dstY = ceil(($this->_dstH - $this->_trgtH) / -2);
            }
        }

        // option 2; crop by width

        elseif (! empty($this->_trgtW) && empty($this->_trgtH)) {
            if ($this->_trgtW <= $this->_srcW) {
                $this->_dstW = $this->_trgtW;
            } else {
                $this->_dstW = $this->_trgtW = $this->_srcW;
            }
            $this->_dstH = $this->_trgtH = ceil(($this->_dstW * $this->_srcH) / $this->_srcW);

            $this->_dstX = 0;
            $this->_dstY = 0;
        }

        // option 3; crop by height

        elseif (empty($this->_trgtW) && ! empty($this->_trgtH)) {
            if ($this->_trgtH <= $this->_srcH) {
                $this->_dstH = $this->_trgtH;
            } else {
                $this->_dstH = $this->_trgtH = $this->_srcH;
            }

            $this->_dstW = $this->_trgtW = ceil(($this->_dstH * $this->_srcW) / $this->_srcH);
            $this->_dstX = 0;
            $this->_dstY = 0;
        }

        // option 4; don't crop

        elseif (empty($this->_trgtW) && empty($this->_trgtH)) {
            $this->_dstW = $this->_trgtW = $this->_srcW;
            $this->_dstH = $this->_trgtH = $this->_srcH;
            $this->_dstX = 0;
            $this->_dstY = 0;
        }
    }
}