Jump to content

Simple Image Resize


bobthebullet990

Recommended Posts

Hi!!! Thanks for your time!!! ...I have created a php script that allows users to upload images to my webserver and then displays all images within the gallery! ...The thumbnails are resized proportionally, with a max size of 150x150px, however, i want to resize the main image if its larger than 500px to proportionally resize it to a size within 500x500px square! ...Is there no simple way to do this other than the way that i have done....

[code]
        // CLEAR THE FILENAME - IN CASE OF MULTIPLE UPLOADS, DON'T WANT AN ERROR!
        unset($thefilename);
        // CHECK THAT THERE IS A FILE TO UPLOAD
        if(!isset($_FILES) && isset($HTTP_POST_FILES)){
          $_FILES = $HTTP_POST_FILES;
        }
        if(!isset($_FILES['fileToUpload'])){
          $message .= "ERROR: The image entered was not found!<br>";
        }
        $thefilename = basename($_FILES['fileToUpload']['name']);
        if(empty($thefilename)){
          $message = "ERROR: The name of the file was not found.";
        }
        if(empty($error)) {
          $imagePath = $uploadDir . $thefilename;
          $result = @move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $imagePath);
 
          if(empty($result))
            $message .= "ERROR: could not save the file!<br>";
         
          // CREATE IMAGE THUMBNAIL
          $thumb = $thefilename;
         
          $handle = @imagecreatefromjpeg($imagePath);
         
          $x=imagesx($handle);
          $y=imagesy($handle);
         
          if($x > $y){                               
            $max = $x;                         
            $min = $y;                         
          }                                         
          if($x <= $y){                               
            $max = $y;                         
            $min = $x;                         
          }                                       
       
          // $size_in_pixel : The maximum value (in pixels) that the thumbnail width or height is allowed.
          $size_in_pixel = '150';
         
          // CREATE WIDTH AND HEIGHT VALUES FOR THE THUMBNAIL - PROPORTIONAL TO ORIGINAL WIDTH AND HEIGHT RATIO.
          $rate = $max/$size_in_pixel;
          $final_x = $x/$rate;
          $final_y = $y/$rate;
             
          if($final_x > $x) {
            $final_x = $x;
            $final_y = $y;
          }
           
          $final_x = ceil($final_x);
          $final_y = ceil($final_y);
         
          $black_picture = imageCreatetruecolor($final_x,$final_y);
          imagefill($black_picture,0,0,imagecolorallocate($black_picture, 255, 255, 255));
          imagecopyresampled($black_picture, $handle, 0, 0, 0, 0,$final_x, $final_y, $x, $y);
         
          $thumbPath = $thumbDir.$thumb;
         
          if(!@imagejpeg($black_picture, $thumbPath, $size_in_pixel))
            imagestring($black_picture, 1, $final_x-4, $final_y-8, ".", imagecolorallocate($black_picture,0,0,0));
                     
          // DELETE ALL TEMPORARY IMAGE DATA
          imagedestroy($handle);
          imagedestroy($black_picture);
        }
[/code]
Link to comment
Share on other sites

I want to image resize and save the resized image, not the original size image! I don't want to have 500kb images [1280x1024 and larger] clogging up my server!!!!!!!!! and then resize them in another script just to view it!!! ...to me, that just seems a waste of processing time and disc space! why resize an image every time when you can resize and save it at the resized size!!!

...Check out my test version of the page; running on WAMP direct fom my PC! ...hopefully you will understand where i am comming from!!!

http://82-32-189-3.cable.ubr07.azte.blueyonder.co.uk/WWW/gallery.php
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.