Ridimensionamento in stile Flickr con attaccment_fu
-
20-09-2019 - |
Domanda
Voglio che adwachment_fu ridimensiona le mie miniature in modo simile a come Flickr, Facebook e Twitter gestiscono questo: se voglio una miniatura 100x100, voglio che la miniatura sia esattamente 100x100 con qualsiasi eccesso di ritagliato in modo che il rapporto di aspetto sia preservato.
Qualche idea?
Soluzione 4
La mia soluzione era di approfondire la cartella del plug -in Attachment_FU (fornitore/plugin) e modificare il file rmagick_processor.rb. Per prima cosa ho ribattezzato Rasize_Image su Rasize_Image_Internal, quindi ho aggiunto:
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
Ora posso usare 'Square: 100x100' come stringa di geometria. Si noti che il codice sopra presuppone che l'output richiesto sia quadrato.
Altri suggerimenti
Per impostare le miniature 100x100, aggiungi quanto segue al tuo modello:
has_attachment :content_type => :image,
:storage => IMAGE_STORAGE,
:max_size => 20.megabytes,
:thumbnails => {
:thumb => '100x100>',
:large => '800x600>',
}
(In questo esempio, sto creando una miniatura 100x100 e anche una dimensione "di grandi dimensioni" di 800x600, in più per mantenere la dimensione originale.)
Inoltre, tieni presente che la miniatura potrebbe non essere esattamente 100x100; Avrà una dimensione massima di 100x100. Ciò significa che se l'originale ha una razione di aspetto di 4: 3, la miniatura sarebbe 100x75. Non sono esattamente sicuro che questo sia quello che intendevi per "esattamente 100x100 con qualsiasi eccesso ritagliato in modo che il rapporto di aspetto sia preservato".
Aggiungi questo al tuo modello
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
e cambia la dimensione della miniatura in:
:thumbnails => {:thumb => 'crop: 100x100' }
fonte:
http://stuff-things.net/2008/02/21/quick-and-dirty-cropping-images-with-attachment_fu/
C'è una direttiva di ritaglio che può essere fornita nella specifica:
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '100x100#'
}
Memoic: '#' sembra lo strumento Crop.
Modificare: Correzione
has_attachment :content_type => :image,
:thumbnails => {
:thumb => '100x100!'
}
Il metodo precedente era per PaperClip che ha una notazione diversa.