Giter Site home page Giter Site logo

image-uploads's Introduction

A solution to uploading photos in Meteor.js

Note: I haven't finished the unit-tests for this yet ๐Ÿ˜ซ
but its usable in all HTML5 browsers (so 9<IE). This works great for mobile devices and desktop (Safri, Chrome and Firefox) but requires a Non-HTML5 Fallback for IE9 and below. So if you need to support older browsers, either help me with the IE work (fork+send a pull request!) or look for an alternative. Thanks. :-)

Features

  • Uses config.js file for S3 Credentials (so we aren't leaking info)

You will need to cp private/config-example.json private/config.json and update your S3 details

Update: I've abstracted the upload part of this to Stream the File to S3 (much faster)
that has a separate module: https://github.com/nelsonic/stream-to-s3 (fully tested / 100% coverage)
Only a couple more steps to get this all wired up! :-)

Why?

Helping people communicate in pictures makes their lives better.

What?

At present there is no "right way" of uploading images in Meteor.js

This example image uploader

  • Uploads images (to S3) effortlessly
  • Use ImageMagic to automatically re-size & optimise the image for mobile devices
  • Return a list of the images that were uploaded to S3 for use in other objects.
  • Displays the photo inline on without refreshing* the page
  • Shows a Progress Bar to improve User Experience
  • Styled upload button gives "native-like" experience

Results

Image Uplaod

Try it!

On your local machine, clone this repository

git clone [email protected]:ideaq/image-uploads.git

If you do not already have node.js or meteor installed, you will need to install:

Once all of those are installed, you will need to:

  1. Run meteorite to download the two dependencies $ mrt
  2. Update update the Amazon Web Services credentials in server_save_file.js
  3. Run the app!
$ meteor

Visit: http://localhost:3000/

And start uploading!

How? (implementation details)

I've included

Using NPM Modules in Meteor

Instead of using an untested meteor (wrapper) module I'm using https://github.com/arunoda/meteor-npm which allows us to use the actual npm module inside meteor.

npm install -g meteor-npm

then create a packages.json file (note the s in packages)

{
	"imagemagick": "0.1.3",
	"knox"       : "0.8.9",
	"tmp"        : "0.0.23"
}

Install dependencies:

mrt

Read more: http://meteorhacks.com/complete-npm-integration-for-meteor.html

Uploading to S3 using AWS SDK

Before Amazon decided to support Node.js the go-to module for S3 was Knox: https://github.com/LearnBoost/knox

Meteor example: https://gist.github.com/dnprock/6689567 Learn from the tests: https://github.com/LearnBoost/knox/blob/master/test/createClient.test.js

Latest version: https://www.npmjs.org/package/knox (for your packages.json)

The (*Official*) Amazon Web Services (AWS) SDK https://github.com/aws/aws-sdk-js

Utterly Useless!! :-(
http://stackoverflow.com/questions/14502143/cant-upload-images-in-nodejs-using-asw-sdk

Latest version check: https://www.npmjs.org/package/aws-sdk

Usage Examples: http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-examples.html

Image Re-Sizing using ImageMagic

On a Mac you will need Homebrew to install ImageMagic (the easy & fast way.)

brew install imagemagick

Now install node-imagemagick (already done above the first time you ran mrt)

Usage: https://github.com/rsms/node-imagemagick Latest version: https://www.npmjs.org/package/imagemagick

Image Rotation

We started using this code in staging today and users started uploading images imediately.

"Its faster than Instagram!" ~ A User

Was a user comment that delighted us.

However they uploaded images from their iDevices which were not in the correct orientation...

So we need to add rotation.

Assumptions

Aproximately 90% of people are right-handed.

iphone landscape right-handed

Therefore we need to rotate images 90 degrees clockwise.

Investigation

ImageMagic has the ability to rotate images:

However the source-code for node imagemagic does not expose the distort method:

https://github.com/rsms/node-imagemagick/blob/master/imagemagick.js

There is no mention of distortion/rotation. :-(

Alternatives > GraphicsMagic

Understanding JPEG Exif Orientation

For convenience, here is what the letter F would look like if it were tagged correctly and displayed by a program that ignores the orientation tag (thus showing the stored image):

  1          2         3        4         5                 6           7            8

888888    888888        88    88        8888888888    88                   88   8888888888
88            88        88    88        88  88        88  88           88  88       88  88
8888        8888      8888    8888      88            8888888888   8888888888           88
88            88        88    88
88            88    888888    888888

Temporary Folder / Files

Usage: https://github.com/raszi/node-tmp
Latest version: https://www.npmjs.org/package/tmp

Research (way more detail than you'll ever need)

Image-upload-as-a-Service Provider

How are others solving the problem?

There was a suggestion to use Meteor CollectionFS: https://github.com/CollectionFS/Meteor-CollectionFS to upload/store images. This is a terrible idea because the file gets stored in MongoDB! Not only is MongoDB storage considerably more expensive, its also slower than a CDN!! (If you know why this is a good idea, please tell me!)

Avoid Filename Collisions with Hashes

All the big players are storing files with generated names (instead of using the files' original name such as "photo1.jpg" thus avoiding name collisions) e.g:

Google is maintaining the file name and storing it in a folder:

Should we maintain the filename? I tend to re-name all the photos I want to be able find later. So does giving these hash.jpg on our system loose some valuable information...? (feedback please...!)

Helpful Links

Troubleshooting

History

I've partially "solved" this problem before:

I need to re-visit these and get them working together (with tests :-)

Popular Image Web/Mobile Sites/Apps

The more successful image apps on the web/mobile:

Next

Try: https://github.com/aheckmann/gm (instead of node-imagemagick)

image-uploads's People

Contributors

nkamc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.