where the second and the third parameter is the desired size in pixels (auto means auto-fit to the scale).
@thumbs = Photo. view_thumbs ( 'originals_name_file.jpg' , '100' , 'auto' )
- require 'RMagick'
- class Photo < ActiveRecord :: Base
- def self . view_thumbs ( image, width = 'auto' , height = 'auto' )
- img_arr = image. split ( "." )
- img, img_type = img_arr [ 0 ] , img_arr [ 1 ]
- img_thumbs = "# {img} _ # {width} x # {height}"
- img_main_dir = "# {RAILS_ROOT} / public / images /"
- img_thumbs_dir = "# {RAILS_ROOT} / public / images / cache /"
- begin
- img_thumbs = Magick :: Image . read ( "# {img_thumbs_dir} / # {img_thumbs}. # {img_type}" )
- rescue Magick :: ImageMagickError # All the salt is here. If there is no necessary thumbs, then after reading the error falls out, which we save. If everything is ok, then the code is not executed further.
- img_orig = Magick :: Image . read ( "# {img_main_dir} / # {image}" ) . first
- img_size = { : main => { : cols => img_orig. columns ,: rows => img_orig. rows }
- : thumb => { : cols => 0.0 ,: rows => 0.0 }
- }
- if 'auto' == width and 'auto' == height
- img_size [ : thumb ] [ : rows ] = img_size [ : main ] [ : rows ]
- img_size [ : thumb ] [ : cols ] = img_size [ : main ] [ : cols ]
- end
- if 'auto' ! = width and 'auto' == height
- img_size [ : thumb ] [ : rows ] = ( ( width. to_f / img_size [ : main ] [ : cols ] ) * img_size [ : main ] [ : rows ] ) . to_i
- img_size [ : thumb ] [ : cols ] = width. to_i
- end
- if 'auto' == width and 'auto' ! = height
- img_size [ : thumb ] [ : rows ] = height. to_i
- img_size [ : thumb ] [ : cols ] = ( ( height. to_f / img_size [ : main ] [ : rows ] ) * img_size [ : main ] [ : cols ] ) . to_i
- end
- if 'auto' ! = width and 'auto' ! = height
- img_size [ : thumb ] [ : rows ] = height. to_i
- img_size [ : thumb ] [ : cols ] = width. to_i
- end
- img_new = img_orig. resize ! ( img_size [ : thumb ] [ : cols ] . to_i , img_size [ : thumb ] [ : rows ] . to_i )
- img_new. write "# {img_thumbs_dir} / # {img_thumbs}. # {img_type}"
- end
- img_thumbs = x
- Return "# {img_thumbs}. # {img_type}"
- end
- end
______________________
- $ img_size = array (
- 'main' => array ( 'width' => imagesx ( $ img_src ) , 'height' => imagesy ( $ img_src ) ) ,
- 'thumb' => array ( 'width' => 0 , 'height' => 0 )
- ) ;
- if ( 'auto' == $ width && 'auto' == $ height ) {
- $ img_size [ 'thumb' ] [ 'height' ] = ( int ) $ img_size [ 'main' ] [ 'height' ] ;
- $ img_size [ 'thumb' ] [ 'width' ] = ( int ) $ img_size [ 'main' ] [ 'width' ] ;
- }
- else if ( 'auto' ! = $ width && 'auto' == $ height ) {
- $ img_size [ 'thumb' ] [ 'width' ] = ( ( $ img_size [ 'main' ] [ 'width' ] <= $ width ) ? $ img_size [ 'main' ] [ 'width' ] : $ width ) ;
- $ img_size [ 'thumb' ] [ 'height' ] = ( int ) round ( ( $ img_size [ 'thumb' ] [ 'width' ] / $ img_size [ 'main' ] [ 'width' ] ) * $ img_size [ ' main ' ] [ ' height ' ] ) ;
- }
- else if ( 'auto' == $ width && 'auto' ! = $ height ) {
- $ img_size [ 'thumb' ] [ 'height' ] = ( ( $ img_size [ 'main' ] [ 'height' ] <= $ height ) ? $ img_size [ 'main' ] [ 'height' ] : $ height ) ;
- $ img_size [ 'thumb' ] [ 'width' ] = ( int ) round ( ( $ height / $ img_size [ 'main' ] [ 'height' ] ) * $ img_size [ 'main' ] [ 'width' ] ) ;
- }
- else if ( 'auto' ! = $ width && 'auto' ! = $ height ) {
- $ img_size [ 'thumb' ] [ 'height' ] = ( ( $ img_size [ 'main' ] [ 'height' ] <= $ height ) ? $ img_size [ 'main' ] [ 'height' ] : $ height ) ;
- $ img_size [ 'thumb' ] [ 'width' ] = ( ( $ img_size [ 'main' ] [ 'width' ] <= $ width ) ? $ img_size [ 'main' ] [ 'width' ] : $ width ) ;
- $ Kt = $ img_size [ 'thumb' ] [ 'height' ] / $ img_size [ 'thumb' ] [ 'width' ] ; // 5/1
- $ Km = $ img_size [ 'main' ] [ 'height' ] / $ img_size [ 'main' ] [ 'width' ] ; // 5/1
- if ( $ Kt > $ Km ) {
- $ img_size [ 'thumb' ] [ 'height' ] = $ img_size [ 'thumb' ] [ 'width' ] * $ Km ;
- }
- else if ( $ Kt < $ Km ) {
- $ img_size [ 'thumb' ] [ 'width' ] = $ img_size [ 'thumb' ] [ 'height' ] / $ Km ;
- }
- }
Source: https://habr.com/ru/post/53912/
All Articles