{"id":70,"date":"2009-04-15T08:27:32","date_gmt":"2009-04-15T07:27:32","guid":{"rendered":"http:\/\/www.any-where.de\/blog\/?p=70"},"modified":"2009-04-15T08:28:25","modified_gmt":"2009-04-15T07:28:25","slug":"rails-custom-image-sizes-using-has_attachment","status":"publish","type":"post","link":"https:\/\/www.any-where.de\/blog\/rails-custom-image-sizes-using-has_attachment\/","title":{"rendered":"Rails: Custom image sizes using has_attachment"},"content":{"rendered":"<p>Ever wanted to offer users the possibility to define their very own image sizes for uploaded pictures?<\/p>\n<p>Here&#8217;s how you can quickly do that.<\/p>\n<p>You will have a class acting as a an attachment such as:<\/p>\n<pre>class AssetImage &lt; ActiveRecord::Base\r\n\r\nhas_attachment :content_type =&gt; :image,\r\n:storage =&gt; :file_system,\r\n:max_size =&gt; 1.megabytes,\r\n:path_prefix =&gt; \"public\/system\/images\/#{ActiveRecord::Base.configurations[RAILS_ENV]['domain_name']}\/assets\/images\",\r\n:thumbnails =&gt; { :large =&gt; '450&gt;', :normal =&gt; '300', :medium =&gt; '200', :thumbnail =&gt; [100,75] },\r\n:processor =&gt; :rmagick\r\n\r\nvalidates_as_attachment\r\n\r\n...<\/pre>\n<p>First add a cattr_accessor to store your custom size in the class:<\/p>\n<pre>cattr_accessor :custom_size<\/pre>\n<p>Now, add a before_save callback which takes the assigned value and adds it to the list of requested image sizes. It is using the value as a minimum size while keeping aspect ratio.<\/p>\n<pre>#store custom size\r\ndef before_save\r\n  return if AssetImage.custom_size.nil?\r\n  attachment_options[:thumbnails][:custom] = AssetImage.custom_size.to_s + \"&gt;\"\r\nend<\/pre>\n<p>In your controller do something like this: Take a value from your parameters and store it in your model to have your image customized.<\/p>\n<pre>def add_image\r\n  AssetImage.custom_size = params[:custom_size].to_i.to_s rescue \"100\"\r\n  a = AssetImage.new(params[:asset_image])\r\n  a.save\r\n  redirect_to :action =&gt; :index\r\nend<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ever wanted to offer users the possibility to define their very own image sizes for uploaded pictures? Here&#8217;s how you can quickly do that. You will have a class acting as a an attachment such as: class AssetImage &lt; ActiveRecord::Base has_attachment :content_type =&gt; :image, :storage =&gt; :file_system, :max_size =&gt; 1.megabytes, :path_prefix =&gt; &#8220;public\/system\/images\/#{ActiveRecord::Base.configurations[RAILS_ENV][&#8216;domain_name&#8217;]}\/assets\/images&#8221;, :thumbnails =&gt; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.any-where.de\/blog\/rails-custom-image-sizes-using-has_attachment\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rails: Custom image sizes using has_attachment&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,25],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-programming-the-web","category-ruby-on-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":4,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":75,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions\/75"}],"wp:attachment":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}