Created
August 2, 2014 10:06
-
-
Save zmajstor/18b1da3f4d981385b489 to your computer and use it in GitHub Desktop.
rails-carrierwave-aviary image upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Load Feather code --> | |
<!-- <script type="text/javascript" src="http://feather.aviary.com/js/feather.js"></script> --> | |
<script type="text/javascript" src="https://dme0ih8comzn4.cloudfront.net/js/feather.js"></script> | |
<!-- Instantiate Feather --> | |
<script type='text/javascript'> | |
var featherEditor = new Aviary.Feather({ | |
apiKey: 'API_KEY', | |
// apiKey: '<%= ENV['AVIARY_KEY'] %>', | |
apiVersion: 3, | |
// enableCORS: true, | |
theme: 'minimum', // Check out our new 'light' and 'dark' themes! | |
tools: 'all', | |
appendTo: '', | |
onSave: function(imageID, newURL) { | |
// var img = document.getElementById(imageID); | |
// img.src = newURL; | |
img = $('#' + imageID); | |
img.attr('src', newURL); | |
img.siblings('input[id$=_url]').val(newURL); | |
img.siblings('span.label-warning').show(); | |
$('.label-danger').show(); | |
featherEditor.close(); | |
// alert('edited image temp url: ' + newURL); | |
}, | |
onError: function(errorObj) { | |
alert(errorObj.message); | |
} | |
}); | |
function launchEditor(id, src) { | |
var title = $('#' + id).data('title'); | |
var ratio = $('#' + id).data('ratio'); | |
// var size = $('#' + id).data('size'); | |
featherEditor.launch({ | |
image: id, | |
url: src, | |
hiresUrl: src, | |
fileFormat: 'png', | |
// maxSize: size, | |
maxSize: 800, | |
// cropPresets: ['131:155'], | |
// initTool: 'crop', | |
forceCropPreset: [title, ratio], | |
forceCropMessage: 'Please crop your photo to this aspect ratio' | |
}); | |
return false; | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for @user do |f| %> | |
<div> | |
<%= f.label :image %>: | |
<%= f.file_field :image, accept: "image/gif,image/jpeg,image/png" %> | |
<%= f.hidden_field :remote_image_url %> | |
<%#= f.hidden_field :image_cache %> | |
<span class="label label-warning">Note: confirm image change with save button below</span> | |
<%= image_tag(f.object.image_url, width: 615, id: 'image', data: { title: 'Image', ratio: '4:3', size: '700' }) %> | |
<% if @user.image? %> | |
<small>Preview @ 400 x 300 px</small> | |
<button class="btn btn-default btn-sm" onclick="return editImage('image');">Edit image</button> | |
| |
<label><%= f.check_box :remove_image %> Remove uploaded image</label> | |
<% end %> | |
</div> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
$('input[type=file]').each(function() { | |
$(this).change(function() { | |
if(this.files!=undefined) { | |
loadImage(this.files[0], $(this).siblings('img:first')); | |
} | |
else { | |
alert('Your Browser does not support HTML5 uploads! Please install newer browser like Chrome'); | |
} | |
}); | |
}); | |
$("form").submit(function() { | |
$(this).find('input[type=file]').remove(); | |
return true; // ensure form still submits | |
}); | |
}); | |
function loadImage(file, image) { | |
var reader; | |
try { | |
reader = new FileReader(); | |
var max_file_size = 1048576 * 4; | |
if (file.size > max_file_size) { | |
alert('File size is too big, limit under 4MB'); | |
return; | |
} | |
} catch (err) { | |
// alert('Your Browser does not support HTML5 uploads! Please install newer browser like Chrome'); | |
alert('No file chosen (or your Browser does not support HTML5 uploads)'); | |
return; | |
} | |
reader.onload = function(e){ | |
// e.target.result holds the DataURL which can be used as a source of the image: | |
// image.attr('src',e.target.result); | |
// image.attr('width','220'); | |
// image.attr('height','192'); | |
launchEditor(image.attr('id'), e.target.result); | |
} | |
// Reading the file as a DataURL. When finished, this will trigger the onload function above: | |
reader.readAsDataURL(file); | |
} | |
function editImage(id) { | |
launchEditor(id, $('#' + id).attr('src')); | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment