// ---------------------------------------------------------------------- /// <summary> /// Gets the thumbnail image file path and creates the /// thumbnail if it does not already exist. /// </summary> /// <param name="bigFile">The path of the original file (from the img tag)</param> /// <param name="width">The original image width</param> /// <param name="height">The original image height</param> /// <returns>The path of the thumbnail</returns> private static string GetSmallFile(string bigFile, int width, int height) { SmallImageUri imageUri = new SmallImageUri(bigFile, _SmallFileNameAddon, _BlogImageFolder); if (!imageUri.FoundSmallFile) { return(null); } if (!File.Exists(imageUri.SmallFileHostPath)) // See if the small file already exists { try { // Create small file using (FileStream bigFileStream = new FileStream(imageUri.FileHostPath, FileMode.Open)) // Open the big file { using (FileStream smallFileStream = new FileStream(imageUri.SmallFileHostPath, FileMode.Create)) // Create and open the small file for writing { ResizeImage(bigFileStream, smallFileStream, width, height); } } } catch (FileNotFoundException err) { return("\0" + ShowError(err, "FileNotFound:GetSmallFile:" + imageUri.FileHostPath)); } catch (DirectoryNotFoundException err) { return("\0" + ShowError(err, "DirectoryNotFound:GetSmallFile:" + imageUri.FileHostPath)); } catch (PathTooLongException err) { return("\0" + ShowError(err, "PathToLong:GetSmallFile:" + imageUri.FileHostPath + ", " + imageUri.SmallFileHostPath)); } } return(imageUri.SmallFileName); }
// ---------------------------------------------------------------------- /// <summary> /// Gets the thumbnail image file path and creates the /// thumbnail if it does not already exist. /// </summary> /// <param name="bigFile">The path of the original file (from the img tag)</param> /// <param name="width">The original image width</param> /// <param name="height">The original image height</param> /// <returns>The path of the thumbnail</returns> private static ImageInfo GetSmallFile(string bigFile) { SmallImageUri imageUri = new SmallImageUri(bigFile, _SmallFileNameAddon, _BlogImageFolder); if (!imageUri.FoundSmallFile) { return(null); } ImageInfo imageInfo = null; if (!File.Exists(imageUri.SmallFileHostPath)) // See if the small file already exists { try { using (FileStream imageStream = new FileStream(imageUri.FileHostPath, FileMode.Open, FileAccess.Read, FileShare.Read)) // Open the big file { using (Image image = Image.FromStream(imageStream)) { imageInfo = GetDisplaySize(image.Width, image.Height); } } } catch (DirectoryNotFoundException err) { return(new ImageInfo() { File = "\0" + ShowError(err, "DirectoryNotFound: " + imageUri.FileHostPath) }); } catch (FileNotFoundException err) { return(new ImageInfo() { File = "\0" + ShowError(err, "FileNotFound: " + imageUri.FileHostPath) }); } catch (PathTooLongException err) { return(new ImageInfo() { File = "\0" + ShowError(err, "PathTooLong: " + imageUri.FileHostPath) }); } catch (IOException err) { return(new ImageInfo() { File = "\0" + ShowError(err, "IOException: " + imageUri.FileHostPath) }); } catch (Exception ex) { return(null); } try { // Create small file using (FileStream bigFileStream = new FileStream(imageUri.FileHostPath, FileMode.Open)) // Open the big file { using (FileStream smallFileStream = new FileStream(imageUri.SmallFileHostPath, FileMode.Create)) // Create and open the small file for writing { ResizeImage(bigFileStream, smallFileStream, imageInfo.GetWidth(), imageInfo.GetHeight()); } } } catch (FileNotFoundException err) { return(new ImageInfo() { File = "\0" + ShowError(err, "FileNotFound:GetSmallFile:" + imageUri.FileHostPath) }); } catch (DirectoryNotFoundException err) { return(new ImageInfo() { File = "\0" + ShowError(err, "DirectoryNotFound:GetSmallFile:" + imageUri.FileHostPath) }); } catch (PathTooLongException err) { return(new ImageInfo() { File = "\0" + ShowError(err, "PathToLong:GetSmallFile:" + imageUri.FileHostPath + ", " + imageUri.SmallFileHostPath) }); } } else { imageInfo = new ImageInfo(); } imageInfo.File = imageUri.SmallFileName; return(imageInfo); }
// ---------------------------------------------------------------------- /// <summary> /// Gets the image to use for a post list item /// </summary> /// <param name="postId">The Id of the post</param> /// <param name="style">Inline css to add</param> /// <returns>Html string for the image.</returns> public static string GetPostImage(Guid postId, string style) { if (!ExtensionManager.ExtensionEnabled(_ExtensionName)) { return(String.Empty); } Post post = Post.GetPost(postId); if (post != null) { int tagIndex = 0; List <BlogParser.HtmlTag> imgTags = BlogParser.HtmlDom.GetTagByTagName( BlogParser.ParseForDoms(post.Content), "img"); // See if a different image has been specified to be used for (int i = 0; i < imgTags.Count; i++) { if (String.Compare(imgTags[i]["postlist"].Value, "true", true) == 0) { tagIndex = i; break; } } if (imgTags.Count > 0) // Images Found { // Add or replace CssClass specified in settings if (!String.IsNullOrEmpty(_ImageCssClass)) { imgTags[tagIndex].Insert(0, new BlogParser.HtmlAttribute("class", _ImageCssClass)); } // Get filename for the new image to be created SmallImageUri imageUri = new SmallImageUri(imgTags[tagIndex]["src"].Value, _SmallFileNameAddon, _BlogImageFolder); if (!imageUri.FoundSmallFile) { return(String.Empty); } ImageSize newSize; // Create Post list image if it does not exist if (!_ForceMaxHeight || !_ForceMaxWidth) { // Get display size try { using (FileStream imageStream = new FileStream(imageUri.FileHostPath, FileMode.Open, FileAccess.Read, FileShare.Read)) // Open the big file { using (Image image = Image.FromStream(imageStream)) { newSize = GetDisplaySize(image.Width, image.Height); } } } catch (DirectoryNotFoundException err) { return(ShowError(err, "DirectoryNotFound: " + imageUri.FileHostPath)); } catch (FileNotFoundException err) { return(ShowError(err, "FileNotFound: " + imageUri.FileHostPath)); } catch (PathTooLongException err) { return(ShowError(err, "PathTooLong: " + imageUri.FileHostPath)); } catch (IOException err) { return(ShowError(err, "IOException: " + imageUri.FileHostPath)); } } else // No need to get a display size { newSize = new ImageSize(_MaxWidth, _MaxHeight, "px"); } // Create Image to display string smallFile = GetSmallFile(imgTags[tagIndex]["src"].Value, Convert.ToInt32(newSize.Width.Replace(_Units, String.Empty)), Convert.ToInt32(newSize.Height.Replace(_Units, String.Empty))); if (Security.IsAuthenticated && !String.IsNullOrEmpty(smallFile) && smallFile[0] == '\0') // See if error from GetSmallFile and send back { string r = smallFile.Replace("\0", String.Empty); return("\">" + r); } imgTags[tagIndex]["src"].Value = smallFile; // Add root if there is none if (String.IsNullOrEmpty(Path.GetDirectoryName(imgTags[tagIndex]["src"].Value))) { imgTags[tagIndex]["src"].Value = Blog.CurrentInstance.AbsoluteWebRoot + imgTags[tagIndex]["src"].Value; } // Prevent the browser from caching so that changes made by an authenticated user will be displayed if (Security.IsAuthenticated) { if (imgTags[tagIndex]["src"].Value.Contains("?")) { imgTags[tagIndex]["src"].Value += "&nocache=" + Guid.NewGuid().ToString().Replace("-", String.Empty); } else { imgTags[tagIndex]["src"].Value += "?nocache=" + Guid.NewGuid().ToString().Replace("-", String.Empty); } } if (_ForceMaxWidth && _ForceMaxHeight) { if (newSize.GetWidth() > _MaxWidth) { newSize.Width = _MaxWidth + "px"; } if (newSize.GetHeight() > _MaxHeight) { newSize.Height = _MaxHeight + "px"; } } imgTags[tagIndex]["width"].Value = newSize.Width; imgTags[tagIndex]["height"].Value = newSize.Height; imgTags[tagIndex]["alt"].Value = post.Title; imgTags[tagIndex]["title"].Value = post.Title; BlogParser.HtmlStyleAttribute styleAttr = new BlogParser.HtmlStyleAttribute(); styleAttr.AddRange(style); // Add single instance specified style styleAttr.AddRange(_ImageInlineStyle); // Add global specified style styleAttr.Add("width", newSize.Width, false); styleAttr.Add("height", newSize.Height, false); imgTags[tagIndex]["style"].Value = styleAttr.Value; BlogParser.HtmlTag alink = new BlogParser.HtmlTag("a", new BlogParser.HtmlAttribute("href", HttpUtility.UrlDecode(post.AbsoluteLink.AbsolutePath)) ); alink.InnerHtml.Add(imgTags[tagIndex].Clone() as BlogParser.HtmlTag); return(alink.ToHtml()); } else // No images in post { if (_UseDefaultImage) { BlogParser.HtmlTag alink = new BlogParser.HtmlTag("a", new BlogParser.HtmlAttribute("href", HttpUtility.UrlDecode(post.AbsoluteLink.AbsolutePath))); BlogParser.HtmlStyleAttribute styleAttr = new BlogParser.HtmlStyleAttribute(); styleAttr.AddRange(style); // Add single instance specified style styleAttr.AddRange(_ImageInlineStyle); // Add global specified style BlogParser.HtmlTag defaultTag = new BlogParser.HtmlTag("img", new BlogParser.HtmlAttribute("src", _DefaultImagePath), new BlogParser.HtmlAttribute("alt", post.Title), new BlogParser.HtmlAttribute("title", post.Title), new BlogParser.HtmlAttribute("class", _ImageCssClass), styleAttr); alink.InnerHtml.Add(defaultTag); return(alink.ToString()); } } } return(String.Empty); }