Useko Netsumi wrote:
> Hi, is there any Ruby code snippets I can use to transform my photo to lower
> resolution, uniform size but maintaining aspect ration, etc ...?
> 
> Thanks
> 
> 
> 
> 
I use GD and this lil' code, it keeps aspect ratio and ONLY works on jpg 
and png, since you say you use php, this could look familiar, the code 
could be *better* but does what I want use it or take it as reference, 
usage of copyResampled would give better output but haven't been able to 
use it.

*warning nuby code ahead*

require "GD"
class Thumber
   #imgpath=Full path to src image         i.e. 
"/home/osuka/pics/good_looking_me.jpg"
   #thumb_dir=dir path ro dest directory   i.e. "/home/osuka/backups/" 
*notice* trailing /
   #newx=new image width                   i.e. 320
   #newy=new image height                  i.e. 240
   #prefix=prefix to add to given image    i.e. "thumb_"
   #type=image type of uotput thumb        i.e. "jpg"|png|gif <-depends 
on GD image write support this particular code
   #uses only jpg and png, haven't need anything else, or in gif case 
can modified it to use an external app like Magick.
   #or gif2png
   def initialize(imgpath,thumb_dir,newx,newy,prefix,type)
     @prefix=prefix
     @imgpath=imgpath
     @thumb_dir=thumb_dir
     @type=type

     if(@imgpath.downcase=~/[a-zA-Z0-9]+.(jpg||jpeg)+/)
       #comment out if noisy
       puts @srctype="jpeg"
       @srcimg=GD::Image.new_from_jpeg(imgpath)
       @ratio=@srcimg.width.to_f/@srcimg.height
     elsif(@imgpath.downcase=~/[a-zA-Z0-9]+.(png)+/)
       #comment out if noisy
       puts @srctype="png"
       @srcimg=GD::Image.new_from_png(imgpath)
       @ratio=@srcimg.width.to_f/@srcimg.height
     elsif(@imgpath.downcase=~/[a-zA-Z0-9]+.(gif)+/)
       #comment out if noisy
       puts @srctype="gif"
       @srcimg=GD::Image.new_from_gif(imgpath)
       @ratio=@srcimg.width.to_f/@srcimg.height
     else
       #comment out if noisy
       puts "other"
     end
     @newx=newx
     @newy=newy
     #comment out if noisy
     puts @ratio
     if(@newy>@newx)
       @newy=(@newx/@ratio).round
     else
       puts @newx=(@newy*@ratio).round
     end

     if(File.exists?(@thumb_dir+@prefix+File.basename(@imgpath)))
       #comment out if noisy
       puts "file exist"
     else
       #comment out if noisy
       puts "file doesn't exist"
       @dstimg=GD::Image.new(@newx,@newy)
 
@srcimg.copyResized(@dstimg,0,0,0,0,@newx,@newy,@srcimg.width,@srcimg.height)
       @thumbf=File.new(@thumb_dir+@prefix+File.basename(@imgpath),"wb")
       if(@type=="jpg")
         @dstimg.jpeg(@thumbf,85)
       elsif(@type=="png")
         @dstimg.png(@thumbf)
       else(@type=="gif")
         #@dstimg.gif(thumbf,85)
       end
     end

   end
end

#example usage:
#img1="e:/adartse/DNA.JPG"
#mypic=Thumber.new(img1,"e:/temp/",80,100,"","jpg")