<?
/*
Class Name:Class.Image()
Version: 1.0
Author:Bogac Aslanyurek
Creation Date:04.11.2006
License: GPL
Email : b.aslanyurek@gmail.com

v1.601b 
===================
* Fixed a bug when image gets larger even if it is smaller than max width/height.

v1.601a
===================
* Added MaxWidth and MaxHeight parameters into constructor.

*/

class Image

{
    var $errmsg="";
    var $error = false;
    var $format    ;                         // file format
    var $file    ;
    var $width      = 0;
    var $height     = 0;
    var $percent    = 0;                       //resize percent
    var $thumb = "";                //instance created by GD
    var $filesize = 4000000;                   //maximum file size = 4 mb
    var $target="";                              //target save file
    var $MaxWidth = 400;            //max width of thumb
    var $MaxHeight = 400;            //max height of thumb
    var $Text ;                    //Text applied
    var $Font = "verdana.ttf";            //Font applied
    var $basename;                //filename w/out format  -->" image"
    var $filename;                //file name with format added -->"image.jpg","image.gif"
    var $path="images/userfiles/";        //default save path

    function Image($file,$MaxWidth=400,$MaxHeight=400) {

        if (!file_exists($file)) {
            $this->errmsg = _FileNotFound_;
            $this->error  = true;
        }
        if (!is_readable($file)) {
            $this->errmsg =_FileNotReadable_;
            $this->error  = true;
        }

        if (filesize($file)<= $this->filesize)
        {
            $this->filesize = filesize($file);
        }
        else{
            $this->errmsg = _FileSizeExceeded_;
            $this->error = true;
        }

        if ($this->error){
            echo $this->errmsg;
            exit();
        }

        //no errors, thumbnail source file is done.
        $this->file = $file;

        //getting file format
        if (strstr(strtolower($file), ".gif"))
        $this->format = "GIF";
        else if (strstr(strtolower($file), ".jpg") || strstr(strtolower($file), ".jpeg"))
        $this->format = "JPEG";
        else if (strstr(strtolower($file), ".png"))
        $this->format = "PNG";
        else {
            $this->errmsg = "Unknown file format";
            $this->error  = true;
        }


        //image create
        switch ($this->format) {
            case "GIF":
                $ResImg = ImageCreateFromGif($this->file);
                break;
            case "JPEG":
                $ResImg = ImageCreateFromJpeg($this->file);
                break;
            case "PNG":
                $ResImg = ImageCreateFromPng($this->file);
                break;
        }

        //percent calculation
        $this->MaxWidth = $MaxWidth;
        $this->MaxHeight = $MaxHeight;

        $width = imagesx($ResImg);
        $height = imagesy($ResImg);
        
        //if image is smaller but not larger
        if($width<=$MaxWidth || $height<=$MaxHeight){
            
            if ($width<=$MaxWidth) {
                $this->width = $width;
            }
            
            if($height<=$MaxHeight){
                $this->height = $height;
            }
        }

        //if image is larger than maxwidth/maxheight
        else {

            if ($width >= $height){
                //Is image horizontal?
                
                $this->width = $this->MaxWidth;
                $this->percent= $this->MaxWidth/$width;
                $this->height = $this->percent * $height;

            }else if ($height>=$width)  {

                //Is image vertical?
                
                $this->height = $this->MaxHeight;
                $this->percent = $this->MaxHeight/$height;
                $this->width = $width*$this->percent;
            }
        }
        
        #
        # Good idea from Mariano Cano P�rez
        # Requires GD 2.0.1 (PHP >= 4.0.6)
        #

        if (function_exists("ImageCreateTrueColor"))
        $this->thumb = ImageCreateTrueColor($this->width, $this->height);
        else
        $this->thumb = ImageCreate($this->width, $this->height) ;
        ImageCopyResampled($this->thumb, $ResImg, 0, 0, 0, 0, $this->width, $this->height, $width, $height);

        $this->basename = randomCode(8);
    }

    function SaveThumbnail ($jpegQuality="80") {

        //get the format and save the file to disk.
        //this->path can be changed in case you need to save somewhere else than "images/userfiles/"


        switch ($this->format)
        {
            case "GIF":
                $this->filename=$this->basename.".gif";
                $this->target =$this->path.$this->filename;
                imagegif($this->thumb,$this->target);
                break;

            case "JPEG":
                $this->filename=$this->basename.".jpg";
                $this->target =$this->path.$this->filename;
                imagejpeg($this->thumb,$this->target,$jpegQuality);
                break;

            case "PNG":
                $this->filename=$this->basename.".png";
                $this->target =$this->path.$this->filename;
                imagepng($this->thumb,$this->target);
                break;
        }
        imagedestroy($this->thumb);
    }

    function ImageText()
    {
        //describe colors and add some Text
        $white = imagecolorallocate($this->thumb, 255, 255, 255);
        $startx = 0;
        $starty = $this->height-10;
        imagettfText($this->thumb,16,0,$startx,$starty,$white,$this->Font,$this->Text);
    }

    function Info()
    {
        // get thumbnail info if needed.
        echo "Format: ".$this->format."<BR>";
        echo "Thumbnail width: ".$this->width."<br>";
        echo "Thumbnail height: ".$this->height."<br>";
        echo "Thumbnail source file: ".$this->file."<br>";
        echo "Filesize: ".($this->filesize/1000)." KB<br>";
        echo "Thumbnail saved in: ".$this->target."<br>";
    }

}

?>
Bi yerde işe yeni girdim. patron başka bir kişiye script yazdırmış. scripti yazan kişi de yukarıda vermiş olduğum sınıfı kullanmış. yükleme yaparken hata olutuğunu söyledi. Hatayı şu şekilde düzelttim.
if($width<=$MaxWidth || $height<=$MaxHeight){
            
            if ($width<=$MaxWidth) {
                $this->width = $width;
            }
            
            if($height<=$MaxHeight){
                $this->height = $height;
            }
        }
burada görmüş olduğunu gibi resmin yüksekliği ve genişliği maksimum yükseklikten, genişlikten büyükse ve eşitse atamayı gerçekleştiriyor. değilse hiç bir işlem yapmıyor resim kopyalama ve düzeltme fonksiyonları da sıfır pixel olduğu için hata vermesine neden oluyor.
    var $width      = 0;
    var $height     = 0;
27 ve 28. satırlardaki değişkenlere resmin boyutunun 400 olmasını istediğim için 400ü atayarak sorunu giderdim