تغيير حجم نمط فليكر مع الملحق_FU
-
20-09-2019 - |
سؤال
أريد مرفق_FU لتغيير حجم الصور المصغرة بطريقة مماثلة لطريقة مماثلة لكيفية التعامل مع Flickr و Facebook و Twitter هذا: إذا كنت أريد مصغرة 100x100 أريد أن تكون الصورة المصغرة أن أكون بالضبط 100x100 مع أي اقتصاص فائض حتى يتم الحفاظ على نسبة العرض إلى الارتفاع.
أيه أفكار؟
المحلول 4
كان الحل الخاص بي هو الخوض في المجلد المكون الإضافي Attachment_fu (البائع / الإضافات) وتحرير ملف rmagick_processor.rb. أولا قمت بإعادة تسميته Resize_Image ل Resize_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
يمكنني الآن استخدام "مربع: 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-Clages-Images-With-Attachment_fu/
هناك توجيه اقتصاص يمكن تقديمه في المواصفات:
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '100x100#'
}
المينيك: "#" يشبه أداة المحاصيل.
تعديل: تصحيح
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '100x100!'
}
الطريقة السابقة كانت للأوراق ذات الأوراق التي لديها تدوين مختلف.