diff --git a/app/controllers/avatar_controller.rb b/app/controllers/avatar_controller.rb index 8408e54c6..b9a166a6e 100644 --- a/app/controllers/avatar_controller.rb +++ b/app/controllers/avatar_controller.rb @@ -11,9 +11,24 @@ class AvatarController < ApplicationController @source_type = params[:source_type] @source_id = params[:source_id] @temp_file = params[:img] || params[:avatar][:image] - @image_file = @temp_file.original_filename + + if @temp_file.respond_to?(:original_filename) + @image_file = @temp_file.original_filename + #image_file.force_encoding("UTF-8") if filename.respond_to?(:force_encoding) + else + @image_file=params[:filename] + end + @is_direct = params[:is_direct] @auth_type = params[:auth_type] + + + #base64转换 + img_base64_head = 'data:image/jpeg;base64,' + if @temp_file && @temp_file.start_with?(img_base64_head) + @temp_file = StringIO.new(Base64.decode64(@temp_file[img_base64_head.size,@temp_file.size-img_base64_head.size])) + end + else unless request.raw_post.nil? @source_type = params[:source_type] @@ -33,6 +48,8 @@ class AvatarController < ApplicationController end end + + size = @temp_file.size if @temp_file && (@temp_file.size > 0) if @temp_file.size > Setting.upload_avatar_max_size.to_i @@ -85,6 +102,7 @@ class AvatarController < ApplicationController else @status = 2 @msg = l(:not_valid_image_file) + logger.error "上传失败: "+@msg end end @temp_file = nil diff --git a/app/views/account/avatar.html.erb b/app/views/account/avatar.html.erb index 43a4e9dee..911eef99d 100644 --- a/app/views/account/avatar.html.erb +++ b/app/views/account/avatar.html.erb @@ -69,68 +69,88 @@ \ No newline at end of file diff --git a/app/views/layouts/base_edu_account.html.erb b/app/views/layouts/base_edu_account.html.erb index 27a709780..085bc1e5d 100644 --- a/app/views/layouts/base_edu_account.html.erb +++ b/app/views/layouts/base_edu_account.html.erb @@ -1,5 +1,5 @@ - - + + <%= h html_title %> diff --git a/public/head/sitelogo.js b/public/head/sitelogo.js index 71aa947a2..cb607e1d2 100644 --- a/public/head/sitelogo.js +++ b/public/head/sitelogo.js @@ -15,7 +15,13 @@ log: function() {} }; - function CropAvatar($element) { + /** + * 图片裁剪 + * @param $element + * @param aspectRatio 支持等比 1 4/3 16/9 + * @constructor + */ + function CropAvatar($element, aspectRatio) { this.$container = $element; this.$avatarView = this.$container.find('.avatar-view'); @@ -34,6 +40,8 @@ this.$avatarWrapper = this.$avatarModal.find('.avatar-wrapper'); this.$avatarPreview = this.$avatarModal.find('.avatar-preview'); + this.$aspectRatio = aspectRatio; + this.init(); } @@ -197,7 +205,7 @@ this.$img = $(''); this.$avatarWrapper.empty().html(this.$img); this.$img.cropper({ - aspectRatio: 1, + aspectRatio: _this.$aspectRatio, preview: this.$avatarPreview.selector, strict: false, // crop: function(data) { @@ -224,49 +232,6 @@ } }, -// ajaxUpload: function() { -// var url = this.$avatarForm.attr('action'), -// data = new FormData(this.$avatarForm[0]), -// _this = this; -// -// $.ajax(url, { -// headers: { -// 'X-XSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') -// }, -// type: 'post', -// data: data, -// dataType: 'json', -// processData: false, -// contentType: false, -// -// beforeSend: function() { -// _this.submitStart(); -// }, -// -// success: function(data) { -// _this.submitDone(data); -// }, -// -// error: function(XMLHttpRequest, textStatus, errorThrown) { -// if (this.uploaded) { -// this.uploaded = false; -// this.cropDone(); -// // this.uploaded = true;this.support.datauri || -// // this.$avatarSrc.val(this.url); -// // this.startCropper(); -// } else { -// this.uploaded = true; -// this.$avatarSrc.val(this.url); -// this.startCropper(); -// this.cropDone(); -// } -// }, -// -// complete: function() { -// _this.submitEnd(); -// } -// }); -// }, syncUpload: function() { this.$avatarSave.click(); @@ -276,27 +241,6 @@ this.$loading.fadeIn(); }, -// submitDone: function(data) { -// if($.isPlainObject(data)) { -// if(data.result) { -// this.url = data.result; -// if(this.support.datauri || this.uploaded) { -// this.uploaded = false; -// this.cropDone(); -// } else { -// this.uploaded = true; -// this.$avatarSrc.val(this.url); -// this.startCropper(); -// } -// this.$avatarInput.val(''); -// } else if(data.message) { -// this.alert(data.message); -// } -// } else { -// this.alert('Failed to response'); -// } -// }, - submitFail: function(msg) { this.alert(msg); }, @@ -324,8 +268,6 @@ } }; - $(function() { - return new CropAvatar($('#crop-avatar')); - }); + window.CropAvatar = CropAvatar; }); \ No newline at end of file