Results 1 to 4 of 4

Thread: rieving image types/mime types

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default rieving image types/mime types

    Hi, I'd like to retireve the file [mime] type (such as image/jpeg, etc) from an image that already exists on my file system. I know how to do this already in an image upload, but i've searched for a way to retrieve mime type of existing image -- no luck.

    Any help is much appreciated.
    - Josh

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,440
    Thanks
    106
    Thanked 120 Times in 118 Posts

    Default

    If you know the filename this will pull up some info about the file.
    PHP Code:
    <pre><?php
    $image 
    $_SERVER['DOCUMENT_ROOT'].'/images/Lain.JPG';
    $imageinfo GetimageSize($image);
    print_r($imageinfo);
    ?></pre>
    returns
    PHP Code:
    Array
    (
        [
    0] => 1024
        
    [1] => 768
        
    [2] => 2
        
    [3] => width="1024" height="768"
        
    [bits] => 8
        
    [channels] => 3
        
    [mime] => image/jpeg

    PHP Code:
    [0] = width
    [1] = height
    [2] = indicates the type of image (dunno what that means)
    [
    3] = width and height together
    [bits] = number of bits per color
    [channels] = channels per pixel (don't know what this means either)
    [mime] = jpg, gif, etc. 
    If you want to list the images in a given folder use this:
    PHP Code:
    <?php
    $imgdir 
    'images'// the directory, where your images are stored
    $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
    $dimg opendir($imgdir);
    while(
    $imgfile readdir($dimg))
    {
    if(
    in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
      
    $a_img[] = $imgfile;
      
    sort($a_img);
      
    reset ($a_img);

    }
    $totimg count($a_img); // total image number
    for($x=0$x $totimg$x++)
    {
    $size getimagesize($imgdir.'/'.$a_img[$x]);
    // do whatever
    $halfwidth ceil($size[0]/2);
    $halfheight ceil($size[1]/2);
    echo 
    'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />';
    }
    ?>
    techietim, could you give an example of how fileinfo works? I could not get their examples to work.
    Last edited by james438; 08-26-2008 at 07:52 AM.

  4. #4
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    As long as you know you will only be working with images, you can use getimagesize() like james438 posted.

    If you need to find the mime type of anything else, you should use the Fileinfo extension, like techietem posted. Do not use mime_content_type().

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •