Results 1 to 7 of 7

Thread: How to find out a MIMETYPE of an image?

  1. #1
    Join Date
    Jul 2011
    Location
    Pune, India
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to find out a MIMETYPE of an image?


    Hi everyone,
    My application needs to open an image file to be set as a background of a qgraphicsview.
    When I select the file from qfiledialog, I need to find out if that file has a particular MIMETYPE.
    Is there any way to do that??
    Need all the help I can get.
    Thnx.

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to find out a MIMETYPE of an image?

    Why?
    MIMETYPE is needed during copy paste.

    Qt Code:
    1. view.setBackgroundBrush(QBrush(QImage("fileName.png")))
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2011
    Location
    Pune, India
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out a MIMETYPE of an image?

    Quote Originally Posted by MarekR22 View Post
    Why?
    MIMETYPE is needed during copy paste.

    Qt Code:
    1. view.setBackgroundBrush(QBrush(QImage("fileName.png")))
    To copy to clipboard, switch view to plain text mode 

    Let me ask the question in another context.
    Any idea about how can i find out if particular image is .svg (Pure) or an .SVG converted from .bmp??
    This will answer my previous question automatically.
    Thnx again.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find out a MIMETYPE of an image?

    Quote Originally Posted by ommharidaas View Post
    Let me ask the question in another context.
    Any idea about how can i find out if particular image is .svg (Pure) or an .SVG converted from .bmp??
    The mime type of the file would be very unlikely to tell these two apart anyway: both would be "image/svg+xml".

    You could tell if an SVG file contains embedded raster images by parsing the file looking for the <image> element. If the xlink:href attribute points at something other than another SVG file, or contains a base64 data block then you have an embedded raster. There is a small possibility that a base64 data block is not a raster image and the only way to detect that would be to decode the data and check its type.
    Last edited by ChrisW67; 15th December 2011 at 08:50. Reason: spelling corrections

  5. The following user says thank you to ChrisW67 for this useful post:

    ommharidaas (15th December 2011)

  6. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to find out a MIMETYPE of an image?

    SVG is a xml file.
    I don't know how exactly converter makes SVG from BMP, but in first step I would try to see content of this xml and figue out how is it done.
    Then I would check if bool QSvgRenderer::elementExists ( const QString & id ) const can be used for detection if it is result of conversion.
    In last step I would parse xml.

  7. The following user says thank you to MarekR22 for this useful post:

    ommharidaas (15th December 2011)

  8. #6
    Join Date
    Jul 2011
    Location
    Pune, India
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out a MIMETYPE of an image?

    Quote Originally Posted by ChrisW67 View Post
    The mime type of the file would be very unlikely to tell these two apart anyway: both would be "image/svg+xml".

    You could tell if an SVG file contains embedded raster images by parsing the file looking for the <image> element. If the xlink:href attribute points at something other than another SVG file, or contains a base64 data block then you have an embedded raster. There is a small possibility that a base64 data block is not a raster image and the only way to detect that would be to decode the data and check its type.

    Thnx ChrisW67,
    Even i thought the same, both of them will have type "image/svg+xml".
    Do u think it is wise to actually go ahead and try to decode that base64 data block???
    If yes, how should i go about it??
    If no, is there a way around it??


    Quote Originally Posted by ChrisW67 View Post
    The mime type of the file would be very unlikely to tell these two apart anyway: both would be "image/svg+xml".

    You could tell if an SVG file contains embedded raster images by parsing the file looking for the <image> element. If the xlink:href attribute points at something other than another SVG file, or contains a base64 data block then you have an embedded raster. There is a small possibility that a base64 data block is not a raster image and the only way to detect that would be to decode the data and check its type.

    Thnx ChrisW67,
    Even i thought the same, both of them will have type "image/svg+xml".
    Do u think it is wise to actually go ahead and try to decode that base64 data block???
    If yes, how should i go about it??
    If no, is there a way around it??



    Added after 6 minutes:


    Quote Originally Posted by MarekR22 View Post
    SVG is a xml file.
    I don't know how exactly converter makes SVG from BMP, but in first step I would try to see content of this xml and figue out how is it done.
    Then I would check if bool QSvgRenderer::elementExists ( const QString & id ) const can be used for detection if it is result of conversion.
    In last step I would parse xml.


    Thnx MarekR22,
    Will check with bool QSvgRenderer::elementExists ( const QString & id ) const and see if it works.

    Last edited by ommharidaas; 15th December 2011 at 11:20.

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find out a MIMETYPE of an image?

    Quote Originally Posted by ommharidaas View Post
    Do u think it is wise to actually go ahead and try to decode that base64 data block???
    If yes, how should i go about it??
    If no, is there a way around it??
    Wise? Yes, if you want to be absolutely certain. However, the chance of someone embedding another SVG using <image> and base64 encoding, rather than either directly inserting the SVG elements or referring to a separate file, seems very low. Only you know your requirements and likely inputs well enough to know whether the probability and effect of a false detection is acceptable or not. If your requirement is simply to detect the use of embedded images then there's no need to look further than an <image> tag.

    You can use QByteArray::fromBase64() once you have extracted the base 64 data from the attribute. If the result starts with "<?xml" (possibly preceded by a byte order mark) then you have an SVG, otherwise a PNG, JPG or other bitmap format.

  10. The following user says thank you to ChrisW67 for this useful post:

    ommharidaas (16th December 2011)

Similar Threads

  1. Identify mimetype
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 7th March 2011, 06:16
  2. Cannot add new mimetype for a custom Qt Creator Plugin
    By yagabey in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2011, 12:52
  3. Replies: 5
    Last Post: 23rd January 2011, 11:27
  4. Replies: 6
    Last Post: 21st September 2009, 10:55
  5. Replies: 3
    Last Post: 14th March 2007, 08:09

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
  •  
Qt is a trademark of The Qt Company.