[Rails] Image upload problems

Ben Schumacher benschumacher at gmail.com
Tue Jan 4 22:20:02 GMT 2005


I have seen cases where images (JPEGs, specifically) from some digital
cameras have troubles being read by various open source image packages
(in my case it was libgd). Something about the image's footer not
being quite right.

I'm not sure this is even remotely related to your problem, but it
might be something to check out. Here's some code that fixed it in PHP
(which should be relatively easy to port to ruby -- I just haven't
done it):

    function fixJPEG($jpegFile)
    {
        $fp = fopen($jpegFile, 'r+b');
        if (fread($fp, 2) == (chr(255) . chr(216)))
        {
            fseek($fp, -2, SEEK_END);
            if (fread($fp, 2) == (chr(255) . chr(217)))
            {
                fclose($fp);
            }
            else
            {
                fwrite($fp, (chr(255) . chr(217)));
                fclose($fp);
            }
        }
    }

If you don't know PHP well enough to understand what that does, write
me back and I'll do a rewrite in ruby (although not until later...
don't have time to write and test it right now).

Cheers,

Ben


On Tue, 4 Jan 2005 11:41:47 -0800, Berndt Jung <berndtj at gmail.com> wrote:
> So, like others on this list, I have started a picture store app.  I
> have followed the directions in the "how to" section for file uploads,
> as well as the previous posters here.  Basically, it seems as though
> it should be working, but the images become corrupt.  I can view them,
> but they look awful.  I tried resizing and formatting with RMagick
> (really cool extention), but it complained about the files I was
> sending it.  So now I'm pretty sure that either ruby/rails is
> corrupting the files, or mysql.
> 
> Anyone having similar issues?
> 
> Thanks,
> 
> Berndt
> 
> MySql.dump
> 
> CREATE TABLE `photos` (
>   `id` int(11) unsigned NOT NULL auto_increment,
>   `name` varchar(127) default NULL,
>   `size` int(11) default NULL,
>   `mime` varchar(127) default NULL,
>   `picture` mediumblob NOT NULL,
>   PRIMARY KEY  (`id`),
>   KEY `name` (`name`)
> ) TYPE=MyISAM
> 
> rails code:
> 
> def create
>    #render_text @params['photo']['tmp_file'].local_path
>    @photo = Photo.new do |p|
>      p.name = @params['photo']['tmp_file'].original_filename.gsub(/[^a-zA-Z0-9.]/,
> '_')
>      p.size = @params['photo']['tmp_file'].size
>      p.mime = @params['photo']['tmp_file'].content_type
>      p.picture = @params['photo']['tmp_file'].read
>   end
>    @params.delete("photo" => "tmp_file")
>    if @photo.save
>       redirect_to :action => "show/" + @photo.id.to_s
>    else
>       render "photo/edit"
>    end
>   end
> 
> def show
>     #@image = Photo.find(@params['id'])
>     #send_data @image.picture, :filename => @image.name, :type =>
> @image.mime, :disposition => "inline"
>     @image = Photo.find(@params["id"])
>                 @response.headers["Content-type"] = @image.mime
>                 render_text  @image.picture
>   end
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


More information about the Rails mailing list