How to set Picture Thumbnail on Postlist?

Topics: Controls
Aug 18, 2010 at 3:20 PM
Edited Aug 18, 2010 at 3:39 PM

I need some guidance in making the postlist on the home page show a thumbnail of the uploaded picture;  I set the settings "Only show description of posts" to true so I am only getting description of posts but with no picture and I want to show a thumbnail of the user uploaded picture (small size).

So is there any extension I dont know about, or can you guide me about what to do...

Aug 20, 2010 at 9:21 AM

Ok guys, I figured it out; Contact me if you want the code...

Sep 8, 2010 at 10:21 PM

This is something that I've been interested in doing.  Would you mind sharing the code that you used?

Thanks.

Sep 9, 2010 at 9:26 AM

I've been receiving requests to post my code, sorry for the delay I have been busy.

In order for this code to work perfectly you must set in the blogengine settings:

Only show description of posts & Only show description of posts when listed by Tag or Category to true, and I only test it on the blogengine 1.6 so I dont know if it will work on previous versions.

So copy the below code (sorry I dont know where to upload the class) and replace the code of (App_code/Extensions/Breakpost.cs) class and let me know if you need further help.


#region using

using System;
using System.Web;
using System.Web.UI;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Text.RegularExpressions;
using System.Collections.Generic;

#endregion

/// <summary>
/// Breaks a post where [more] is found in the body and adds a link to full post.
/// </summary>
[Extension("Breaks a post where [more] is found in the body and adds a link to full post", "1.4", "BlogEngine.NET", 1010)]
public class BreakPost
{

    /// <summary>
    /// Hooks up an event handler to the Post.Serving event.
    /// </summary>
    static BreakPost()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
    }

    /// <summary>
    /// Handles the Post.Serving event to take care of the [more] keyword.
    /// </summary>
    private static void Post_Serving(object sender, ServingEventArgs e)
    {
        if (BlogEngine.Core.BlogSettings.Instance.ShowDescriptionInPostList == false)
            return;
        //if (e.Body.IndexOf("[Read more]") == -1)
        //  return;

        if (e.Location == ServingLocation.PostList)
        {
            AddMoreLink(sender, e);
        }
        else if (e.Location == ServingLocation.SinglePost)
        {
            PrepareFullPost(e);
        }
        else if (e.Location == ServingLocation.Feed)
        {
            e.Body = e.Body.Replace("[Read more]", string.Empty);
        }
    }

    private static Regex openingTagRegex = new Regex(@"<([A-Z][A-Z0-9]*?)\b[^>/]*>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    private static Regex closedTagRegex = new Regex(@"</([A-Z][A-Z0-9]*?)\b[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

    /// <summary>
    /// Replaces the [more] string with a hyperlink to the full post.
    /// </summary>
    private static void AddMoreLink(object sender, ServingEventArgs e)
    {
        Post post = (Post)sender;

        int index = e.Body.IndexOf("[Read more]");
        if (index == -1)
        {
            e.Body = GenerateThumbImage(post.Content) + e.Body;
            return;
        }
        string link = "<a class=\"more\" href=\"" + post.RelativeLink + "#continue\">" + Resources.labels.more + "</a>";
        string NewBody = e.Body.Substring(0, index);

        // Need to close any open HTML tags in NewBody where the matching close tags have been truncated.
        string closingTagsToAppend = string.Empty;
        MatchCollection openingTagsCollection = openingTagRegex.Matches(NewBody);

        if (openingTagsCollection.Count > 0)
        {
            // Copy the opening tags in MatchCollection to a generic list.
            List<string> openingTags = new List<string>();
            foreach (Match openTag in openingTagsCollection)
            {
                if (openTag.Groups.Count == 2)
                {
                    openingTags.Add(openTag.Groups[1].Value);
                }
            }
            MatchCollection closingTagsCollection = closedTagRegex.Matches(NewBody);
            // Iterate through closed tags and remove the first matching open tag from the openingTags list.
            foreach (Match closedTag in closingTagsCollection)
            {
                if (closedTag.Groups.Count == 2)
                {
                    int indexToRemove = openingTags.FindIndex(delegate(string openTag) { return openTag.Equals(closedTag.Groups[1].Value, StringComparison.InvariantCultureIgnoreCase); });
                    if (indexToRemove != -1)
                        openingTags.RemoveAt(indexToRemove);
                }
            }
            // A closing tag needs to be created for any remaining tags in the openingTags list.
            if (openingTags.Count > 0)
            {
                // Reverse the order of the tags so tags opened later are closed first.
                openingTags.Reverse();
                closingTagsToAppend = "</" + string.Join("></", openingTags.ToArray()) + ">";
            }
        }
        e.Body = GenerateThumbImage(post.Content) + NewBody + link + closingTagsToAppend;


    }
    protected static string GenerateThumbImage(string html)
    {
        try
        {
            string newhtml_Image = "";
            int Thumb_Width = 100;
            int StartIndex = html.IndexOf("<img");
            int EndIndex = html.IndexOf("/>", StartIndex);
            string html_Image = html.Substring(StartIndex, EndIndex - StartIndex + 2);
            int WidthStart = html_Image.IndexOf("width=", 0);
            if (WidthStart == -1)
            {
                int LastIndex = html_Image.IndexOf("/>", 0);
                newhtml_Image = "<p>" + html_Image.Substring(0, LastIndex - 1) + " width=\"" + Thumb_Width + "\" />" + "</p>";
                return newhtml_Image;
            }
            int WidthEnd = html_Image.IndexOf("\"", WidthStart + 9);
            int htmlwidth = Convert.ToInt32(html_Image.Substring(WidthStart + 7, WidthEnd - WidthStart - 7));
            int HeightStart = html_Image.IndexOf("height=");
            int HeightEnd = html_Image.IndexOf("\"", HeightStart + 9);
            int htmlheight = Convert.ToInt32(html_Image.Substring(HeightStart + 8, HeightEnd - HeightStart - 8));
            newhtml_Image = "<p>" + html_Image.Substring(0, WidthStart) + "width=\"" + Thumb_Width + "\" height=\"" + (int)((float)htmlheight / ((float)htmlwidth / (float)Thumb_Width)) + "\" />" + "</p>";
            return newhtml_Image;
        }
        catch
        {
            return "";
        }
    }

    /// <summary>
    /// Replaces the [more] string on the full post page.
    /// </summary>
    private static void PrepareFullPost(ServingEventArgs e)
    {
        HttpRequest request = HttpContext.Current.Request;
        if (request.UrlReferrer == null || request.UrlReferrer.Host != request.Url.Host)
        {
            e.Body = e.Body.Replace("[more]", string.Empty);
        }
        else
        {
            e.Body = e.Body.Replace("[more]", "<span id=\"continue\"></span>");
        }
    }

}

Sep 10, 2010 at 3:13 AM

Thank you very much.

I don't plan on replacing the BreakPost extension, but using similar logic in an extension is a great idea.