Page 1 of 1

display image in a particular width and height

Posted: Fri Jan 02, 2009 7:09 am
by anujphp
Hello
i am displaying images from a paticular folder,the code is working fine but i want to display all the image in same width and height...the code for displaying images is givn below

Code: Select all

<?php 
// define absolute path to image folder
 $image_folder = 'IMAGE FOLDER NAME'; 
 // get the image name from the query string
  
   if (isset($_GET['pic']) && basename($_GET['pic']) == $_GET['pic'])
    { 
    $pic = $image_folder.$_GET['pic']; 
    if (file_exists($pic) && is_readable($pic))
     { 
   // get the filename extension
    $ext = substr($pic, -3); 
    // set the MIME type
     switch ($ext)
      { 
       case 'jpg': $mime = 'image/jpeg';
       break; 
       case 'gif': $mime = 'image/gif';
        break; 
        case 'png': $mime = 'image/png'; 
        break; 
        default: $mime = false; 
     } 
     // if a valid MIME type exists, display the image
      // by sending appropriate headers and streaming the file 
      if ($mime)
       { 
        header('Content-type: '.$mime);
        header('Content-length: '.filesize($pic));
         $file = @ fopen($pic, 'rb');
          if ($file)
           { 
           fpassthru($file); exit; 
           } 
        } 
    } 
}
 
 ?>
I am calling the php pages as below

Code: Select all

FILENAME.php?pic=$imgname
THANK YOU IN ADVANCE....

Re: display image in a particular width and height

Posted: Fri Jan 02, 2009 7:28 am
by Reviresco
Just set the width and height you want in the <img /> tags. Since it's always the same, there's no need to do it dynamically.

Re: display image in a particular width and height

Posted: Fri Jan 02, 2009 10:40 am
by msurabbott
This is often done using the GD Library because it can actually resize the image on the fly - but this should only be done if the size you want the image to be and the actual size are quite different. Simply using the height="" and width="" attributes in the img tag will distort the image if the aspect ratio is different on your desired, and actual sizes.

Granted not all servers have the GD Library, and it is slower than just setting attributes, but the resulting image will look much better and you can even store the resized image, and next time you look to pull it up, use the already resized image rather than resizing it again.

Re: display image in a particular width and height

Posted: Sun Jan 04, 2009 3:55 am
by anujphp
my problem is that i cant make a copy of all the image i receive from the user,it will take up lot of space which will be a wastage of space,and i am not using any image tag to show the image just i am calling the php files and the image is shown there.thats why i cant use the height and width property

Re: display image in a particular width and height

Posted: Sun Jan 04, 2009 11:31 am
by msurabbott
Ok, well then dont store it, that was just an option. Do it on the fly every time...

Re: display image in a particular width and height

Posted: Sun Jan 11, 2009 5:36 am
by anujphp
Thank you everybody,
the issue has been solved, omniuni has sent me a code which works perfactly the way i was loking for.
once again thanks you all