첨부 파일 _fu와 함께 Flickr 스타일 크기 조정
-
20-09-2019 - |
문제
나는 Flickr, Facebook 및 Twitter를 처리하는 방법과 비슷한 방식으로 첨부 파일 _fu를 크기를 조정하기를 원합니다. 100x100 썸네일을 원한다면 종횡비가 보존되도록 과잉 자른 상태에서 썸네일이 정확히 100x100이되기를 원합니다.
어떤 아이디어?
해결책 4
내 솔루션은 attachment_fu 플러그인 폴더 (공급 업체/플러그인)를 탐구하고 rmagick_processor.rb 파일을 편집하는 것입니다. 먼저 RESIZE_IMAGE로 이름을 RESINE_IMAGE_INternal로 변경 한 다음 다음을 추가했습니다.
def resize_image(img, size)
# resize_image take size in a number of formats, we just want
# Strings in the form of "square: WxH"
if (size.is_a?(String) && size =~ /^square: (\d*)x(\d*)/i) ||
(size.is_a?(Array) && size.first.is_a?(String) &&
size.first =~ /^square: (\d*)x(\d*)/i)
iw, ih = img.columns, img.rows
aspect = iw.to_f / ih.to_f
if aspect > 1
shave_off = (iw - ih) / 2
img.shave!(shave_off, 0)
else
shave_off = (ih-iw) / 2
img.shave!(0, shave_off)
end
resize_image_internal(img, "#{$1}x#{$2}!")
else
resize_image_internal(img, size) # Otherwise let attachment_fu handle it
end
end
이제 'Square : 100x100'을 기하학 문자열로 사용할 수 있습니다. 위의 코드는 필요한 출력이 제곱이라고 가정합니다.
다른 팁
100x100 썸네일을 설정하려면 다음을 모델에 추가하십시오.
has_attachment :content_type => :image,
:storage => IMAGE_STORAGE,
:max_size => 20.megabytes,
:thumbnails => {
:thumb => '100x100>',
:large => '800x600>',
}
(이 예에서는 원래 크기를 유지하는 것보다 100x100 썸네일과 800x600 '큰'크기를 만들고 있습니다.)
또한 썸네일은 정확히 100x100이 아닐 수도 있습니다. 최대 치수는 100x100입니다. 이는 원본의 측면 배급이 4 : 3 인 경우 썸네일은 100x75입니다. 나는 그것이 당신이 "종횡비가 보존되도록 과잉 자른 끄기를 가진 정확히 100x100"의 의미인지 확실하지 않습니다.
이것을 모델에 추가하십시오
protected
# Override image resizing method
def resize_image(img, size)
# resize_image take size in a number of formats, we just want
# Strings in the form of "crop: WxH"
if (size.is_a?(String) && size =~ /^crop: (\d*)x(\d*)/i) ||
(size.is_a?(Array) && size.first.is_a?(String) &&
size.first =~ /^crop: (\d*)x(\d*)/i)
img.crop_resized!($1.to_i, $2.to_i)
# We need to save the resized image in the same way the
# orignal does.
self.temp_path = write_to_temp_file(img.to_blob)
else
super # Otherwise let attachment_fu handle it
end
end
축소판 크기를 다음으로 변경합니다.
:thumbnails => {:thumb => 'crop: 100x100' }
원천:
http://stuff-things.net/2008/02/21/quick-and-dirty-cropping-images-with-attachment_fu/
사양에 제공 될 수있는 자르기 지침이 있습니다.
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '100x100#'
}
Memonic : '#'는 작물 도구처럼 보입니다.
편집하다: 보정
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '100x100!'
}
이전 방법은 다른 표기법이있는 종이 클립에 대한 것이 었습니다.