Fixed FF issue with image resize.

This commit is contained in:
Martin Edenhofer 2015-03-18 11:38:05 +01:00
parent ca7bf185b1
commit f3b9e612b0
3 changed files with 104 additions and 100 deletions

View file

@ -108,19 +108,18 @@ class Index extends App.Controller
storeImage: (src) => storeImage: (src) =>
# store avatar globally # store avatar globally
params = @oldDataUrl = src
avatar_full: src
# add resized image
avatar = new App.ImageService( src )
params['avatar_resize'] = avatar.toDataURLForAvatar( 'auto', 160 )
# store on server site # store on server site
store = (newDataUrl) =>
@ajax( @ajax(
id: 'avatar_new' id: 'avatar_new'
type: 'POST' type: 'POST'
url: @apiPath + '/users/avatar' url: @apiPath + '/users/avatar'
data: JSON.stringify( params ) data: JSON.stringify(
avatar_full: @oldDataUrl
avatar_resize: newDataUrl
)
processData: true processData: true
success: (data, status, xhr) => success: (data, status, xhr) =>
avatarHolder = $(App.view('profile/avatar-holder')( src: src, avatar: data.avatar ) ) avatarHolder = $(App.view('profile/avatar-holder')( src: src, avatar: data.avatar ) )
@ -128,6 +127,9 @@ class Index extends App.Controller
@pick avatarHolder.find('.avatar') @pick avatarHolder.find('.avatar')
) )
# add resized image
App.ImageService.resizeForAvatar( src, 'auto', 160, store )
onUpload: (event) => onUpload: (event) =>
callback = @storeImage callback = @storeImage
EXIF.getData event.target.files[0], -> EXIF.getData event.target.files[0], ->

View file

@ -25,10 +25,10 @@ class Index extends App.ControllerContent
# get data # get data
@ajax( @ajax(
id: 'getting_started', id: 'getting_started'
type: 'GET', type: 'GET'
url: @apiPath + '/getting_started', url: @apiPath + '/getting_started'
processData: true, processData: true
success: (data, status, xhr) => success: (data, status, xhr) =>
# redirect to login if master user already exists # redirect to login if master user already exists
@ -81,6 +81,11 @@ class Admin extends App.ControllerContent
processData: true processData: true
success: (data, status, xhr) => success: (data, status, xhr) =>
# check if user got created right now
#if true
# @navigate '#getting_started/base'
# return
# redirect to login if master user already exists # redirect to login if master user already exists
if @Config.get('system_init_done') if @Config.get('system_init_done')
@navigate '#login' @navigate '#login'
@ -251,26 +256,22 @@ class Base extends App.ControllerContent
submit: (e) => submit: (e) =>
e.preventDefault() e.preventDefault()
# get params
params = @formParam(e.target)
# add logo
params['logo'] = @logoPreview.attr('src')
# add resized image
if params['logo']
resizeLogo = new App.ImageService( params['logo'] )
params['logo_resize'] = resizeLogo.toDataURLForApp( @logoPreview.width(), @logoPreview.height() )
@hideAlerts() @hideAlerts()
@disable(e) @disable(e)
# get params
@params = @formParam(e.target)
# add logo
@params.logo = @logoPreview.attr('src')
store = (logoResizeDataUrl) =>
@params.logo_resize = logoResizeDataUrl
@ajax( @ajax(
id: 'getting_started_base' id: 'getting_started_base'
type: 'POST' type: 'POST'
url: @apiPath + '/getting_started/base' url: @apiPath + '/getting_started/base'
data: JSON.stringify( params ) data: JSON.stringify(@params)
processData: true processData: true
success: (data, status, xhr) => success: (data, status, xhr) =>
if data.result is 'ok' if data.result is 'ok'
@ -288,6 +289,9 @@ class Base extends App.ControllerContent
@enable(e) @enable(e)
) )
# add resized image
App.ImageService.resizeForAvatar( @params.logo, @logoPreview.width(), @logoPreview.height(), store )
hideAlerts: => hideAlerts: =>
@$('.form-group').removeClass('has-error') @$('.form-group').removeClass('has-error')
@$('.alert').addClass('hide') @$('.alert').addClass('hide')

View file

@ -1,20 +1,24 @@
class App.ImageService class App.ImageService
constructor: (url) ->
@orgDataURL = url
src: (url) => @resizeForAvatar: (dataURL, x, y, callback) =>
@orgDataURL = url if @checkUrl(dataURL)
callback(dataURL)
else
@resize( dataURL, x, y, 2, 'image/jpeg', 0.7, callback )
resize: ( x = 'auto', y = 'auto', sizeFactor = 1) => @toDataURLForApp: (dataURL, x, y, callback) =>
@canvas = document.createElement('canvas') if @checkUrl(dataURL)
context = @canvas.getContext('2d') callback(dataURL)
else
@resize( dataURL, x, y, 2, 'image/png', 0.7, callback )
@resize: ( dataURL, x = 'auto', y = 'auto', sizeFactor = 1, type, quallity, callback) =>
# load image from data url # load image from data url
imageObject = new Image() imageObject = new Image()
imageObject.src = @orgDataURL imageObject.onload = =>
imageWidth = imageObject.width imageWidth = imageObject.width
imageHeight = imageObject.height imageHeight = imageObject.height
if y is 'auto' && x is 'auto' if y is 'auto' && x is 'auto'
x = imageWidth x = imageWidth
y = imageHeight y = imageHeight
@ -32,28 +36,22 @@ class App.ImageService
x = x * sizeFactor x = x * sizeFactor
y = y * sizeFactor y = y * sizeFactor
# set canvas dimensions # create canvas and set dimensions
@canvas.width = x canvas = document.createElement('canvas')
@canvas.height = y canvas.width = x
canvas.height = y
# draw image on canvas and set image dimensions # draw image on canvas and set image dimensions
context = canvas.getContext('2d')
context.drawImage( imageObject, 0, 0, x, y ) context.drawImage( imageObject, 0, 0, x, y )
@canvas
checkUrl: => # execute callback with resized image
newDataUrl = canvas.toDataURL( type, quallity )
callback(newDataUrl)
# load image from data url
imageObject.src = dataURL
@checkUrl: (dataURL) ->
ignore = /\.svg$/i ignore = /\.svg$/i
ignore.test( @orgDataURL ) ignore.test( dataURL )
toDataURL: (type, quallity = 1) =>
#@resize()
@canvas.toDataURL( type, quallity )
toDataURLForAvatar: ( x, y ) =>
return if @checkUrl()
@resize( x, y, 2 )
@toDataURL( 'image/jpeg', 0.7 )
toDataURLForApp: ( x, y ) =>
return if @checkUrl()
@resize( x, y, 2 )
@toDataURL( 'image/png', 0.7 )