Creating Image Thumbnails without using any external plugins

I wrote the below code ( searched Google and got here and there ) . This is working perfect . But do you guys see any issues with this approach ? Or any better way to handle it ? There are good number of plugins , but I wanted this to be very very light and came up with this approach.

//view


$this->widget('ext.MUploadify',array(

    'name'=>'Photo',

    'buttonText'=>'Browse Photos',    

    'multi' => true,

    'script'=>array('mailBox/upload'),

    'fileExt' => '*.jpg;*.png;',

    'method' => 'post',

    'uploadLimit'  => '1',

    'uploadButtonText' =>'Upload Photos'

));

//Action




    public function actionUpload() {

        

        if (isset($_POST['Photo'])) {

            $user_id=10;

            $album_id=15;

            $filename=$_POST['Filename'] ;

            $imageoriginal = CUploadedFile::getInstanceByName('Photo');

            $tempFile = $imageoriginal->getTempName();

            $fileName =  $filename;

            $targetPath = $_SERVER['DOCUMENT_ROOT'].date('Y'). '/'.date('m'). '/'.date('d'). '/'.$user_id. '/'.$album_id. '/' ;

            $targetFile =  str_replace('//','/',$targetPath) . $fileName;

            if (!is_dir($targetPath)) {

                mkdir($targetPath,0755,true);

            }

            $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); 	


            if($extension=="jpg" || $extension=="jpeg" )

            {

                    $src = imagecreatefromjpeg($tempFile);

            }

            else if($extension=="png")

            {

                    $src = imagecreatefrompng($tempFile);

            }

            else

            {

                    $src = imagecreatefromgif($tempFile);

            }


            list($width,$height)=getimagesize($tempFile);


            $newwidth=60;

            $newheight=($height/$width)*$newwidth;

            $tmp=imagecreatetruecolor($newwidth,$newheight);


            $newwidth1=25;

            $newheight1=($height/$width)*$newwidth1;

            $tmp1=imagecreatetruecolor($newwidth1,$newheight1);


            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

            imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);


            $filename = str_replace('//','/',$targetPath) . 'thumb_'.$fileName;

            $filename1 = str_replace('//','/',$targetPath) . 'small_'.$fileName;


            imagejpeg($tmp,$filename,100);

            imagejpeg($tmp1,$filename1,100);


            imagedestroy($src);

            imagedestroy($tmp);

            imagedestroy($tmp1);

            

            move_uploaded_file($tempFile,$targetFile);

            echo 1;

            Yii::app()->end();        

            }

            return false;    

          }

Thank You

Regards

Yii Fan

Try WideImage class (google it)… Its light weight and will work for your purpose, easy to use too.

Thank You rookie84 . I looked at WideImage , but still felt heavy and for us we just need thumbnails .

The code I mentioned above is just 20 lines of code , so thought I can avoid even WideImage too …

Thanks again

Regards

Yii Fan

Why not put it into an application components. Then you can change properties like were to store the files, max height/weight, jpeg quality etc. easily in config and you can reuse the class across projects if needed.

Thank You Y!! , I will do that . I wondered my code is OK to use or any pitfalls in that ( Other than design …)

Regards

Yii Fan