How to read the first bytes of png,jpg and jpeg and get the height/width

Discussion in 'PHP' started by free-designer, Jul 12, 2010.

  1. #1
    Hey guys,
    I was looking for a way to get the image height and width mush faster then using getimagesize();
    so i thought that getting the first bytes of an image would be faster to get the height and width.
    can anyone help me with the code that should do that for png,jpg and jpeg?

    thanks so mush
     
    free-designer, Jul 12, 2010 IP
  2. Johnta

    Johnta Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Php fread function will read any number of Bytes you want
     
    Johnta, Jul 12, 2010 IP
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    i dont think it will get the width and hieght of the image as op wants
     
    Bohra, Jul 12, 2010 IP
  4. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Anyone, correct me if I'm wrong, but I don't think that's possible. Besides, you're making your life harder with that.

    getimagesize( ) was created for that purpose. Just do a:
    list($current_width, $current_height) = getimagesize($filename);
    PHP:
    And you're done. Stop making your life complicated.
     
    Rainulf, Jul 12, 2010 IP
  5. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    @Rainulf: I know that, the problem is that this function is so slow that why i hate it. anyway im going to change my plans with my script cuz i can't find what i need thanks so mush
     
    free-designer, Jul 12, 2010 IP
  6. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #6
    Just how is it slow? The dimensions of an image are already in the image itself, all the function does is grab it.
     
    Rainulf, Jul 12, 2010 IP
  7. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No, getimagesize() downloads all of the picture first from an remote url, that why it's slow it has to download it and then get the image height and width
     
    free-designer, Jul 12, 2010 IP
  8. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #8
    Ahh I see, so that's your issue. There's imagesx( ) and imagesy( ), I haven't tried those two myself, but if I have to guess, they probably do the same as getimagesize( ). But figuring out with bytes alone wouldn't give us the image's height and width, at least that's what I think - I could be wrong. So here's my personal approach (i haven't tried it myself). The issue is - downloading the image to get its height and width is unavoidable. So why not just download a portion of it(use the imagecopy( ) function)? Say a pixel of width and a pixel of height. Then use those two functions I mentioned, imagesx( ) and imagesy( ). Theoroetically, it should decrease the downloading. :)
     
    Rainulf, Jul 12, 2010 IP
  9. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    @Rainulf: Well, i guess you made a good point i'll check it out by my self, and if i came back with any results i'll post a comment back.
    Thanks Rainulf
     
    free-designer, Jul 12, 2010 IP
  10. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #10
    Awesome! I'm eager to see the results. :)
     
    Rainulf, Jul 12, 2010 IP
  11. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
  12. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #12
    Rainulf, Jul 12, 2010 IP
  13. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #13
    No i didnt ... but why isnt it possible with curl instead of using the fucntion of php which downloads the whole image its better to use curl and transfer it to the server and then do the checking this would be faster
     
    Bohra, Jul 12, 2010 IP