Add: auto resize, auto stamp

This commit is contained in:
cutls 2019-12-14 01:54:40 +09:00
parent 0dcc0ff0ac
commit adf37f6513
14 changed files with 196 additions and 60 deletions

View File

@ -138,7 +138,7 @@ onmessage = function(e) {
} else if (e.data[0] == 'udg') {
udg(e.data[1][0], e.data[1][1])
} else if (e.data[0] == 'media') {
media(e.data[1][0], e.data[1][1], e.data[1][2])
media(e.data[1][0], e.data[1][1], e.data[1][2], e.data[1][3])
} else if (e.data[0] == 'post') {
post('pass')
} else if (e.data[0] == 'toastSaved') {

View File

@ -13,6 +13,10 @@ onmessage = function(e) {
ipc.send('dialogStore', e.data[1])
} else if (e.data[0] == 'bmpImage') {
ipc.send('bmp-image', e.data[1])
} else if (e.data[0] == 'resizeImage') {
ipc.send('resize-image', e.data[1])
} else if (e.data[0] == 'stampImage') {
ipc.send('stamp-image', e.data[1])
} else if (e.data[0] == 'dialogCW') {
ipc.send('dialogCW', e.data[1])
} else if (e.data[0] == 'nativeNotf') {
@ -112,7 +116,31 @@ ipc.on('theme-css-response', function(event, arg) {
})
//img.js
ipc.on('bmp-img-comp', function(event, b64) {
if (b64[2]) {
var stamped = true
} else {
var stamped = false
}
postMessage(['media', [b64[0], 'image/png', b64[1], stamped]], '*')
})
ipc.on('resizeJudgement', function(event, b64) {
var resize = localStorage.getItem('uploadCrop') * 1
if (resize > 0) {
var element = new Image()
var width
element.onload = function() {
var width = element.naturalWidth
var height = element.naturalHeight
if (width > resize || height > resize) {
ipc.send('resize-image', [b64, resize])
} else {
postMessage(['media', [b64[0], 'image/png', b64[1]]], '*')
}
}
element.src = b64
} else {
postMessage(['media', [b64[0], 'image/png', b64[1]]], '*')
}
})
//ui,img.js
ipc.on('general-dl-prog', function(event, arg) {

View File

@ -60,6 +60,24 @@ function handleFileUpload(files, obj, no) {
var fr = new FileReader()
fr.onload = function(evt) {
var b64 = evt.target.result
var resize = localStorage.getItem('uploadCrop') * 1
if (resize > 0) {
var element = new Image()
var width
element.onload = function() {
var width = element.naturalWidth
var height = element.naturalHeight
if (width > resize || height > resize) {
postMessage(['resizeImage', [b64, resize]], '*')
return false
} else {
$('#b64-box').val(b64)
var ret = media(b64, files['type'], no)
}
}
element.src = b64
return false
}
$('#b64-box').val(b64)
var ret = media(b64, files['type'], no)
}
@ -68,7 +86,15 @@ function handleFileUpload(files, obj, no) {
}
//ファイルアップロード
function media(b64, type, no) {
function media(b64, type, no, stamped) {
var acct_id = $('#post-acct-sel').val()
var domain = localStorage.getItem('domain_' + acct_id)
var user = localStorage.getItem('user_' + acct_id)
if ($('#stamp').hasClass('stamp-avail') && !stamped) {
postMessage(['stampImage', [b64, user + '@' + domain]], '*')
return false
}
console.log(b64)
var l = 4
var c = 'abcdefghijklmnopqrstuvwxyz0123456789'
var cl = c.length
@ -88,8 +114,6 @@ function media(b64, type, no) {
var media = toBlob(b64, type)
var fd = new FormData()
fd.append('file', media)
var acct_id = $('#post-acct-sel').val()
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
var httpreq = new XMLHttpRequest()
if (localStorage.getItem('mode_' + domain) == 'misskey') {
@ -104,7 +128,6 @@ function media(b64, type, no) {
}
var previewer = 'url'
fd.append('i', at)
//fd.append('isSensitive', nsfw);
httpreq.send(fd)
} else {
var previewer = 'preview_url'
@ -133,11 +156,7 @@ function media(b64, type, no) {
}
var img = localStorage.getItem('img')
if (json.type.indexOf('image') != -1) {
var html = `<img src="${json[previewer]}" class="preview-img pointer" data-media="${
json['id']
}" oncontextmenu="deleteImage('${json['id']}')" onclick="altImage('${acct_id}','${
json['id']
}')" title="${lang.lang_postimg_delete}">`
var html = `<img src="${json[previewer]}" class="preview-img pointer" data-media="${json['id']}" oncontextmenu="deleteImage('${json['id']}')" onclick="altImage('${acct_id}','${json['id']}')" title="${lang.lang_postimg_delete}">`
$('#preview').append(html)
} else {
$('#preview').append(lang.lang_postimg_previewdis)
@ -300,3 +319,12 @@ function altImage(acct_id, id) {
}
})
}
function stamp() {
if ($('#stamp').hasClass('stamp-avail')) {
$('#stamp').html('Off')
$('#stamp').removeClass('stamp-avail')
} else {
$('#stamp').html('On')
$('#stamp').addClass('stamp-avail')
}
}

View File

@ -295,6 +295,8 @@ function clear() {
$('#nsfw').removeClass('yellow-text')
$('#nsfw').html('visibility_off')
$('#nsfw').removeClass('nsfw-avail')
$('#stamp').html('Off')
$('#stamp').removeClass('stamp-avail')
$('#nsc').text(lang.lang_nothing)
$('#drag').css('background-color', '#e0e0e0')
$('#preview').html('')

View File

@ -1,43 +1,92 @@
function img(mainWindow, dir) {
const electron = require("electron");
const dialog = electron.dialog;
const fs = require("fs");
var Jimp = require("jimp");
var ipc = electron.ipcMain;
const BrowserWindow = electron.BrowserWindow;
const electron = require('electron')
const dialog = electron.dialog
const fs = require('fs')
var Jimp = require('jimp')
var ipc = electron.ipcMain
const BrowserWindow = electron.BrowserWindow
ipc.on('file-select', (e, args) => {
dialog.showOpenDialog(null, {
dialog.showOpenDialog(
null,
{
properties: ['openFile', 'multiSelections'],
title: '添付ファイルを選択',
defaultPath: '.',
filters: [
{ name: 'メディアファイル', extensions: ['jpg', 'png', 'gif', 'bmp', 'jpeg', 'mp4', 'webm'] },
{
name: 'メディアファイル',
extensions: ['jpg', 'png', 'gif', 'bmp', 'jpeg', 'mp4', 'webm']
},
{ name: '画像', extensions: ['jpg', 'png', 'gif', 'bmp', 'jpeg'] },
{ name: '動画', extensions: ['mp4', 'webm'] },
{ name: '全てのファイル', extensions: ['*'] }
]
}, (fileNames) => {
},
fileNames => {
if (!fileNames) {
return false;
return false
}
for (var i = 0; i < fileNames.length; i++) {
var path = fileNames[i];
var bin = fs.readFileSync(path, 'base64');
e.sender.webContents.send('bmp-img-comp', [bin, 'new']);
var path = fileNames[i]
var bin = fs.readFileSync(path, 'base64')
e.sender.webContents.send('resizeJudgement', [bin, 'new'])
}
});
});
}
)
})
ipc.on('bmp-image', (e, args) => {
var m = args[0].match(/(.+)\\(.+)\.(.+)$/);
Jimp.read(args[0], function (err, lenna) {
if (err) throw err;
lenna.getBase64(Jimp.MIME_PNG, function (err, src) {
e.sender.webContents.send('bmp-img-comp', [src, args[1]]);
});
});
});
Jimp.read(args[0], function(err, lenna) {
if (err) throw err
lenna.getBase64(Jimp.MIME_PNG, function(err, src) {
e.sender.webContents.send('bmp-img-comp', [src, args[1]])
})
})
})
ipc.on('resize-image', (e, args) => {
var ext = args[0].toString().slice(args[0].indexOf('/') + 1, args[0].indexOf(';'))
if (ext == 'jpeg') {
var use = 'MIME_JPEG'
} else {
var use = 'MIME_PNG'
}
var b64 = args[0].replace(/^data:\w+\/\w+;base64,/, '')
var decodedFile = new Buffer(b64, 'base64')
Jimp.read(decodedFile, function(err, lenna) {
if (err) throw err
lenna.scaleToFit(args[1], args[1]).getBase64(Jimp[use], function(err, src) {
e.sender.webContents.send('bmp-img-comp', [src, args[1]])
})
})
})
ipc.on('stamp-image', (e, args) => {
var text = args[1]
var b64 = args[0].replace(/^data:\w+\/\w+;base64,/, '')
var decodedFile = new Buffer(b64, 'base64')
console.log(text)
Jimp.read(decodedFile, function(err, image) {
if (err) throw err
Jimp.loadFont(Jimp.FONT_SANS_16_BLACK).then(font => {
var evWidth = Jimp.measureText(font, text)
var width = image.bitmap.width
var height = image.bitmap.height
var left = width - evWidth - 10
var top = height - 30
var color = Jimp.intToRGBA(image.getPixelColor(left, top))
console.log(left, top, color)
var ave = (color.r + color.g + color.b) / 3
if (ave > 128) {
image.print(font, left, top, args[1]).getBase64(Jimp.MIME_PNG, function(err, src) {
e.sender.webContents.send('bmp-img-comp', [src, args[1], true])
})
} else {
Jimp.loadFont(Jimp.FONT_SANS_16_WHITE).then(font => {
image.print(font, left, top, args[1]).getBase64(Jimp.MIME_PNG, function(err, src) {
e.sender.webContents.send('bmp-img-comp', [src, args[1], true])
})
})
}
})
})
})
}
exports.img = img;
exports.img = img

View File

@ -45,7 +45,7 @@ function system(mainWindow, dir, lang, dirname) {
//エクスポートのダイアログ
ipc.on("exportSettings", function(e, args) {
dialog.showSaveDialog(
null,
mainWindow,
{
title: "Export",
properties: ["openFile", "createDirectory"],
@ -62,13 +62,14 @@ function system(mainWindow, dir, lang, dirname) {
//インポートのダイアログ
ipc.on("importSettings", function(e, args) {
dialog.showOpenDialog(
null,
mainWindow,
{
title: "Import",
properties: ["openFile"],
filters: [{ name: "TheDesk Config", extensions: ["thedeskconfig", "thedeskconfigv2", "json5"] }]
},
fileNames => {
console.log("imported from: ", fileNames)
if (!fileNames) {
return false;
}
@ -79,7 +80,7 @@ function system(mainWindow, dir, lang, dirname) {
//保存フォルダのダイアログ
ipc.on("savefolder", function(e, args) {
dialog.showOpenDialog(
null,
mainWindow,
{
title: "Save folder",
properties: ["openDirectory"]
@ -92,7 +93,7 @@ function system(mainWindow, dir, lang, dirname) {
//カスタムサウンドのダイアログ
ipc.on("customSound", function(e, arg) {
dialog.showOpenDialog(
null,
mainWindow,
{
title: "Custom sound",
properties: ["openFile"],

View File

@ -205,9 +205,11 @@
<div class="col s12 mize" style="margin-bottom:5px; padding:0;">
<div id="taglist"></div>
<div id="preview" class="mize"></div>
<span class=" sml mize"><span data-trans="reply">@@replyMode@@</span>:
<span id="rec">@@no@@</span>/<span data-trans="file">@@temp@@</span>:
<span id="mec">@@nothing@@</span>/@@poll@@:<span id="pollsta">@@no@@</span>
<span class=" sml mize"><span>@@replyMode@@</span>:
<span id="rec">@@no@@</span>/<span>@@temp@@</span>:
<span id="mec">@@nothing@@</span>
<a onclick="stamp()" class="pointer" title="@@stampWarn@@">@@stamp@@: <span id="stamp">Off</span></a>
/@@poll@@:<span id="pollsta">@@no@@</span>
<span id="vis" class="hide">public</span>
</span>
<br>

View File

@ -20,6 +20,8 @@
"yes":"Yes",
"temp":"Attaching files",
"nothing":"None",
"stamp": "Stamp",
"stampWarn": "Your acct(aa@bb.cc) is printed on the right-bottom of the uploaded image",
"vis":"Adjust status privacy",
"cwtext":"Warning text",
"selectVis":"Adjust status privacy",

View File

@ -10,6 +10,7 @@
"env": "System Preferences",
"setlang": "Languages",
"backup": "Import and export of preferences",
"backupWarn": "If you got a error when you choose the file, please paste the strings of file and click import",
"import": "Import",
"export": "Export",
"hardwareAcceleration": "Disable hardware acceleration",
@ -137,6 +138,8 @@
"nothing": "Hidden",
"localonly": "Local Only",
"zeroWidthEmoji": "Zero-width space when inserting emojis",
"uploadCrop": "Auto scale to fit",
"uploadCropWarn": "Max long-side px. Uploaded images are converted to JPEG(from JPEG) or PNG(from others). Set 0 and the images will not be resized. Pay attention to GIF animation.",
"keysc": "Keyboard shortcut Preferences",
"iks": "Easy inserter",
"okswarn": "You can insert any letters and emojis with only 3 keys",

View File

@ -136,6 +136,8 @@
"nothing": "表示しない",
"localonly": "ローカル限定",
"zeroWidthEmoji": "絵文字にゼロ幅スペースを使う",
"uploadCrop": "添付画像の自動リサイズ",
"uploadCropWarn": "最大の長辺ピクセル指定。JPEG以外は勝手にPNGイメージに変換するで。GIFアニメに注意しいや。0に設定するとリサイズせんで。",
"keysc": "キーボードショートカットの設定",
"iks": "簡単文字入力",
"okswarn": "絵文字やタグ、&gt;BTなどを登録しておくとすぐに使えてええ感じや。",

View File

@ -20,6 +20,8 @@
"yes":"はい",
"temp":"添付ファイル",
"nothing":"なし",
"stamp": "スタンプ",
"stampWarn": "画像右下にアカウント名(aa@bb.cc)テキストを挿入します",
"vis":"公開範囲",
"cwtext":"警告文",
"selectVis":"公開範囲指定",

View File

@ -10,6 +10,7 @@
"env": "環境設定",
"setlang": "言語",
"backup": "設定のインポートとエクスポート",
"backupWarn": "インポートできない場合、ここにバックアップデータの中身をコピーしてください。",
"import": "インポート",
"export": "エクスポート",
"hardwareAcceleration": "ハードウェアアクセラレーションの無効化",
@ -137,6 +138,8 @@
"nothing": "表示しない",
"localonly": "ローカル限定",
"zeroWidthEmoji": "絵文字にゼロ幅スペースを使う",
"uploadCrop": "添付画像の自動リサイズ",
"uploadCropWarn": "最大の長辺ピクセル指定。JPEG以外は自動でPNGイメージに変換されます。大きなGIFアニメは静止画になります。0に設定するとリサイズしません。",
"keysc": "キーボードショートカットの設定",
"iks": "簡単文字入力",
"okswarn": "絵文字やタグ、&gt;BTなどを登録しておくとすぐに入力できます。",

View File

@ -52,7 +52,9 @@
<button onclick="exportSettings()" class="btn waves-effect lime darken-3"
style="width:100%; max-width:200px;">@@export@@</button>
<button onclick="importSettings()" class="btn waves-effect cyan darken-3"
style="width:100%; max-width:200px;">@@import@@</button>
style="width:100%; max-width:200px;">@@import@@</button><br>
@@backupWarn@@<br>
<input type="text" id="imp-exp" style="width: 300px">
<div id="envView">
<template v-for="(item, i) in config">
<h5>{{item.text.head}}</h5>

View File

@ -658,5 +658,17 @@ var postConstruction = [
desc: '',
checkbox: yesno
}
},{
id: 'uploadCrop',
storage: 'uploadCrop',
checkbox: false,
doubleText: false,
width: 100,
setValue: '0',
text: {
head: '@@uploadCrop@@',
desc: '@@uploadCropWarn@@',
after: 'px'
}
}
]