Advertisement
gidmakus

ImageResizerFull

May 16th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.67 KB | None | 0 0
  1. <?php
  2.     /**
  3.      * A class to resize and upload image to a directory.
  4.      */
  5.     class Image_Resize_Upload
  6.     {
  7.         private $imgFilePath;
  8.         private $newWidth;
  9.         private $newHeight;
  10.         private $orgWidth;
  11.         private $orgHeight;
  12.         private $image;
  13.         private $imgType;
  14.         private $imgData;
  15.        
  16.         private $resizeImg;
  17.        
  18.         /**
  19.          * Default construct
  20.          * @param Integer - the width to resize image to.
  21.          * @param Integer - the height to resize image to.
  22.          */
  23.         public function __construct( $width, $height = 0 )
  24.         {
  25.             $this->setNewWidth( $width );
  26.             $this->setNewHeight( $height );
  27.         }
  28.        
  29.         /**
  30.          *  sets the image data.
  31.          * @param String the path to the image
  32.          */
  33.         public function setImage( &$image )
  34.         {
  35.             $this->image = $image;
  36.         }
  37.        
  38.         /**
  39.          * sets the image data.
  40.          * @param String - the path for the file name.
  41.          */
  42.         public function setImgData( &$fileName )
  43.         {
  44.             $this->imgData = getimagesize( $fileName );
  45.            
  46.         }
  47.    
  48.         /**
  49.          * sets the image mime type.
  50.          * @param Array - image data.
  51.          */
  52.         public function setImgType( &$imgData )
  53.         {
  54.             $this->imgType = $imgData['mime'];
  55.         }
  56.        
  57.         /**
  58.          * sets the original width of the image
  59.          * @param String the image data.
  60.          */
  61.         public function setOrgWidth( &$imgData )
  62.         {
  63.             list( $orgWidth, $orgHeight ) = $imgData;
  64.             $this->orgWidth = $orgWidth;
  65.         }
  66.        
  67.         /**
  68.          * sets the original height of the image.
  69.          * @param String the image data.
  70.          */
  71.         public function setOrgHeight( &$imgData )
  72.         {
  73.             list( $orgWidth, $orgHeight ) = $imgData;
  74.            
  75.             $this->orgHeight = $orgHeight;
  76.         }
  77.        
  78.         /**
  79.          * sets the max width
  80.          * @param Integer - image actual width
  81.          */
  82.         public function setNewWidth( &$width )
  83.         {
  84.             $this->newWidth = $width;
  85.         }
  86.        
  87.         /**
  88.          * sets the max height
  89.          * @param Integer - image actual height
  90.          */
  91.         public function setNewHeight( &$height )
  92.         {
  93.             $this->newHeight = $height;
  94.         }
  95.        
  96.         /**
  97.          * sets the image file path.
  98.          * @param String - the path where the new image should reside.
  99.          */
  100.         public function setImgPath( $imgFilePath )
  101.         {
  102.             $this->imgFilePath = $imgFilePath;
  103.         }
  104.        
  105.         /**
  106.          * gets the image data.
  107.          * @return image data.
  108.          */
  109.         public function getImgData()
  110.         {
  111.             return $this->imgData;
  112.         }
  113.        
  114.         /**
  115.          * gets the image mime type
  116.          * @return image type
  117.          */
  118.         public function getImgType()
  119.         {
  120.             return $this->imgType;
  121.         }
  122.        
  123.         /**
  124.          * gets the image.
  125.          * @return image.
  126.          */
  127.         public function getImage()
  128.         {
  129.             return $this->image;
  130.         }
  131.        
  132.         /**
  133.          * gets the new width
  134.          * @return new width
  135.          */
  136.         public function getNewWidth()
  137.         {
  138.             return $this->newWidth;
  139.         }
  140.        
  141.         /**
  142.          * gets the max height.
  143.          * @return new height.
  144.          */
  145.         public function getNewHeight()
  146.         {
  147.             return $this->newHeight;
  148.         }
  149.        
  150.         /**
  151.          * gets the path where image should be saved.
  152.          * @return destination path.
  153.          */
  154.         public function getImgPath()
  155.         {
  156.             return $this->imgFilePath;
  157.         }
  158.        
  159.         /**
  160.          * gets the original image width.
  161.          * @return the original width.
  162.          */
  163.         public function getOrgWidth()
  164.         {
  165.             return $this->orgWidth;
  166.         }
  167.        
  168.         /**
  169.          * gets the original image height.
  170.          * @return the original height.
  171.          */
  172.         public function getOrgHeight()
  173.         {
  174.             return $this->orgHeight;
  175.         }
  176.        
  177.         /**
  178.          * gets the maximum allowed image size.
  179.         * @return the maximum size.
  180.         */
  181.         public function getImgMaxSize()
  182.         {
  183.             return $this->$maxFileSize;
  184.         }
  185.        
  186.         /**
  187.          * resizes image to a new dimension.
  188.          * @param width - the new image width.
  189.          * @param height - the new image height.
  190.          */
  191.         public function resizeImage()
  192.         {
  193.             $this->setOrgWidth( $this->getImgData() );
  194.             $this->setOrgHeight( $this->getImgData() );
  195.            
  196.             //which height to use
  197.             $height =   $this->getNewHeight() == 0 ? $this->calHeight() :
  198.             $this->getNewHeight();
  199.             $this->getImgType();
  200.             $this->createImage();
  201.            
  202.             $imageNewSize = imagecreatetruecolor( $this->getNewWidth(),
  203.                 $height );
  204.            
  205.             imagecopyresampled( $imageNewSize,
  206.             $this->createImage(), 0, 0, 0, 0,$this->getNewWidth(),
  207.             $height, $this->getOrgWidth(),
  208.             $this->getOrgHeight() );
  209.            
  210.             $mimeType = $this->getImgType();
  211.             ob_start();
  212.             switch( $mimeType )
  213.             {
  214.                 case 'image/png':
  215.                     imagepng( $imageNewSize, $this->getImgPath() );
  216.                 break;
  217.                
  218.                 case 'image/jpeg':
  219.                     imagejpeg( $imageNewSize, $this->getImgPath() );
  220.                 break;
  221.                
  222.                 case 'image/gif':
  223.                     imagegif( $imageNewSize, $this->getImgPath() );
  224.                 break;  
  225.             }
  226.            
  227.             $this->resizedImage = ob_get_contents();
  228.             ob_end_clean();
  229.            
  230.             return $this->resizedImage;
  231.            
  232.         }
  233.        
  234.         /**
  235.          * creates image for resizing.
  236.          */
  237.         public function createImage()
  238.         {
  239.             $mimeType = $this->getImgType();
  240.             switch( $mimeType )
  241.             {
  242.                 case 'image/png':
  243.                     return imagecreatefrompng(
  244.                         $this->getImage() );
  245.                 break;
  246.                
  247.                 case 'image/jpeg':
  248.                     return imagecreatefromjpeg(
  249.                         $this->getImage() );
  250.                 break;
  251.                
  252.                 case 'image/gif':
  253.                     return imagecreatefromgif(
  254.                         $this->getImage() );
  255.                
  256.                 break;  
  257.             }
  258.         }
  259.        
  260.         /**
  261.          * calculates a height for the image to maintain proportionality when
  262.          * resizing the image.
  263.          *
  264.          * @return the new height
  265.          */
  266.         public function calHeight()
  267.         {
  268.             $aspectRatio = ( $this->getOrgHeight() / $this->getOrgWidth() );
  269.  
  270.             $newHeight = ( $aspectRatio * $this->getNewWidth() );
  271.             return $newHeight;
  272.         }
  273.        
  274.         /**
  275.          * renames the filename. replaces all white spaces with
  276.          * under scores and change all letters to lowercase.
  277.          * @param String the name of the image.
  278.          *
  279.          * @return replaced all spaces with under scores and all characters
  280.          * are in lowercase.
  281.          */
  282.         public function renameImage( &$filename )
  283.         {
  284.             $newFilename = str_replace( " ", "_",$filename );
  285.             return strtolower( $newFilename );
  286.         }
  287.        
  288.         /**
  289.          * uploads image after resize
  290.          *
  291.          * @param String - the path where the image resides.
  292.          * @return Integer - 0: file upload succeeded.
  293.          * 1: file is not being upload via HTTP.
  294.          * 2: couldn't copy file to it destination folder.
  295.          */
  296.         public function uploadImage( $sourcePath, $destPath, $fileName )
  297.         {
  298.             $file = $this->renameImage( $fileName );
  299.            
  300.             $this->setImgPath( $destPath.$file );
  301.            
  302.                 if( is_uploaded_file( $sourcePath ) ) {
  303.                
  304.                     if( copy( $sourcePath, $this->getImgPath() ) ) {
  305.                         $this->setImage(  $this->getImgPath() );
  306.                         $this->setImgData( $this->getImage() );
  307.                         $this->setImgType( $this->getImgData() );
  308.                        
  309.                         return 0;
  310.                     } else {
  311.                        
  312.                         return 2;
  313.                     }          
  314.                
  315.                 } else {
  316.                
  317.                     return 1;
  318.                 }
  319.              
  320.         }
  321.        
  322.     }
  323.  
  324. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement