SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stupid newbie question about getimagezise

    Thank you for caring to have a look at my question
    am trying to do some image resizing,went thru some articles and visit php.net
    I always came across codes like this:

    $image = "image-dir/image.jpg";
    $size = getimagesize("$image");

    the thing is i dont have a file called image.jpg,i guess i have filename or even worse some renamed file(say $newfile)

    A)
    Is it a general thing that a php script takes care of as a default or i have to put my file name?
    B)
    At which point getimagesize starts to work,before or after a file is renamed?
    C)
    Can a thumbernail script still access a renamed file that has had its extension renamed as well? like mypic258888.jpg855555555 ?

    I'd be very happy to have these answred to me,it'll take lots of confusion

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    getimagesize() will look for the image based on the string you give it. So, if you have a file called index.php which calls getimagesize('image.jpg') it will look for an image with the name 'image.jpg' in the same directory as index.php. If you rename the image before calling getimagesize(), naturally you will need to supply the new name.

    If you want to do this to an image with another name, simply call it with that name:
    PHP Code:
    $newfile 'mypic325.jpg';
    $size getimagesize($newfile); 
    The function will work if the extension is not .jpg (since it will look at the mime-type, not the file extension) but this is probably not a good idea nor of any practical use.

  3. #3
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to clarify upon a very common misconception, a file's mime-type is not some magical property, it is merely a different way of expressing a file's extension. In almost all cases, a file's mime-type is determined solely upon the file's extension.

    Images are one of the exceptions to this: images contain "magic constants" (they have another more common name, but I can't recall it at the moment) that can define what kind of image they are. It is these "magic constants" that getimagesize looks at, and thus it works just was well for a JPEG that has the extension .jpg and one that has the extension .foo - I have tested and confirmed this behavior. This is what makes getimagesize indispensable for e.g. validation of uploaded files (i.e. identify them as images).

    "Magic constants" do not exist for text files, for Microsoft .doc (or other MS Office filetypes), nor for almost any file. They do exist for some types of executables, but not all. For files that do not have "magic constants", the file extension is literally the only way PHP (or really anything else) has any chance of figuring out what the file is.
    PHP questions? RTFM
    MySQL questions? RTFM

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Cheers kromey, I've tested getimagesize on known jpegs with random extensions too, but I didn't know images were special in this respect.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey View Post
    "Magic constants" do not exist for text files, for Microsoft .doc (or other MS Office filetypes), nor for almost any file.
    Actually, most filetypes do use signatures. It might be interesting for you to check your system's "magic.mime" file.

  6. #6
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all guys for your contributions.It was indeed interesting and took most of my confusions.

    Just one last more question
    How does the code for resizing upon upload differ from dynamically resizing upon browser rendering?
    Thank you in advance !
    Last edited by Tikila; Jun 25, 2007 at 22:41.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Resampling is always a trade-off between speed and quality. Browsers use fastest and worst algorithm, graphic packages - far better, but slower ones.

  8. #8
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    Actually, most filetypes do use signatures. It might be interesting for you to check your system's "magic.mime" file.
    Interesting, I didn't know that many files in fact had "magic constants." You learn something new every day!
    PHP questions? RTFM
    MySQL questions? RTFM


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
  •