`
葉小兔
  • 浏览: 29326 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Convert png to gif

阅读更多

 

require "tempfile"
class Gif
  def self.convert_jpg_to_gif(arr)
    if arr.length == 1
      a = "#{Rails.root}/public/gif/1.jpeg"
      b = "#{Rails.root}/public/gif/2.jpeg"
      c = arr[0]
      d = "#{Rails.root}/public/gif/4.jpeg"
    elsif arr.length == 2
      a = arr[0]
      b = "#{Rails.root}/public/gif/2.jpeg"
      c = arr[1]
      d = "#{Rails.root}/public/gif/4.jpeg"
    elsif arr.length == 3
      a = arr[0]
      b = arr[1]
      c = arr[-1]
      d = "#{Rails.root}/public/gif/4.jpeg"
    elsif arr.length == 4
      a = arr[0]
      b = arr[1]
      c = arr[-1]
      d = arr[2]
    end
    system("convert #{b} -background 'rgba(0,0,0,0.0)' -rotate \"-20\" #{Rails.root}/public/gif/22.gif")
    system("convert #{d} -background 'rgba(0,0,0,0.0)' -rotate \"-20\" #{Rails.root}/public/gif/41.gif")

    system("composite -gravity NorthEast -geometry 18x18+102+32  #{a} #{Rails.root}/public/gif/1231.gif #{Rails.root}/public/gif/a.gif ")
    system("composite -gravity SouthWest -geometry 22x22+15+48   #{Rails.root}/public/gif/22.gif #{Rails.root}/public/gif/a.gif #{Rails.root}/public/gif/a.gif")
    system("composite -gravity SouthEast -geometry 26x26+86+26   #{c} #{Rails.root}/public/gif/a.gif #{Rails.root}/public/gif/a.gif ")  #71X26
    system("composite -gravity SouthEast -geometry 16x16+24+81   #{Rails.root}/public/gif/41.gif #{Rails.root}/public/gif/a.gif  #{Rails.root}/public/gif/b1.gif ")

    system("convert #{b} -background 'rgba(0,0,0,0.0)' -rotate \"+20\" #{Rails.root}/public/gif/22.gif")

    system("composite -gravity NorthEast -geometry 18x18+102+32  #{a}  #{Rails.root}/public/gif/1232.gif  #{Rails.root}/public/gif/a1.gif ")
    system("composite -gravity SouthWest -geometry 22x22+19+49   #{Rails.root}/public/gif/22.gif #{Rails.root}/public/gif/a1.gif  #{Rails.root}/public/gif/a1.gif ")
    system("composite -gravity SouthWest -geometry 26x26+71+26   #{c} #{Rails.root}/public/gif/a1.gif  #{Rails.root}/public/gif/a1.gif ")  #71X26
    system("composite -gravity SouthEast -geometry 16x16+24+81   #{Rails.root}/public/gif/41.gif #{Rails.root}/public/gif/a1.gif  #{Rails.root}/public/gif/b2.gif ")

    system("convert -delay 100 -dispose Background #{Rails.root}/public/gif/b1.gif  #{Rails.root}/public/gif/b2.gif -loop 0 #{Rails.root}/public/gif/b3.gif")
    "#{Rails.root}/public/gif/b3.gif"
  end
end
require "tempfile"
class Gif
  def self.convert_jpg_to_gif(arr, work_image_path)
    if arr.length == 1
      a = "#{Rails.root}/public/gif/1.jpeg"
      b = "#{Rails.root}/public/gif/2.jpeg"
      c = arr[0]
      d = "#{Rails.root}/public/gif/4.jpeg"
    elsif arr.length == 2
      a = arr[0]
      b = "#{Rails.root}/public/gif/2.jpeg"
      c = arr[1]
      d = "#{Rails.root}/public/gif/4.jpeg"
    elsif arr.length == 3
      a = arr[0]
      b = arr[1]
      c = arr[-1]
      d = "#{Rails.root}/public/gif/4.jpeg"
    elsif arr.length == 4
      a = arr[0]
      b = arr[1]
      c = arr[-1]
      d = arr[2]
    end

    t11 = Tempfile.new(["t11",".png"], "#{Rails.root}/public/temp")   #temofile中:.png指定格式后可以生成背景透明的图片
    system("convert #{a} -background 'rgba(0,0,0,0.0)' -rotate \"6\" #{t11.path}")
    t21 = Tempfile.new(["t21",".png"], "#{Rails.root}/public/temp")
    system("convert #{b} -background 'rgba(0,0,0,0.0)' -rotate \"-18\" #{t21.path}")
    t31 = Tempfile.new(["t31",".png"], "#{Rails.root}/public/temp")
    system("convert #{c} -background 'rgba(0,0,0,0.0)' -rotate \"7\" #{t31.path}")
    t41 = Tempfile.new(["t41",".png"], "#{Rails.root}/public/temp")
    system("convert #{d} -background 'rgba(0,0,0,0.0)' -rotate \"11\" #{t41.path}")

    ta1 = Tempfile.new(["ta1",".png"], "#{Rails.root}/public/temp")
    tb1 = Tempfile.new(["tb1",".png"], "#{Rails.root}/public/temp")
    system("composite -gravity NorthEast -geometry 44x44+93+25   #{t11.path} #{Rails.root}/public/gif/b3.png #{ta1.path}")
    system("composite -gravity SouthWest -geometry 53x53+16+39   #{t21.path} #{ta1.path} #{ta1.path}")
    system("composite -gravity SouthEast -geometry 54x54+91+44   #{t31.path} #{ta1.path} #{ta1.path} ")
    system("composite -gravity SouthEast -geometry 44x44+25+92   #{t41.path} #{ta1.path}  #{tb1.path} ")

    system("convert #{a} -background 'rgba(0,0,0,0.0)' -rotate \"-14\" #{t11.path}")
    system("convert #{b} -background 'rgba(0,0,0,0.0)' -rotate \"+10\" #{t21.path}")
    system("convert #{c} -background 'rgba(0,0,0,0.0)' -rotate \"-11\" #{t31.path}")
    system("convert #{d} -background 'rgba(0,0,0,0.0)' -rotate \"-15\" #{t41.path}")

    ta2 = Tempfile.new(["ta2",".png"], "#{Rails.root}/public/temp")
    tb2 = Tempfile.new(["tb2",".png"], "#{Rails.root}/public/temp")
    system("composite -gravity NorthEast -geometry 48x48+93+22   #{t11.path} #{Rails.root}/public/gif/b2.png #{ta2.path}")
    system("composite -gravity SouthWest -geometry 48x48+22+38   #{t21.path} #{ta2.path} #{ta2.path}")
    system("composite -gravity SouthWest -geometry 55x55+125+41   #{t31.path} #{ta2.path} #{ta2.path} ")
    system("composite -gravity SouthEast -geometry 47x47+23+91   #{t41.path} #{ta2.path}  #{tb2.path} ")

    system("convert #{a} -background 'rgba(0,0,0,0.0)' -rotate \"+16\" #{t11.path}")
    system("convert #{b} -background 'rgba(0,0,0,0.0)' -rotate \"+3\" #{t21.path}")
    system("convert #{c} -background 'rgba(0,0,0,0.0)' -rotate \"-11\" #{t31.path}")
    system("convert #{d} -background 'rgba(0,0,0,0.0)' -rotate \"+11\" #{t41.path}")

    ta3 = Tempfile.new(["ta3",".png"], "#{Rails.root}/public/temp")
    tb3 = Tempfile.new(["tb3",".png"], "#{Rails.root}/public/temp")
    system("composite -gravity NorthEast -geometry 50x50+89+20   #{t11.path}  #{Rails.root}/public/gif/b1.png  #{ta3.path} ")
    system("composite -gravity SouthWest -geometry 44x44+24+40   #{t21.path} #{ta3.path}  #{ta3.path} ")
    system("composite -gravity SouthWest -geometry 55x55+98+41   #{t31.path} #{ta3.path}  #{ta3.path} ")
    system("composite -gravity SouthEast -geometry 45x45+20+91   #{t41.path} #{ta3.path}  #{tb3.path} ")

    path = work_image_path.gsub(/\d+\.jpg/, "#{Time.now.to_i}.gif")
    result = File.new(path, 'w')
    tb4 = Tempfile.new(["tb4",".gif"], "#{Rails.root}/public/temp") #此处必须声明.gif后缀负责converet将会报错,命令不是别temp文件
    system("convert -delay 100 -dispose Background #{tb1.path} #{tb2.path}  #{tb3.path} -loop 0 #{tb4.path}")
    system("convert -dispose none -delay 0 #{work_image_path} -dispose previous -delay 100 #{tb4.path} -loop 0 #{path}")
    [t11,t21,t31,t41, ta1, tb1, ta2, ta3,tb2,tb3, tb4].each do |tempfile|
      tempfile.delete
    end
    path
  end
end
* 不加.png 和.gif在服务器上运行没有错误,也生成了想要的图片,为什么clone下来后就不行了呢?和tempfile类相关还是和converet中gif这个带帧的格式相关呢?
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics