Re: [CommunityEngine] Serving scaled images for posts (first_image_in_body)

15 views
Skip to first unread message

Bruno Bornsztein

unread,
Jul 15, 2013, 11:12:08 AM7/15/13
to communi...@googlegroups.com
Hi,
Well, you could certainly override the first_image_in_body method with your own logic. Unfortunately, since the image is hardcoded into the HTML, you'll probably have to use Regexes to take the image URL and get the thumb. Here's an example (I do this on Curbly):

  def first_image_in_body(size = nil, options = {})
    styles = configatron.photo.paperclip_options.styles.to_hash.keys

    doc = Hpricot( post )
    image = doc.at("img")
    if image && !image['src'].blank?
      src = image['src']
      if size
        regex = /\_(#{styles.map(&:to_s).join('|')})(\.)(.*)$/
        if match = src.match(regex) #image already has a style
          format = match[3]
          src.gsub(regex, "_#{size.to_s}\\2#{format}")
        else
          regex = /(.*)(\.)([^\/]+)$/
          if match = src.match(regex)
            format = match[3]
            src.gsub(regex, "\\1_#{size}\\2#{format}")
          end
        end
      else
        src
      end
    else
      nil
    end
  end


(Yeah, that's ugly ... sorry).

Hope that helps.
Bruno



On Tue, Jul 9, 2013 at 2:06 PM, nml <dr.nan...@gmail.com> wrote:
Hi,

Right now, the first_image_in_body method in the posts model leads to the full sized image for any image uploaded into a post. Is it possible to have the url point to the scaled versions of the uploaded images (:thumb or :medium) for the first image in a post?

Thanks for any help!

--
You received this message because you are subscribed to the Google Groups "CommunityEngine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to communityengi...@googlegroups.com.
To post to this group, send email to communi...@googlegroups.com.
Visit this group at http://groups.google.com/group/communityengine.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
Message has been deleted
0 new messages