Skip to content
Snippets Groups Projects
Select Git revision
  • 64f1a73dd50943eae2f45e24b410defaa5db6dc1
  • master default
  • shib-cas
3 results

Image.php

Blame
  • Image.php 3.09 KiB
    <?php
    
    Zend_Loader::loadClass('Zend_Filter');
    Zend_Loader::loadClass('Nmc_Math_OrderedPair');
    Zend_Loader::loadClass('Nmc_Image_Exception');
    
    class Nmc_Image {
    
        const IMAGE_FORMAT_PNG = 0x1;
        const IMAGE_FORMAT_GIF = 0x2;
        const IMAGE_FORMAT_JPEG = 0x4;
    
        const ALIGN_VERTICAL_TOP = 0x01;
        const ALIGN_VERTICAL_MIDDLE = 0x02;
        const ALIGN_VERTICAL_BOTTOM = 0x04;
        const ALIGN_HORIZONTAL_LEFT = 0x10;
        const ALIGN_HORIZONTAL_CENTER = 0x20;
        const ALIGN_HORIZONTAL_RIGHT = 0x40;
    
        const _ALIGN_VERTICAL_MASK = 0x0F;
        const _ALIGN_HORIZONTAL_MASK = 0xF0;
    
        /**
         * Returns a new Nmc_Image object created from the image file
         *
         * @param string $filePath
         * @return Nmc_Image_Abstract
         * @throws Nmc_Image_Exception
         */
        public static function fromFile($filePath)
        {
            $fp = @fopen($filePath, 'r');
            if($fp !== false) {
                fclose($fp);
            } else {
                throw new Nmc_Image_Exception('$filePath does not exist or cannot be read');
            }
    
            $dim = @getimagesize($filePath);
    		switch( $dim[2] )
    		{
    			case IMAGETYPE_GIF:
    				$img = imagecreatefromgif($filePath);
    				return new Nmc_Image_Gif($img);
    			break;
    
    			case IMAGETYPE_JPEG:
    				$img = imagecreatefromjpeg($filePath);
    				return new Nmc_Image_Jpeg($img);
    			break;
    
    			case IMAGETYPE_PNG:
    				$img = imagecreatefrompng($filePath);
    				return new Nmc_Image_Png($img);
    			break;
    
    			default:
    			    throw new Nmc_Image_Exception('$filePath was not a valid image type' );
    			break;
    		}
        }
    
        /**
         * Returns a new Nmc_Image object created from the supplied resource
         *
         * @param gd_resource $imageResource
         * @return Nmc_Image_Abstract
         * @throws Nmc_Image_Exception
         */
        public static function fromResource($imageResource)
        {
            return new Nmc_Image_Png($imageResource);
        }
    
        /**
         * Returns a new Nmc_Image object of a blank image with specified
         * width, height, and optional color (defaults to transparent).
         *
         * @param Nmc_Math_OrderedPair $size
         * @param Nmc_Image_Color[optional] $backgroundColor
         * @return Nmc_Image_Abstract
         * @throws Nmc_Image_Exception
         *
         */
    
        public static function fromNewImage(Nmc_Math_OrderedPair $size, Nmc_Image_Color $backgroundColor = null)
        {
            if(!$size->inQuadrant(1)) {
                throw new Nmc_Image_Exception('$size must have dimensions > 0');
            }
    
            $newImg = imagecreatetruecolor($size->x, $size->y);
            if($backgroundColor == null) {
                $bgc = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
            } else {
                $bgc = imagecolorallocatealpha($newImg,
                                               $backgroundColor->r,
                                               $backgroundColor->g,
                                               $backgroundColor->b,
                                               $backgroundColor->a);
            }
            imagealphablending($newImg, false);
            imagefilledrectangle($newImg, 0, 0, $size->x, $size->y, $bgc);
            imagealphablending($newImg, true);
    
            return new Nmc_Image_Png($newImg);
        }
    }
    
    ?>