public string GetImageResolution(Sitecore.Data.Items.MediaItem mi) { // check item before we begin if (mi != null) { // inject the stream from the media item to an image (needed to get exif reader running) System.Drawing.Image img = System.Drawing.Image.FromStream(mi.GetMediaStream()); if (img!=null) { // init a new exif reader Sitecore.Drawing.Exif.Reader ExifReader = new Sitecore.Drawing.Exif.Reader(img); if (ExifReader != null) { // allocate the different types of properties we're gonna be using Sitecore.Drawing.Exif.Properties.Property prop; Sitecore.Drawing.Exif.Properties.UInt16Property u16prop; Sitecore.Drawing.Exif.Properties.RationalProperty rprop; // start fetching properties.. prop = ExifReader.GetProperty((int)Sitecore.Drawing.Exif.Properties.Tag.ResolutionUnit); if (prop != null) { // first is a UInt16 property that we'll step through and see what type it is.. u16prop = (Sitecore.Drawing.Exif.Properties.UInt16Property)prop; if (u16prop!=null) { // get the resolution unit string resUnit = "dpi"; switch (u16prop[0]) { case 1: resUnit = "n/a"; break; case 2: resUnit = "dpi"; break; case 3: resUnit = "dpcm"; break; } // now go fetch the two remaining properties (they're both rational properties) // horizontal resolution string horzRes = "1/72"; prop = ExifReader.GetProperty((int)Sitecore.Drawing.Exif.Properties.Tag.XResolution); if (prop != null) { // cast it to rational property rprop = (Sitecore.Drawing.Exif.Properties.RationalProperty)prop; if (rprop != null) { if (rprop.Values[0].ToString() != "") { horzRes = rprop.Values[0].ToString(); // try to calculate it appropriately.. (sucks, but we gotta do it unless we want mumbo jumbo text to appear..).. string[] hstrs = rprop.Values[0].ToString().Split('/'); if (hstrs.Length>0) { // format the string horzRes = string.Format("{0}", (double.Parse(hstrs[0].Trim()) / double.Parse(hstrs[1].Trim()))); } } } } // repeat for vertical string vertRes = "1/72"; prop = ExifReader.GetProperty((int)Sitecore.Drawing.Exif.Properties.Tag.YResolution); if (prop != null) { // cast it to rational property rprop = (Sitecore.Drawing.Exif.Properties.RationalProperty)prop; if (rprop != null) { if (rprop.Values[0].ToString() != "") { vertRes = rprop.Values[0].ToString(); // try to calculate it appropriately.. (sucks, but we gotta do it unless we want mumbo jumbo text to appear..).. string[] vstrs = rprop.Values[0].ToString().Split('/'); if (vstrs.Length > 0) { // format the string vertRes = string.Format("{0}", (double.Parse(vstrs[0].Trim()) / double.Parse(vstrs[1].Trim()))); } } } } // finally format the output and return it return string.Format("{0} {1} x {2} {3}", horzRes, resUnit, vertRes, resUnit); } } } } } return ""; }The resulting output will give you a string along the lines of '300 dpi x 300 dpi' Regards, P.
Monday, May 21, 2007
EXIF data from MediaItem
Reading EXIF data from sitecore media items is probably gonna be -a lot- easier in the future, but as far as i can tell there's no easy way of doing it so far..
Hopefully there's a better (and easier) way of getting EXIF information, but i didn't have time to find it, and this works, so it'll do for now :)
The following code will return the resolution specific EXIF information of a passed in MediaItem:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment