Jump to content

Archived

This topic is now archived and is closed to further replies.

bobthebullet990

Simple Image Resize

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]

Share this post


Link to post
Share on other sites
see my post a few minutes ago labeled "image resize function an implementation" this show an easy function for resizing on the fly.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.