Code này là đơn giản nhất trong các code úp ảnh mà diễn đàn đã share . Chỉ cần chọn thẳng ảnh trong máy để up , khi úp xong sẽ tự động hiện link ảnh thẳng vào bài viết . Nhược điểm là phải úp từng lượt và không úp được bằng url .
Vào modules >> HTML & JAVASCRIPT >> Javascript codes management
Tạo js này và click vào In all the page
Thay client ID đã tạo ở trang Imgur vào chữ 'Thay ID ở đây' dòng 3 của Js
Nếu chưa biết tạo Client ID thì vào đọc hướng dẫn ở topic này :
http://www.hotrofm.net/t304-code-up-anh-len-imgur-mien-phi
Tags: #code #imgur #bbcode
BƯỚC 1: TẠO JS THAY THẾ
Vào modules >> HTML & JAVASCRIPT >> Javascript codes management
Tạo js này và click vào In all the page
Code:
;window['FormData'] && $(function(){$(function(){
var imgur_client_id = 'Thay ID ở Đây'; // you will get it via http://api.imgur.com/oauth2/addclient
var current = 0, uploaded, xhrs, file_selector, button = $('.sceditor-button-servimg').off().click(function(){
if(current) return finish();
if(!file_selector) file_selector = $('<input type="file" multiple/ accept="image/*">').css({position:'absolute', top:-100}).appendTo('body').change(function (e) {
var files = e.target.files, fd, len;
uploaded = [], xhrs = [];
for (var i = 0, len = files.length; i < len; i += 1) {
if (files[i].type.indexOf('image/') && files[i].type !== 'application/pdf') continue;
current++;
fd = new FormData();
fd.append('image', files[i]);
(function(xhr){
var num = i, xhr = jQuery.ajaxSettings.xhr();
xhrs.push(xhr);
xhr.open('POST', 'https://api.imgur.com/3/image');
xhr.setRequestHeader('Authorization', 'Client-ID '+imgur_client_id);
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4) return;
current--;
if (xhr.status === 200) {
var res = JSON.parse(xhr.responseText);
uploaded[num] = res.data.link;
}
if(!current) finish();
};
xhr.send(fd);
})();
}
if(current) button.css({background:'url(http://i.imgur.com/EMsOJtZ.gif) no-repeat'});
});
file_selector.click();
return false;
}).children();
var finish = function(){
for(var i=0; i < xhrs.length; i++) {
if(xhrs[i].readyState == 4) continue;
xhrs[i].onreadystatechange = function(){};
xhrs[i].abort();
}
for(var i=0; i < uploaded.length; i++) {
if(uploaded[i] === undefined) continue;
$('body').find('.sceditor-button-image').click().end().find('.sceditor-insertimage').find('#image').val(uploaded[i]).end().find('input.button').click();
$('#text_editor_textarea').sceditor('instance').insertText('\n');
}
button.removeAttr('style');
current = 0;
file_selector.wrap('<form>').closest('form').get(0).reset();
file_selector.unwrap();
return false;
};
})});
BƯỚC 2: THAY CLIENT ID VÀO JS
Thay client ID đã tạo ở trang Imgur vào chữ 'Thay ID ở đây' dòng 3 của Js
Nếu chưa biết tạo Client ID thì vào đọc hướng dẫn ở topic này :
http://www.hotrofm.net/t304-code-up-anh-len-imgur-mien-phi
Nguồn : fmdesign
Tags: #code #imgur #bbcode