Location: PHPKode > scripts > Impleo Music Collection Script > impleo/classes/Image.class.php
<?php
/* ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <hide@address.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Christoffer Kjeldgaard Petersen
 * ----------------------------------------------------------------------------
 */

class Image
{
    private $img;
    private $imgName;
    private $type;

    /**
     * Constructs a new image.
     *
     * @param resource $imgResource An image resource identifier of type png, jpg or gif.
     * @param string $name Name of the image.
     */
    public function __construct($imgResource, $name)
    {
        if($imgResource)
        {
            $this->imgName = str_replace(" ", "-", strtolower($name));
            $a = getimagesize($imgResource);
            switch($a[2])
            {
                case 3:
                    $this->img =imagecreatefrompng($imgResource);
                    $this->type = "png";
                    break;
                case 2:
                    $this->img =imagecreatefromjpeg($imgResource);
                    $this->type = "jpg";
                    break;
                case 1:
                    $this->img =imagecreatefromgif($imgResource);
                    $this->type = "gif";
            }
        }
    }

    /**
     *
     * @return int
     */
    public function getWidth()
    {
        return imagesx($this->img);
    }

    /**
     *
     * @return int
     */
    public function getHeight()
    {
        return imagesy($this->img);
    }

    /**
     * Saves the image to a specified directory.
     * @param string $path URL destination of the image
     * @return The name of the image saved.
     */
    public function saveImage($path)
    {
        if (strlen($this->type) > 0)
        {
            $imgUrl = "{$path}{$this->imgName}";
            $name = $this->imgName;

            // Ensure unique image name for original image:
            $boolExists = false;
            $i = 0;
            while ($boolExists == false)
            {
                if (file_exists($imgUrl))
                {
                    $name =  $i . $this->imgName;
                    $imgUrl = $path . $name;
                    $this->imgName = $name;
                    $i++;
                }
                else
                $boolExists = true;
            }

            // Upload:
            switch($this->type)
            {
                case "png":
                    imagepng($this->img, $imgUrl, 100);
                    break;
                case "jpg":
                    imagejpeg($this->img, $imgUrl, 100);
                    break;
                case "gif":
                    imagegif($this->img, $imgUrl);
            }

            return $name;
        }
        return null;
    }

    /**
     * Resize the image
     * @param int $width New width of the image
     * @param int $height [Optional] New height of the image.
     */
    public function resize($width,$height=null)
    {
        $proportion = $width / $this->getWidth();
        if($height==null)
        {
            $height = round(($proportion*$this->getHeight()));
        }
        $im = imagecreatetruecolor($width,$height);
        imagecopyresampled($im, $this->img, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->img=$im;
    }

    /**
     * Crops the image from the top left corner
     * @param int $nw New width of the image.
     * @param int $nh New height of the image.
     */
    public function cropImage($nw, $nh)
    {
        $w = $this->getWidth();
        $h = $this->getHeight();

        $simg = $this->img;

        $dimg = imagecreatetruecolor($nw, $nh);

        $wm = $w/$nw;
        $hm = $h/$nh;

        $h_height = $nh/2;
        $w_height = $nw/2;

        if($w> $h)
        {
            $adjusted_width = $w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $half_width - $w_height;

            imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
        }
        elseif(($w <$h) || ($w == $h))
        {
            $adjusted_height = $h / $wm;
            $half_height = $adjusted_height / 2;
            $int_height = $half_height - $h_height;

            imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
        }
        else
        {
            imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
        }

        $this->img = $dimg;
    }

}

?>
Return current item: Impleo Music Collection Script