Skip to main content
deleted 798 characters in body
Source Link
user7459
user7459
 <?php

/**
 *      Module  :       Model
 *      Name    :       Upload
 *      Input   :       File Information
 *      Output  :       Resized Files in .jpg format
 *      Notes   :

 resizeMove() - resizes the picture to $maxMedium and $maxSmall and moves the file to a permanent location.  
 makeDimensions() - calculates the dimensions of the new images so that there is not distortion if possible.
 getImage() - creates a php image for manipulation.
 updateSessionAndDb - updates the mysql table - move out.

 */

    class Upload
    {
        private $originalWidth, 
                $originalHeight, 
                $newWidth, 
                $newHeight, 
                $maxMedium = 50,
            $maxSmall = 20;

    private $src$maxSmall = NULL;
20;
    private 
         private $src = $fileType,NULL;
            $fileName,
        private  
   $sessionId,
            $path_medium $fileType,
            $path_small;

    function __construct($fileType, $fileName),
    {
        if(Constant::DEBUG_ON)
    $sessionId,
    {
            Session::start();$path_medium,
            Session::activate(0, 1000, 'Test Account A', 'bookmarks');$path_small;
        }

        /**
     function __construct($fileType, $fileName)
  *    Initialize the Object{
     
     */

        $this->sessionId = Session::get('id');
            $this->path_medium = Constant::PICTURES . "$this->sessionId.jpg";
            $this->path_small = Constant::PICTURES . "$this->sessionId-1.jpg";
        $this->fileType = $fileType;
        $this->fileName>fileType = $fileName;

        /**$fileType;
         *    Instantiate$this->fileName the= Object$fileName;
         */

        $this->instantiate();
    }
 
    private function instantiate()
    {
        $this->createImages();
      private function $this->updateSessionAndDbcreateImages();
    }

    private function createImages(){
    {
        if(move_uploaded_file($this->fileName, $this->path_medium))
            {
                if($this->getImage($this->path_medium))
                {
                    list($this->originalWidth,$this->originalHeight)=getimagesize($this->path_medium);
                    $this->resizeMove($this->maxMedium,$this->path_medium);
                    $this->resizeMove($this->maxSmall,$this->path_small);
                    imagedestroy($this->src);
            }
        }
    }
    private function updateSessionAndDb()
    {
        $db = new Database();}
        $result = $db->_pdoQuery('none', 'update_picture', array($this->sessionId));}
        Session::set('picture',1);
    }
    private function resizeMove($max, $path)
        {
            $this->makeDimensions($max);
            $image_true_color = imagecreatetruecolor($this->newWidth, $this->newHeight);
            imagecopyresampled($image_true_color, $this->src, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->
            originalWidth, $this->originalHeight);
            imagejpeg($image_true_color, $path);
            imagedestroy($image_true_color);
        }
    
        private function makeDimensions($max)
        {
            $this->newWidth=$this->originalWidth; 
            $this->newHeight=$this->originalHeight;
            if(($this->originalWidth > $this->originalHeight) && ($this->originalWidth > $max))
            {
                $this->newWidth = $max;
                $this->newHeight = ($max / $this->originalWidth) * $this->originalHeight;
            }
            elseif($this->originalHeight > $this->originalWidth && $this->originalHeight > $max)
            {
                $this->newHeight = $max;
                $this->newWidth = ($max / $this->originalHeight) * $this->originalWidth;
            } 
            elseif ($this->originalWidth > $max)
            {
                $this->newWidth = $this->newHeight = $max;
            }
        }
    
        private function getImage($path)
        {
            $type_creators = array( 
                'image/gif' => 'imagecreatefromgif', 
                'image/pjpeg' => 'imagecreatefromjpeg', 
                'image/jpeg' => 'imagecreatefromjpeg', 
                'image/png' => 'imagecreatefrompng'); 
            if(array_key_exists($this->fileType, $type_creators)) 
            { 
                $this->src = $type_creators[$this->fileType]($path); 
                return true; 
            }
        return false; 
        }
    }
<?php

/**
 *      Module  :       Model
 *      Name    :       Upload
 *      Input   :       File Information
 *      Output  :       Resized Files in .jpg format
 *      Notes   :

 resizeMove() - resizes the picture to $maxMedium and $maxSmall and moves the file to a permanent location.  
 makeDimensions() - calculates the dimensions of the new images so that there is not distortion if possible.
 getImage() - creates a php image for manipulation.
 updateSessionAndDb - updates the mysql table - move out.

 */

class Upload
{
    private $originalWidth, 
            $originalHeight, 
            $newWidth, 
            $newHeight, 
            $maxMedium = 50,
            $maxSmall = 20;

    private $src = NULL;

    private 
            $fileType,
            $fileName,
            $sessionId,
            $path_medium,
            $path_small;

    function __construct($fileType, $fileName)
    {
        if(Constant::DEBUG_ON)
        {
            Session::start();
            Session::activate(0, 1000, 'Test Account A', 'bookmarks');
        }

        /**
         *    Initialize the Object
         */

        $this->sessionId = Session::get('id');
        $this->path_medium = Constant::PICTURES . "$this->sessionId.jpg";
        $this->path_small = Constant::PICTURES . "$this->sessionId-1.jpg";
        $this->fileType = $fileType;
        $this->fileName = $fileName;

        /**
         *    Instantiate the Object
         */

        $this->instantiate();
    }
 
    private function instantiate()
    {
        $this->createImages();
        $this->updateSessionAndDb();
    }

    private function createImages()
    {
        if(move_uploaded_file($this->fileName, $this->path_medium))
        {
            if($this->getImage($this->path_medium))
            {
                list($this->originalWidth,$this->originalHeight)=getimagesize($this->path_medium);
                $this->resizeMove($this->maxMedium,$this->path_medium);
                $this->resizeMove($this->maxSmall,$this->path_small);
                imagedestroy($this->src);
            }
        }
    }
    private function updateSessionAndDb()
    {
        $db = new Database();
        $result = $db->_pdoQuery('none', 'update_picture', array($this->sessionId));
        Session::set('picture',1);
    }
    private function resizeMove($max, $path)
    {
        $this->makeDimensions($max);
        $image_true_color = imagecreatetruecolor($this->newWidth, $this->newHeight);
        imagecopyresampled($image_true_color, $this->src, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->
        originalWidth, $this->originalHeight);
        imagejpeg($image_true_color, $path);
        imagedestroy($image_true_color);
    }
    private function makeDimensions($max)
    {
        $this->newWidth=$this->originalWidth; 
        $this->newHeight=$this->originalHeight;
        if(($this->originalWidth > $this->originalHeight) && ($this->originalWidth > $max))
        {
            $this->newWidth = $max;
            $this->newHeight = ($max / $this->originalWidth) * $this->originalHeight;
        }
        elseif($this->originalHeight > $this->originalWidth && $this->originalHeight > $max)
        {
            $this->newHeight = $max;
            $this->newWidth = ($max / $this->originalHeight) * $this->originalWidth;
        } 
        elseif ($this->originalWidth > $max)
        {
            $this->newWidth = $this->newHeight = $max;
        }
    }
    private function getImage($path)
    {
        $type_creators = array( 
            'image/gif' => 'imagecreatefromgif', 
            'image/pjpeg' => 'imagecreatefromjpeg', 
            'image/jpeg' => 'imagecreatefromjpeg', 
            'image/png' => 'imagecreatefrompng'); 
        if(array_key_exists($this->fileType, $type_creators)) 
        { 
            $this->src = $type_creators[$this->fileType]($path); 
            return true; 
        }
    return false; 
    }
}
 <?php

/**
 *      Module  :       Model
 *      Name    :       Upload
 *      Input   :       File Information
 *      Output  :       Resized Files in .jpg format
 *      Notes   :

 resizeMove() - resizes the picture to $maxMedium and $maxSmall and moves the file to a permanent location.  
 makeDimensions() - calculates the dimensions of the new images so that there is not distortion if possible.
 getImage() - creates a php image for manipulation.
 updateSessionAndDb - updates the mysql table - move out.

 */

    class Upload
    {
        private $originalWidth, 
                $originalHeight, 
                $newWidth, 
                $newHeight, 
                $maxMedium = 50,
                $maxSmall = 20;
    
        private $src = NULL;
    
        private  
                $fileType,
                $fileName,
                $sessionId,
                $path_medium,
                $path_small;
    
        function __construct($fileType, $fileName)
        {
     
            $this->sessionId = Session::get('id');
            $this->path_medium = Constant::PICTURES . "$this->sessionId.jpg";
            $this->path_small = Constant::PICTURES . "$this->sessionId-1.jpg";
            $this->fileType = $fileType;
            $this->fileName = $fileName;
    
        }
    
        private function createImages()
        {
            if(move_uploaded_file($this->fileName, $this->path_medium))
            {
                if($this->getImage($this->path_medium))
                {
                    list($this->originalWidth,$this->originalHeight)=getimagesize($this->path_medium);
                    $this->resizeMove($this->maxMedium,$this->path_medium);
                    $this->resizeMove($this->maxSmall,$this->path_small);
                    imagedestroy($this->src);
                }
            }
        }
    
        private function resizeMove($max, $path)
        {
            $this->makeDimensions($max);
            $image_true_color = imagecreatetruecolor($this->newWidth, $this->newHeight);
            imagecopyresampled($image_true_color, $this->src, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->
            originalWidth, $this->originalHeight);
            imagejpeg($image_true_color, $path);
            imagedestroy($image_true_color);
        }
    
        private function makeDimensions($max)
        {
            $this->newWidth=$this->originalWidth; 
            $this->newHeight=$this->originalHeight;
            if(($this->originalWidth > $this->originalHeight) && ($this->originalWidth > $max))
            {
                $this->newWidth = $max;
                $this->newHeight = ($max / $this->originalWidth) * $this->originalHeight;
            }
            elseif($this->originalHeight > $this->originalWidth && $this->originalHeight > $max)
            {
                $this->newHeight = $max;
                $this->newWidth = ($max / $this->originalHeight) * $this->originalWidth;
            } 
            elseif ($this->originalWidth > $max)
            {
                $this->newWidth = $this->newHeight = $max;
            }
        }
    
        private function getImage($path)
        {
            $type_creators = array( 
                'image/gif' => 'imagecreatefromgif', 
                'image/pjpeg' => 'imagecreatefromjpeg', 
                'image/jpeg' => 'imagecreatefromjpeg', 
                'image/png' => 'imagecreatefrompng'); 
            if(array_key_exists($this->fileType, $type_creators)) 
            { 
                $this->src = $type_creators[$this->fileType]($path); 
                return true; 
            }
        return false; 
        }
    }
edited title
Link
user7459
user7459

Image upload classUpload Class

Tweeted twitter.com/#!/StackCodeReview/status/157650070207082497
improved formatting
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

Image Upload Classupload class

This file takes a user picture uplaodupload and converts two pictuespictures which are named according to the Session ID.

Right now he biggest drawback is that you can notcannot crop the image at upload time like you can on popluarpopular sites like Facebook.

However I'm just looking for overall constructive criticism on the structure of the class. TheThe paths to save the files to are Cosntantsconstants.

I thought about breaking it up in to two classes but I want to stick with just one.

Thanks!

Image Upload Class

This file takes a user picture uplaod and converts two pictues which are named according to the Session ID.

Right now he biggest drawback is that you can not crop the image at upload time like you can on popluar sites like Facebook.

However I'm just looking for overall constructive criticism on the structure of the class. The paths to save the files to are Cosntants.

I thought about breaking it up in to two classes but I want to stick with just one.

Thanks!

Image upload class

This file takes a user picture upload and converts two pictures which are named according to the Session ID.

Right now he biggest drawback is that you cannot crop the image at upload time like you can on popular sites like Facebook.

However I'm just looking for overall constructive criticism on the structure of the class. The paths to save the files to are constants.

I thought about breaking it up in to two classes but I want to stick with just one.

Source Link
user7459
user7459
Loading