Thread: The GD class
View Single Post
Old 12-28-2008, 10:00 PM   #5 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

This function creates a thumbnail even if it's below the max size.. but you can change that.

php Code:
/**
     * Creates thumbnail of image and moves to thumbnail dir
     *
     * @return void
     */

    private function createThumb()
    {
       
        switch($_FILES[$this->form_inputName]['type'])
        {
           
            case 'image/jpeg':
               
                $srcimage = imagecreatefromjpeg($this->image_bigPath . $this->file_name);
                break;

            case 'image/gif':

                $srcimage = imagecreatefromgif($this->image_bigPath . $this->file_name);
                break;

            case 'image/png':
               
                $srcimage = imagecreatefrompng($this->image_bigPath . $this->file_name);
                break;
           
            default:
               
                $srcimage = imagecreatefromjpeg($this->image_bigPath . $this->file_name);
               
        }
       

        $srcimagex = imagesx($srcimage);
        $srcimagey = imagesy($srcimage);
       
        if($srcimagex > $this->thumb_width || $srcimagey > $this->thumb_height)
        {
           
            $diffx = $srcimagex - $this->thumb_width;
            $diffy = $srcimagey - $this->thumb_height;
           
            if($srcimagex > $srcimagey || $srcimagey > $srcimagex)
            {
               
                if($diffx > $diffy)
                {
                   
                    $ratio = $this->thumb_width / $srcimagex;
                   
                }
               
                elseif($diffy > $diffx)
                {
                   
                    $ratio = $this->thumb_height / $srcimagey;
                   
                }
               
                else
                {
                   
                    $ratio = $this->thumb_width / $srcimagex;
                   
                }
               
            }
           
            else
            {
               
                if($this->thumb_width > $this->thumb_height || $this->thumb_height > $this->thumb_width)
                {
                   
                    if($diffx > $diffy)
                    {
                       
                        $ratio = $this->thumb_width / $srcimagex;
                       
                    }
                   
                    elseif($diffy > $diffx)
                    {
                       
                        $ratio = $this->thumb_height / $srcimagey;
                       
                    }
                   
                    else
                    {
                       
                        $ratio = $this->thumb_width / $srcimagex;
                       
                    }
                   
                }
               
                else
                {
                   
                    $ratio = $this->thumb_height / $srcimagey;
                   
                }
               
            }

            $thumbwidth = $srcimagex * $ratio;
            $thumbheight = $srcimagey * $ratio;
           
            $thumbnail = imagecreatetruecolor($thumbwidth, $thumbheight);
            imagecopyresampled($thumbnail, $srcimage, 0, 0, 0, 0, $thumbwidth, $thumbheight, $srcimagex, $srcimagey);
            $success = imagejpeg($thumbnail, $this->image_thuPath . $this->file_name, 75);
           
            if($success)
            {
               
                $this->thumb_creation = 1;
               
            }
           
            else throw new Exception('Thumbnail creation did not work.');         

        }
       
        else
        {
           
            if(!imagejpeg($srcimage, $this->image_thuPath . $this->file_name, 75))
            {
                   
                throw new Exception('An error occured when creating a thumbnail.');
                       
            }
           
            else return true;
           
        }
       
    }
__________________
Tanax is offline  
Reply With Quote