Skip to content

Instantly share code, notes, and snippets.

@redwraith2
Created July 10, 2015 00:41
Show Gist options
  • Save redwraith2/a508134fd3b3e20405c9 to your computer and use it in GitHub Desktop.
Save redwraith2/a508134fd3b3e20405c9 to your computer and use it in GitHub Desktop.
readURL = (input, template, id) ->
filesToUpload = input.files
file = filesToUpload[0]
# Create an image
img = document.createElement('img')
# Create a file reader
reader = new FileReader
# Set the image once loaded into file reader
reader.onload = (e) ->
img.src = e.target.result
canvas = document.createElement('canvas')
ctx = canvas.getContext('2d')
ctx.drawImage img, 0, 0
MAX_WIDTH = 1024
MAX_HEIGHT = 1024
width = img.width
height = img.height
if width > height
if width > MAX_WIDTH
height *= MAX_WIDTH / width
width = MAX_WIDTH
else
if height > MAX_HEIGHT
width *= MAX_HEIGHT / height
height = MAX_HEIGHT
canvas.width = width
canvas.height = height
ctx = canvas.getContext('2d')
ctx.drawImage img, 0, 0, width, height
dataurl = canvas.toDataURL('image/png')
crop(dataurl, template, id)
# Load files into file reader
reader.readAsDataURL file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment