Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
refs #17 新規投稿: 画像のサイズチェックをJS側で行うように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Nov 7, 2013
1 parent 063ae6b commit 5471635
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
25 changes: 3 additions & 22 deletions app/models/clip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ def like_count
end

def create_image
return if @url.nil?
return false if @url.nil?
image = Image.where(url: @url).first
if image.nil?
require 'open-uri'
require 'image_size'
image_size = ImageSize.new(open(@url))
image = Image.create(url: @url, width: image_size.width, height: image_size.height)
return false if image.nil?
end
self.image_id = image.id
end
Expand All @@ -83,7 +84,7 @@ def thumb_size_for_style_sheet
end

def create_html_only_images
self.listup_available_image_urls.map do |url|
self.listup_image_urls_from_html.map do |url|
ActionController::Base.helpers.image_tag(url)
end.join
end
Expand All @@ -93,16 +94,6 @@ def create_html_only_images_with_pagenate(page)
create_html_only_images.paginate(page: page, per_page: self.class.per_page).join
end

def listup_available_image_urls
require 'open-uri'
require 'image_size'
listup_image_urls_from_html.select do |image_url|
file = open(image_url) rescue next
image_size = ImageSize.new(file)
(image_size.size.present? && available_size_image?(image_size) && available_aspect_image?(image_size))
end
end

def listup_image_urls_from_html
require 'hpricot'
return [] if self.origin_html.blank?
Expand Down Expand Up @@ -169,14 +160,4 @@ def uri_relative_path(path)
return URI.parse(path).path if path =~ /^http/
path
end

def available_size_image?(image_size)
image_size.width > 16 && image_size.height > 16
end

def available_aspect_image?(image_size)
aspect = [ 16, 5 ]
image_aspect = 1.0 * image_size.width / image_size.height
image_aspect < (aspect[0] / aspect[1]) && image_aspect > (aspect[1] / aspect[0])
end
end
3 changes: 3 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class Image < ActiveRecord::Base

#validates :url, :uniqueness => true, :presence => true, :format => { :with => /^https?:\/\/.*\/.*\.(jpg|jpeg|png|gif)$/ }
validates :url, :uniqueness => true, :presence => true, :format => { :with => /^https?:\/\/.*\/.*$/ }
validates :width, :numericality => { :greater_than => 16 }
validates :height, :numericality => { :greater_than => 16 }
validates_with ImageAspectValidator, :width => :width, :height => :height, :aspect => [ 16, 5 ]

def thumb_path
require 'digest/sha2'
Expand Down
9 changes: 7 additions & 2 deletions app/views/clips/get_image_tags.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ var $container = $('#container')
$(this).find('.box img').each(function() {
width = <%= Settings.thumb_width %>;
height = Math.floor($(this).height() * (width / $(this).width() * 1.0));
$(this).width(width);
$(this).height(height);
aspect = width / height * 1.0;
if (width > 16 && height > 16 && (aspect < (16 / 5.0) && aspect > (5.0 / 16))) {
$(this).width(width);
$(this).height(height);
} else {
$(this).parent().remove();
}
});
$(this).removeClass('hidden');
$(this).masonry('reload');
Expand Down
14 changes: 14 additions & 0 deletions lib/validator/image_aspect_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ImageAspectValidator < ActiveModel::Validator
def validate(record)
width = record.send(options[:width])
height = record.send(options[:height])
record.errors.add(:base, "Aspect ratio that not meet #{options[:aspect].join(':')}.") unless avable_aspect?(width, height)
end

def avable_aspect?(width, height)
horizontal_aspect = options[:aspect][0]
vertical_aspect = options[:aspect][1]
aspect = width / height * 1.0
aspect < (horizontal_aspect / vertical_aspect * 1.0) && aspect > (vertical_aspect / horizontal_aspect * 1.0)
end
end

0 comments on commit 5471635

Please sign in to comment.