Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image resizing #487

Closed
polsvoice opened this issue May 9, 2015 · 5 comments
Closed

Image resizing #487

polsvoice opened this issue May 9, 2015 · 5 comments

Comments

@polsvoice
Copy link

@polsvoice polsvoice commented May 9, 2015

I was wondering if Redcarpet supports image resizing, as mentioned in the top comment in this StackOverflow thread. For example, to display an image at 300px wide x 450px high:

![](http://www.example.com/myImage.png =300x450)

Would this type of behavior be considered an extension or a renderer?

@robin850
Copy link
Collaborator

@robin850 robin850 commented May 10, 2015

Hi @polsvoice,

Actually that's pretty easy to implement yourself with a custom render:

class CustomRender < Redcarpet::Render::HTML
  def image(link, title, alt_text)
    if title =~ /=(\d+)x(\d+)/
      %(<img src="#{link}" width="#{$1}px" height="#{$2}px" alt="#{alt_text}>")
    else
      %(<img src="#{link}" title="#{title}" alt="#{alt_text}">)
    end
  end
end

With this custom render, you'll have, however, to surround the =wxh syntax with quotes. If you do not want to, you can do link = link.split(" ") and do the =~on link[1] and replace link with link[0].

Thanks for spotting this! :-)

@GreenGremlin
Copy link

@GreenGremlin GreenGremlin commented Oct 14, 2016

One note, the image height and width attributes shouldn't have units.

class CustomRender < Redcarpet::Render::HTML
  def image(link, title, alt_text)
    if title =~ /=(\d+)x(\d+)/
      %(<img src="#{link}" width="#{$1}" height="#{$2}" alt="#{alt_text}>")
    else
      %(<img src="#{link}" title="#{title}" alt="#{alt_text}">)
    end
  end
end

@robin850
Copy link
Collaborator

@robin850 robin850 commented Oct 15, 2016

Ah thanks @GreenGremlin !

@soundasleep
Copy link

@soundasleep soundasleep commented May 18, 2017

I think this would be a great idea to add into the engine. I found the example code a little broken (it wasn't regexping against the link, just title), this worked really well for me:

    def image(link, title, alt_text)
      if link =~ /^(.+?)\s*=+(\d+)(?:px|)$/
        # e.g. ![alt](url.png =100px)
        # e.g. ![alt](url.png =100)
        %(<img src="#{$1}" style="max-width: #{$2}px" alt="#{alt_text}">)
      elsif link =~ /^(.+?)\s*=+(\d+)(?:px|)x(\d+)(?:px|)$/
        # e.g. ![alt](url.png =30x50)
        %(<img src="#{$1}" style="max-width: #{$2}px; max-height: #{$3}px;" alt="#{alt_text}">)
      else
        %(<img src="#{link}" title="#{title}" alt="#{alt_text}">)
      end
    end

If you'd like I can submit this as a PR, perhaps as a renderer option. I think it's a pretty common scenario.

@drum445
Copy link

@drum445 drum445 commented Feb 16, 2019

None of the above seem to work correctly, this did the trick for me

class CustomRender < Redcarpet::Render::HTML
  def image(link, title, alt_text)

    parts = link.split(" ")
    link = parts[0]

    if parts.size == 1
      # ![alt](url.png)
      %(<img src="#{link}" title="#{title}" alt="#{alt_text}">)
    else
      w, h = parts[1].gsub("=", "").gsub("px", "").split("x")
      if h
        # ![alt](url.png =500x200)
        %(<img src="#{link}" width="#{w}" height="#{h}" alt="#{alt_text}">)
      else
        # ![alt](url.png =500x)
        %(<img src="#{link}" width="#{w}" alt="#{alt_text}">)
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants