热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

rubyCarrierWave扩展名fix_exif_rotation,strip,quality,resize_to_fill_if_larger

篇首语:本文由编程笔记#小编为大家整理,主要介绍了rubyCarrierWave扩展名fix_exif_rotation,strip,quality,resize_to_fill_if_lar

篇首语:本文由编程笔记#小编为大家整理,主要介绍了ruby CarrierWave扩展名fix_exif_rotation,strip,quality,resize_to_fill_if_larger相关的知识,希望对你有一定的参考价值。




class ImageUploader # Include RMagick or ImageScience support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# include CarrierWave::ImageScience
# Including support for the asset pipeline
# https://github.com/jnicklas/carrierwave/issues/493
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"#{CarrierWave::Uploader::Base.store_dir}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
asset_path("defaults/#{model.class.to_s.underscore}/default#{'_' if version_name.present?}#{version_name}.jpg")
#"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png bmp)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end


#config/initializers/carrierwave.rb
module CarrierWave
module RMagick
# Rotates the image based on the EXIF Orientation
def fix_exif_rotation
manipulate! do |img|
img.auto_orient!
img = yield(img) if block_given?
img
end
end
# Strips out all embedded information from the image
def strip
manipulate! do |img|
img.strip!
img = yield(img) if block_given?
img
end
end
# Reduces the quality of the image to the percentage given
def quality(percentage)
manipulate! do |img|
img.write(current_path){ self.quality = percentage }
img = yield(img) if block_given?
img
end
end
def resize_to_fill_if_larger(width, height, gravity=::Magick::CenterGravity)
geometry = get_geometry
if geometry.first>width && geometry.last > height
manipulate! do |img|
img.crop_resized!(width, height, gravity)
img = yield(img) if block_given?
img
end
end
end
def get_geometry
img = ::Magick::Image::read(current_path).first
geometry = [ img.columns, img.rows ]
end
module ClassMethods
def resize_to_fill_if_larger(width, height, gravity=::Magick::CenterGravity)
process :resize_to_fill_if_larger => [width, height]
end
end
end
end


class ArticleImageUploader process :fix_exif_rotation
process :strip
process :cOnvert=> 'jpg'
process :quality => 85 # Percentage from 0 - 100
version :gallery_thumb do
process :resize_to_fill => Settings.images.article_images.processing.gallery_thumb #44x44
end
version :gallery_large do
process :resize_to_fill_if_larger => Settings.images.article_images.processing.gallery_large #604x300
end
version :side_bar do
process :resize_to_fill => Settings.images.article_images.processing.side_bar #140x106
end
version :large_lightbox do
process :resize_to_limit => Settings.images.article_images.processing.large_lightbox #1200x720
end
version :featured do
process :resize_to_fill => Settings.images.article_images.processing.featured #303x300
end
version :category_list_top do
process :resize_to_fill => Settings.images.article_images.processing.category_list_top #222x167
end
version :category_list_thumb do
process :resize_to_fill => Settings.images.article_images.processing.category_list_thumb #60x45
end
end


推荐阅读
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了关系型数据库和NoSQL数据库的概念和特点,列举了主流的关系型数据库和NoSQL数据库,同时描述了它们在新闻、电商抢购信息和微博热点信息等场景中的应用。此外,还提供了MySQL配置文件的相关内容。 ... [详细]
  • 本文介绍了使用FormData对象上传文件同时附带其他参数的方法。通过创建一个表单,将文件和参数添加到FormData对象中,然后使用ajax发送POST请求进行文件上传。在发送请求时,需要设置processData为false,告诉jquery不要处理发送的数据;同时设置contentType为false,告诉jquery不要设置content-Type请求头。 ... [详细]
  • 近期,某用户在重启RAC一个节点的数据库实例时,发现启动速度非常慢。同时业务部门反馈连接RAC存活节点的业务也受影响。通过对日志的分析, ... [详细]
  • {moduleinfo:{card_count:[{count_phone:1,count:1}],search_count:[{count_phone:4 ... [详细]
  • 1.官网下载了mysql-5.7.17-win64.zip包,配置遇到很多麻烦,记录一下;2.解压后放到指定的文件夹,修改mysql-5.7.17的配置文件my-default.i ... [详细]
  • x86 linux的进程调度,x86体系结构下Linux2.6.26的进程调度和切换
    进程调度相关数据结构task_structtask_struct是进程在内核中对应的数据结构,它标识了进程的状态等各项信息。其中有一项thread_struct结构的 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 浅析Mysql数据回滚错误的解决方法_PHP教程:MYSQL的事务处理主要有两种方法。1、用begin,rollback,commit来实现begin开始一个事务rollback事 ... [详细]
author-avatar
尚庆湃
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有