// ---------------------------------------------------------------------- /// <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 or Create Image to display ImageInfo image = GetSmallFile(imgTags[tagIndex]["src"].Value); if (image != null) { if (!String.IsNullOrEmpty(image.File) && image.File[0] == '\0') // See if error from GetSmallFile and send back { if (Security.IsAuthenticated) { string r = image.File.Replace("\0", String.Empty); if (string.IsNullOrWhiteSpace(r)) { return(imgTags[tagIndex]["src"].Value); } else { return("\">" + r); } } else { return(string.Empty); } } imgTags[tagIndex]["src"].Value = image.File; // 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 (image.GetWidth() > _MaxWidth) { image.Width = _MaxWidth + "px"; } if (image.GetHeight() > _MaxHeight) { image.Height = _MaxHeight + "px"; } } if (_ForceMaxWidth) { image.Width = _MaxWidth.ToString() + _Units; imgTags[tagIndex]["width"].Value = image.Width; } else { imgTags[tagIndex]["width"].Value = null; } if (_ForceMaxHeight) { image.Height = _MaxHeight.ToString() + _Units; imgTags[tagIndex]["height"].Value = image.Height; } else { imgTags[tagIndex]["height"].Value = null; } 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 if (_ForceMaxWidth) { styleAttr.Add("width", image.Width, false); } if (_ForceMaxHeight) { styleAttr.Add("height", image.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 { // image == null 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()); } } } 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); }
// ---------------------------------------------------------------------- /// <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); }