CarrierWave + RMagick ç»åã®ãªãµã¤ãºãã¾ã¨ãã¦ã¿ã¾ãã
ããã«ã¡ã¯ã麺å¦ã¾ã¤ã°å¯åºé·ã§ãã
ä¹
ã
ã®æ稿ã§ããã©ãããè²ã
æ°ã«ããªããã¨ã«ãã¾ããã
åºé·ã®è¦ç·ããªãã ã£ã¦è¨ããã§ãããï¼ããï¼
ãã¦å¯åºé·ãå
æ¥ä¸ãã£ã¨CarrierWaveãæ±ã£ã¦ããã¾ãã¦
ãã®ä¸ã§RMagickã使ã£ãç»åã®ãªãµã¤ãºããã£ã¦ããã¾ããã
ãã®ãªãµã¤ãºç¨ã®ã¡ã½ãããããã¤ãåå¨ããã®ã§ãã
ï¼http://rubydoc.info/gems/carrierwave/CarrierWave/RMagickï¼
å¯åºé·ã¯é³¥é ã®ããâ¦ãä½åº¦ãã£ã¦ããã©ããã©ããªããã ã£ãï¼ãã¨ããç¶æ³ã«â¦ã
ããã§ããªãµã¤ãºã¡ã½ããã¨å®è¡ã®çµæãä½ãã¿ã¼ã³ãã¾ã¨ãã¦ã¿ã¾ãã
ã¾ãã¯ãªãªã¸ãã«ã®ãã®ãããªç»åãç¨æãã¦ã¿ã¾ããã
â100Ã100ï¼åãã¹20x20ï¼ã®ç»åã§ãã
ï¼ãªãã5段ç®ã®éã ãæ¿ããã®ã§ãããã©ãããæ°ã«ãªãããâ¦ãï¼
ã§ã¯ããã®ç»åãä¸è¨ã®ã¡ã½ããã«ããã¦ã¿ã¾ãã
ã»resize_to_fit
ã»resize_to_limit
ã»resize_and_pad
ã»resize_to_fill
resize_to_fit(width, height)
ã¾ãã¯ã馴æã¿ã® resize_to_fit ã§ãã
縦横æ¯ãä¿æããã¾ã¾ãæå®ããããµã¤ãºã«åã¾ããããªãµã¤ãºãã¾ãã
æå®ãµã¤ãº | å ç»å | å®è¡å¾ã®ç»å | åè |
---|---|---|---|
150x150 | ã | ||
150x100 | ã | ||
150x50 | ã | ||
100x150 | |||
100x100 | ã | ||
100x50 | ã | ||
50x150 | ã | ||
50x100 | ã | ||
50x50 | ã |
resize_to_geometry_string(geometry_string)
å¼æ°ã«ä¸ããããæååããããªãµã¤ãºãè¡ãã¾ãã
resize_to_geometry_string("150x50")
ãã®ãããªå½¢ã§150ï½50ã§ãªãµã¤ãºãã¾ãã
å®è¡ããã¨ãã®æåã¯ãããã resize_to_fit ã®ããã¨åãã§ãã
éã£ã¦ãããã©ãªããããã³ããé¡ããã¾ãã
resize_to_limit(width, height)
resize_to_fitã¨åæ§ã«ç¸¦æ¨ªæ¯ãç¶æããã¾ã¾ãªãµã¤ãºãã¾ãã
resize_to_fitã¨ã®éãã¯ã対象ã®ç»åã®ãµã¤ãºãå¼æ°ã«æå®ããã縦横ãµã¤ãºä»¥å
ã®å ´åã¯ãªãµã¤ãºãã¾ããã
æå®ããã縦横ãµã¤ãºããªã¼ãã¼ããå ´åã«ã®ã¿ãæå®ããããµã¤ãºã«åã¾ããããªãµã¤ãºãã¾ãã
æå®ãµã¤ãº | å ç»å | å®è¡å¾ã®ç»å | åè |
---|---|---|---|
150x150 | resize_to_fitã¨éãã¨ããã | ||
150x100 | ã | ||
150x50 | ã | ||
100x150 | |||
100x100 | |||
100x50 | ã | ||
50x150 | ã | ||
50x100 | ã | ||
50x50 | ã |
resize_and_pad(width, height, background = :transparent, gravity = ::Magick::CenterGravity)
ä¸ããããwidthã¨height㧠resize_to_fit ã®ãªãµã¤ãºããã¾ãã
ããã«è¶³ããªãé¨åã«backgroundã®è²ãæ·ãã¾ãï¼å¯åºé·ã¯åº§å¸å£ã¨å¼ãã§ãã¾ããï¼
座å¸å£ãæ·ãã®ã§ãå¿
ãæå®ããããµã¤ãºã´ã£ããã®ç»åã«ãªãã¾ãã
gravityã¯åº§å¸å£ã®ä¸ã®ã©ã®ä½ç½®ã«ç»åãé
ç½®ããããã§ãã
ä¸è¨ã®ä¾ã§ã¯ãé»ã座å¸å£ã®ä¸å¤®ã«ãªãµã¤ãºããç»åãé
ç½®ããããã«ãã¦ãã¾ãã
æå®ãµã¤ãº | å ç»å | å®è¡å¾ã®ç»å | åè |
---|---|---|---|
150x150 | æã é»ãã®ã¯å ç»åãééã®ãã | ||
150x100 | ã | ||
150x50 | ã | ||
100x150 | |||
100x100 | |||
100x50 | ã | ||
50x150 | ã | ||
50x100 | ã | ||
50x50 | ã |
resize_to_fill(width, height, gravity = ::Magick::CenterGravity)
縦横æ¯åºå®ã§ãªãµã¤ãºããã¦ãããã«gravityã§æå®ãããä½ç½®ãããã¬ãã¾ãã
æå®ããã width 㨠height ã®ãµã¤ãºã§ããæããæã¾ã§å¼ã伸ã°ãã¦ãããã¬ãã¤ã¡ã¼ã¸ã
æå®ãµã¤ãº | å ç»å | å®è¡å¾ã®ç»å | åè |
---|---|---|---|
150x150 | ã | ||
150x100 | ã | ||
150x50 | ã | ||
100x150 | |||
100x100 | |||
100x50 | ã | ||
50x150 | ã | ||
50x100 | ã | ||
50x50 | ã |
ãªãã±ï¼manipulate!(options = {}, &block)
ä¸è¨ã«ãªããªãµã¤ãºã»å å·¥ãããå ´å㯠manipulate! ã使ãå¿
è¦ãããã¾ãã
ãã®ãããªæãã§ã
class MenuPhotoUploader < CarrierWave::Uploader::Base include CarrierWave::RMagick MAX_WIDTH = MAX_HEIGHT = 300 process :set_bgcolor def set_bgcolor manipulate! do |img| img.background_color = 'whitesmoke' img.extent(MAX_WIDTH, MAX_HEIGHT, -(MAX_WIDTH-img.columns)/2, -(MAX_HEIGHT-img.rows)/2) end end # end set_bgcolor end