Hi

I've created a script which lets me upload 3 files, checks that they are image format, converts them into JPEG and then moves them into a folder on the server.

However, we are expecting our users to want to upload large image files (<1Mb) because they are photographs from digital cameras. The images will then be displayed on the site - 3 at a time - so I am concerned that sending 3Mb of images is going to be a performance problem.

What I'd like to do, is resize the images when they are uploaded. I'm not sure how to go about making a 1Mb image into a 30k image (ideal size), so I would appreciate some input on that.

What functions should I use? Should I move the uploaded file first, then resize? Should I resize the uploaded file then move it to the folder?

Thanks

    The main problem I am having is how to reference a file.......is it by filepath or is it like an object?

      if they are uploading 1MB files, then the files will be on the server before you can do anything about it. If you want to reduce them from that point on, then use one of the meny image resize function, or don;t allow big uploads.

        here is the code i use. It resized the image proprtionally with the maximum width and height given., hope it helps you:

        function thumbnail5($image_path,$thumb_path,$image_name,$thumb_width,$type)
        {
        $max_width = 100;
        $max_height = 75;

        if($type == 'gif'){
        	$src_img = imagecreatefromgif($image_path . $image_name);
        }elseif($type == 'png'){
        	$src_img = imagecreatefrompng($image_path . $image_name);
        }else{
        	$src_img = imagecreatefromjpeg($image_path . $image_name);
        }

        $size = getimagesize($image_path . $image_name); // Read the size
        $width = $size[0];
        $height = $size[1];

             // Proportionally resize the image to the
             // max sizes specified above
        
             $x_ratio = $max_width / $width;
             $y_ratio = $max_height / $height;
        
             if( ($width <= $max_width) && ($height <= $max_height) )
             {
                   $tn_width = $width;
                   $tn_height = $height;
             }
             elseif (($x_ratio * $height) < $max_height)
             {
                   $tn_height = ceil($x_ratio * $height);
                   $tn_width = $max_width;
             }
             else
             {
                   $tn_width = ceil($y_ratio * $width);
                   $tn_height = $max_height;
             }
        
        
        
        $dst_img = imagecreatetruecolor($tn_width,$tn_height);
        imagecopyresized($dst_img,$src_img,0,0,0,0,$tn_width,$tn_height,imagesx($src_img),imagesy($src_img));

        #imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
        if($type == 'gif'){
        imagegif($dst_img, $thumb_path .$image_name);
        }elseif($type == 'png'){
        imagepng($dst_img, $thumb_path .$image_name);
        }else{
        imagejpeg($dst_img, $thumb_path .$image_name);
        }
        return true;
        }

          // Proportionally resize the image to the
          // max sizes specified above

          Umm... beware... using this method all of your photos must be 4x3. If they are 3x4 or odd sizes (like digital cameras can produce) then it will squish them and distort them. Also, if the image is smaller than 100x75 then they will grow!

            So, how do we ensure that the image proportions stay true? I want my images to fit within a box which is 120px x 90px. To keep the proportions correct I would want either black background or transparent.

            Let's say I have an image on my server and it's path is http://www.mywebsite.com/images/image1.jpg

            The image is 800K and has dimensions of 1600px x 1000px

            I want it to be 120x90 and filesize <30K

            How would I go about that?

              proportional sizing means checking the dimensions first, then create a ratio var

              if (width * 0.75 >= height) { 
                 $ratio = 120 / width;
              } else { 
                 $ratio = 90 / height;
              }
              

              then math it out (to integers)

              $newwidth = floor($ratio * width);
              $newheight = floor($ratio * height);

              then it will never exceed 120 wide or 90 high.

                Write a Reply...