Advertisement

03.01.2007 at 01:37PM PST, ID: 22422680
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

Resizing images using php

Asked by rbosco020475 in PHP Scripting Language

I have a script that resizes photos submitted by users of my site that works very poorly. The script contains a class that resizes an uploaded image and saves it to a directory on the web server. It does what it is supposed to do but the photo quality is terrible and the ratio on the photos is not consistant. For design reasons I want the photos to all end up the same size (consistant height and width) so the page looks good. Any ideas on how I could adjust the script below to accomplish consistant sizes and better quality?

============== Current Code =============================
NOTE: A form posts to this script with a file field called "imgfile" as a $_POST variable:

class thumbnail
{
      var $img;

      function thumbnail($imgfile)
      {
            //detect image format
            $this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
            $this->img["format"]=strtoupper($this->img["format"]);
            if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
                  //JPEG
                  $this->img["format"]="JPEG";
                  $this->img["src"] = ImageCreateFromJPEG ($imgfile);
            } elseif ($this->img["format"]=="PNG") {
                  //PNG
                  $this->img["format"]="PNG";
                  $this->img["src"] = ImageCreateFromPNG ($imgfile);
            } elseif ($this->img["format"]=="GIF") {
                  //GIF
                  $this->img["format"]="GIF";
                  $this->img["src"] = ImageCreateFromGIF ($imgfile);
            } elseif ($this->img["format"]=="WBMP") {
                  //WBMP
                  $this->img["format"]="WBMP";
                  $this->img["src"] = ImageCreateFromWBMP ($imgfile);
            } else {
                  //DEFAULT
                  echo "Not Supported File";
                  exit();
            }
            @$this->img["lebar"] = imagesx($this->img["src"]);
            @$this->img["tinggi"] = imagesy($this->img["src"]);
            //default quality jpeg
            $this->img["quality"]=100;
      }

      function size_height($size=100)
      {
            //height
          $this->img["tinggi_thumb"]=$size;
          @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
      }

      function size_width($size=100)
      {
            //width
            $this->img["lebar_thumb"]=$size;
          @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
      }

      function size_auto($size=100)
      {
            //size
            if ($this->img["lebar"]>=$this->img["tinggi"]) {
                $this->img["lebar_thumb"]=$size;
                @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
            } else {
                $this->img["tinggi_thumb"]=$size;
                @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
             }
      }

      function jpeg_quality($quality=75)
      {
            //jpeg quality
            $this->img["quality"]=$quality;
      }

      function show()
      {
            //show thumb
            @Header("Content-Type: image/".$this->img["format"]);

            /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
            $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
                @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

            if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
                  //JPEG
                  imageJPEG($this->img["des"],"",$this->img["quality"]);
            } elseif ($this->img["format"]=="PNG") {
                  //PNG
                  imagePNG($this->img["des"]);
            } elseif ($this->img["format"]=="GIF") {
                  //GIF
                  imageGIF($this->img["des"]);
            } elseif ($this->img["format"]=="WBMP") {
                  //WBMP
                  imageWBMP($this->img["des"]);
            }
      }

      function save($save="")
      {
            //save thumb
            if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]);
            /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
            $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
                @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

            if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
                  //JPEG
                  imageJPEG($this->img["des"],"$save",$this->img["quality"]);
            } elseif ($this->img["format"]=="PNG") {
                  //PNG
                  imagePNG($this->img["des"],"$save");
            } elseif ($this->img["format"]=="GIF") {
                  //GIF
                  imageGIF($this->img["des"],"$save");
            } elseif ($this->img["format"]=="WBMP") {
                  //WBMP
                  imageWBMP($this->img["des"],"$save");
            }
      }
}

$tm1 = $_FILES['file1']['tmp_name'];
$tm2 = $_FILES['file2']['tmp_name'];
$tm3 = $_FILES['file3']['tmp_name'];

if($tm1 != ""){
move_uploaded_file("$tm1" , '/www/website.com/userpics/'.$recordid.'_'.'1'.'.'.'jpg');
$thumb=new thumbnail('/www/website.com/userpics/'.$recordid.'_'.'1'.'.'.'jpg');                  
$thumb->size_width(90);                        
$thumb->size_height(96);                        
$thumb->jpeg_quality(100);                        
$thumb->save('/www/website.com/outputpics/'.$recordid.'_'.'1'.'.'.'jpg');            
$thumb=new thumbnail('/www/website.com/userpics/'.$recordid.'_'.'1'.'.'.'jpg');                  
$thumb->size_width(178);                        
$thumb->size_height(192);                        
$thumb->jpeg_quality(100);                        
$thumb->save('/www/website.com/listviewpics/'.$adid.'_'.'1'.'.'.'jpg');
}

Any ideas are appreciated....Start Free Trial
[+][-]03.01.2007 at 01:50PM PST, ID: 18636213

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: PHP Scripting Language
Sign Up Now!
Solution Provided By: flasht
Participating Experts: 1
Solution Grade: A
 
 
[+][-]03.01.2007 at 01:52PM PST, ID: 18636232

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32