首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RMAGICK:调整和更改图片的文件类型(在运行中)...不正确的图像标题错误

RMAGICK:调整和更改图片的文件类型(在运行中)...不正确的图像标题错误
EN

Stack Overflow用户
提问于 2010-03-09 01:49:51
回答 1查看 2K关注 0票数 0

这是关于RMagick和Ruby On Rails的。

我执行以下操作:

代码语言:javascript
复制
  image = params[:image]
  # params[:image] is the image from the file-form.
  name = image.original_filename.scan(/[^\/\\]+/).last
  name = dir + t.day.to_s + t.month.to_s + t.year.to_s + t.hour.to_s + t.min.to_s + t.sec.to_s + name
  f = File.new(name, "wb")
  f.write image
  f.close
  image = Magick::Image.read(name)
  image = image.resize_to_fit(200, 250)
  f = File.new(name, "wb")
  f.write image.to_blob
  f.close

我真的需要先保存再更改吗?如果不仅改变文件大小,还改变文件类型呢?我想要一个60%质量的JPG。

这个错误是什么意思?

代码语言:javascript
复制
Magick::ImageMagickError (Improper image header `public/images/avatars/Joern/83201018458ich2_kleiner.png'):

请帮帮我。

你的,乔恩。

EN

回答 1

Stack Overflow用户

发布于 2010-03-09 03:29:02

对于任何感兴趣的人,这是解决方案:

代码语言:javascript
复制
  t = Time.now
  if !File.directory?(dir = "public/images/avatars/#{self.current_user.login}/")
    Dir.mkdir(dir)
  end
  image = params[:image]
  basename = t.day.to_s + t.month.to_s + t.year.to_s + t.hour.to_s + t.min.to_s + t.sec.to_s + ".jpg"
  name = dir + basename
  image = Magick::Image.from_blob(image.read).last
  image = image.resize_to_fit(200, 250)
  image.write(name) do self.quality = 60 end
  picture = Picture.new
  picture.path = "avatars/" + self.current_user.login + "/" + basename
  picture.user_id = self.current_user.id
  picture.save
  redirect_to :action => :show, :id => picture.id
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2403390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档