使用imagemagick动态裁剪和图像

时间:2013-08-05 17:28:43

标签: jquery ruby-on-rails image imagemagick rmagick

所以我有一些erb可以根据用户输入动态生成逐步说明。在每个步骤中都有文本,然后是至少一个图像标记覆盖在顶部(突出显示步骤指令文本所指的内容)。问题是现在图像尺寸太大而不利于在移动设备上显示。 (它们的最大宽度为640px)。我有每个标签的X-Y位置,所以我想动态裁剪每个标签,以减少指令加载时的图像宽度(我认为300px会很好)。我一直在做一些研究,我认为这可以用imagemagick或rmagick完成,但我不完全确定。任何正确方向的帮助都将受到高度赞赏。谢谢你。

jQuery(标签的显示方式)

$('span.i_contact').each(function() {                
    var pos_width = $(this).data('pos-width');
    var pos_height = $(this).data('pos-height');
    var xpos = ($(this).data('pos-x')) / 2.1;
    var ypos = ($(this).data('pos-y')) / 2.1;
    var taggedNode = $('<div class="tagged" />')
    taggedNode.css({
        "border":"5px solid orange",
        "width":pos_width,
        "height":pos_height,
        "left":xpos,
        "top":ypos
    });

    var n = $(this).data('index')
    $('.i_tagmap' + n).append(taggedNode)  
    console.log(taggedNode.position())    
});

$("span.o_contact").each(function() {            
    var pos_width = $(this).data('pos-width');
    var pos_height = $(this).data('pos-height');
    var xpos = ($(this).data('pos-x')) / 2.1;
    var ypos = ($(this).data('pos-y')) / 2.1;

    var taggedNode = $('<div class="tagged" />')
    taggedNode.css({
        "border":"5px solid green",
        "width":pos_width,
        "height":pos_height,
        "left":xpos,
        "top":ypos  
    });
    var n = $(this).data('index')
    $('.o_tagmap' + n).append(taggedNode)        
});

ERB(如何生成每个步骤)

<% n = steps.index(step) %>
<section id="step<%= n+1 %>" style="padding-top: 60px;">
<h2 style="margin-left:20px;"> Step <%= n+1 %></h2>
<div class="stepcontainer">
    <div class="steptext">
        <% if step.priority == 1 %>
            <%= "Plug the #{step.i_connection.cord_type.name} end of the cable into the #{step.i_product.full_name}.  Then plug the #{step.o_connection.cord_type.name} end into the #{step.o_product.full_name}." %>
        <% elsif step.priority == 2 %>
            <%= "Plug the #{step.i_connection.cord_type.name} end of the cable into the #{step.i_product.full_name}.  Then plug the #{step.o_connection.cord_type.name} end into the #{step.o_product.full_name}." %>
        <% elsif step.priority == 3 %>
            <%= "Plug the #{step.o_connection.product.full_name } #{step.o_connection.cord_type.name} Cable into the wall." %>
        <% elsif step.priority == 4 %>
            <%= "Plug the #{step.i_connection.cord_type.name} end of the cable into the #{step.i_connection.product.full_name}.  Then plug the #{step.o_connection.cord_type.name} end into the #{step.o_connection.product.full_name}." %>
        <% elsif step.priority == 5 %>
            <%= "Plug #{step.o_connection.product.full_name}" %>
        <% elsif step.priority == 6 %>
            <%= "Touch the #{step.o_connection.button.name} Button on the #{step.o_connection.product.full_name}" %>
        <% end %>
    </div>

    <div class="modalbutton">
        <%= render(step.flags.new) %>   
    </div>

    <div class="productimg">    
        <span class="o_contact o_contact<%= n %>" data-pos-x="<%= step.o_connection.pos_x %>" data-pos-y="<%= step.o_connection.pos_y %>"  data-pos-width="<%= step.o_connection.pos_width %>" data-pos-height="<%= step.o_connection.pos_height %>" id="spanid<%= n %>" data-index="<%= n %>"> </span>

    <% if step.input_contact.present? %>
        <span class="i_contact i_contact<%= n %>" data-pos-x="<%= step.i_connection.pos_x %>" data-pos-y="<%= step.i_connection.pos_y %>"  data-pos-width="<%= step.i_connection.pos_width %>" data-pos-height="<%= step.i_connection.pos_height %>" ="spanid<%= n %>" data-index="<%= n %>"></span>    

        <div id="image_panel<%= n %>" style="float:left; width:300px; position:relative;">
            <%= link_to image_tag(step.i_connection.image.image.url(:medium)), "#{step.i_connection.image.image.url(:large)}", class: "fancybox" %>
            <div class="i_tagmap<%= n %>"></div>
        </div>      
    </div>

    <div class="cableimg">
        <% if step.i_connection.cord_type.present? %>
            <%= image_tag(step.i_connection.cord_type.image.url(:thumb), :class => "orange")  %>
        <% end %>           
        <% end %>   

        <% if step.o_connection.cord_type.present? %>
            <%= image_tag(step.o_connection.cord_type.image.url(:thumb), :class => "green") %>      
        <% end %>
    </div>

    <% if step.o_connection.button.present? %>
        <div class="productimg">
            <div id="image_panel<%= n %>" style="float:left; width:300px; position:relative;">
                <%= link_to image_tag(step.o_connection.image.image.url(:medium)), "#{step.o_connection.image.image.url(:large)}", class: "fancybox" %>
                <div class="o_tagmap<%= n %>"></div>
            </div>  
        </div>

    <% else %>
        <div class="productimg">
            <div id="image_panel<%= n %>" style="float:left; width:300px; position:relative;">
                <%= link_to image_tag(step.o_connection.image.image.url(:medium)), "#{step.o_connection.image.image.url(:large)}", class: "fancybox" %>
                <div class="o_tagmap<%= n %>"></div>
            </div>  
        </div>              
    <% end %>
</div>
 </section>

0 个答案:

没有答案