2014-09-05 48 views
-1

我需要驗證圖像尺寸,然後將其上傳到cloudinary,因爲我使用的是直接從表單上傳。如何在上傳之前檢索圖像尺寸cloudinary

$(".cloudinary-fileupload").fileupload(
# disableImageResize: false, 
imageMinWidth : 460 
imageMinHeight: 230 
acceptFileTypes: /(\.|\/)(gif|jpeg|png|bmp|ico)$/i, 
maxFileSize: 2097152, 
processalways: (e,data) -> 
if (data.files.error) 
alert(data.files[0].error) 

# dropZone: "#direct_upload" 
start: (e) -> 
$(".status").text "Starting upload..." 
return 
progress: (e, data) -> 
$(".status").text "Uploading... " + Math.round((data.loaded * 100.0)/data.total) + "%" 
return 
fail: (e, data) -> 
$(".status").text "Upload failed" 
return 

這是用於上傳我的jQuery文件....

,這是用於上傳到cloudinary圖像視圖文件...

<%= cl_image_upload_tag(:image_id, 
:html => {:id => "resource_image", :class => "hidden"}, 

:tags => "directly_uploaded", 
:crop => :scale, 
:maxFileSize => 2048, 
:resource_type => "image", 
:format => 'jpg', 
:cloudinarydone => true, 
:notification_url => new_image_url 

) %> 

請告訴我,如何實現所需的功能,因爲如果尺寸大於460,230,我想要上傳圖像,否則會爲用戶生成警報,以更改他選擇的圖片。

回答

0

使用jQuery文件上傳上傳本地文件時,可以獲得尺寸。 你應該結合fileuploadprocessalways有以下幾點:

function (e, data) {  
var width = data.img.width; 
var height = data.img.height; 
} 

您還需要包括必需的jQuery文件上傳的所有額外的JS庫,可能打開一些jQuery的文件上傳選項,例如:

previewThumbnail: false 
previewMaxWidth: 1000 
previewMaxHeight: 1000 
+0

fileuploadprocessalways :(e,data) - > width = data.img.width height = data.img.height console.log height //這是我的咖啡腳本文件,我正在使用您的代碼,但在控制檯上它仍然不打印任何高度,而且當放置進行中的功能說s不能得到未定義的屬性。 – 2014-09-12 04:32:53

+0

您應該確保包含所有相關庫。請參閱:https://github.com/cloudinary/cloudinary_js#client-side-image-resizing-before-upload – 2014-09-14 08:44:07