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') { } else if (e.data[0] == 'udg') {
udg(e.data[1][0], e.data[1][1]) udg(e.data[1][0], e.data[1][1])
} else if (e.data[0] == 'media') { } 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') { } else if (e.data[0] == 'post') {
post('pass') post('pass')
} else if (e.data[0] == 'toastSaved') { } else if (e.data[0] == 'toastSaved') {

View File

@ -13,6 +13,10 @@ onmessage = function(e) {
ipc.send('dialogStore', e.data[1]) ipc.send('dialogStore', e.data[1])
} else if (e.data[0] == 'bmpImage') { } else if (e.data[0] == 'bmpImage') {
ipc.send('bmp-image', e.data[1]) 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') { } else if (e.data[0] == 'dialogCW') {
ipc.send('dialogCW', e.data[1]) ipc.send('dialogCW', e.data[1])
} else if (e.data[0] == 'nativeNotf') { } else if (e.data[0] == 'nativeNotf') {
@ -112,7 +116,31 @@ ipc.on('theme-css-response', function(event, arg) {
}) })
//img.js //img.js
ipc.on('bmp-img-comp', function(event, b64) { 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]]], '*') postMessage(['media', [b64[0], 'image/png', b64[1]]], '*')
}
}
element.src = b64
} else {
postMessage(['media', [b64[0], 'image/png', b64[1]]], '*')
}
}) })
//ui,img.js //ui,img.js
ipc.on('general-dl-prog', function(event, arg) { ipc.on('general-dl-prog', function(event, arg) {

View File

@ -60,6 +60,24 @@ function handleFileUpload(files, obj, no) {
var fr = new FileReader() var fr = new FileReader()
fr.onload = function(evt) { fr.onload = function(evt) {
var b64 = evt.target.result 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) $('#b64-box').val(b64)
var ret = media(b64, files['type'], no) 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 l = 4
var c = 'abcdefghijklmnopqrstuvwxyz0123456789' var c = 'abcdefghijklmnopqrstuvwxyz0123456789'
var cl = c.length var cl = c.length
@ -88,8 +114,6 @@ function media(b64, type, no) {
var media = toBlob(b64, type) var media = toBlob(b64, type)
var fd = new FormData() var fd = new FormData()
fd.append('file', media) 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 at = localStorage.getItem('acct_' + acct_id + '_at')
var httpreq = new XMLHttpRequest() var httpreq = new XMLHttpRequest()
if (localStorage.getItem('mode_' + domain) == 'misskey') { if (localStorage.getItem('mode_' + domain) == 'misskey') {
@ -104,7 +128,6 @@ function media(b64, type, no) {
} }
var previewer = 'url' var previewer = 'url'
fd.append('i', at) fd.append('i', at)
//fd.append('isSensitive', nsfw);
httpreq.send(fd) httpreq.send(fd)
} else { } else {
var previewer = 'preview_url' var previewer = 'preview_url'
@ -133,11 +156,7 @@ function media(b64, type, no) {
} }
var img = localStorage.getItem('img') var img = localStorage.getItem('img')
if (json.type.indexOf('image') != -1) { if (json.type.indexOf('image') != -1) {
var html = `<img src="${json[previewer]}" class="preview-img pointer" data-media="${ 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}">`
json['id']
}" oncontextmenu="deleteImage('${json['id']}')" onclick="altImage('${acct_id}','${
json['id']
}')" title="${lang.lang_postimg_delete}">`
$('#preview').append(html) $('#preview').append(html)
} else { } else {
$('#preview').append(lang.lang_postimg_previewdis) $('#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').removeClass('yellow-text')
$('#nsfw').html('visibility_off') $('#nsfw').html('visibility_off')
$('#nsfw').removeClass('nsfw-avail') $('#nsfw').removeClass('nsfw-avail')
$('#stamp').html('Off')
$('#stamp').removeClass('stamp-avail')
$('#nsc').text(lang.lang_nothing) $('#nsc').text(lang.lang_nothing)
$('#drag').css('background-color', '#e0e0e0') $('#drag').css('background-color', '#e0e0e0')
$('#preview').html('') $('#preview').html('')

View File

@ -1,43 +1,92 @@
function img(mainWindow, dir) { function img(mainWindow, dir) {
const electron = require("electron"); const electron = require('electron')
const dialog = electron.dialog; const dialog = electron.dialog
const fs = require("fs"); const fs = require('fs')
var Jimp = require("jimp"); var Jimp = require('jimp')
var ipc = electron.ipcMain; var ipc = electron.ipcMain
const BrowserWindow = electron.BrowserWindow; const BrowserWindow = electron.BrowserWindow
ipc.on('file-select', (e, args) => { ipc.on('file-select', (e, args) => {
dialog.showOpenDialog(
dialog.showOpenDialog(null, { null,
{
properties: ['openFile', 'multiSelections'], properties: ['openFile', 'multiSelections'],
title: '添付ファイルを選択', title: '添付ファイルを選択',
defaultPath: '.', defaultPath: '.',
filters: [ 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: ['jpg', 'png', 'gif', 'bmp', 'jpeg'] },
{ name: '動画', extensions: ['mp4', 'webm'] }, { name: '動画', extensions: ['mp4', 'webm'] },
{ name: '全てのファイル', extensions: ['*'] } { name: '全てのファイル', extensions: ['*'] }
] ]
}, (fileNames) => { },
fileNames => {
if (!fileNames) { if (!fileNames) {
return false; return false
} }
for (var i = 0; i < fileNames.length; i++) { for (var i = 0; i < fileNames.length; i++) {
var path = fileNames[i]; var path = fileNames[i]
var bin = fs.readFileSync(path, 'base64'); var bin = fs.readFileSync(path, 'base64')
e.sender.webContents.send('bmp-img-comp', [bin, 'new']); e.sender.webContents.send('resizeJudgement', [bin, 'new'])
} }
}); }
}); )
})
ipc.on('bmp-image', (e, args) => { ipc.on('bmp-image', (e, args) => {
Jimp.read(args[0], function(err, lenna) {
var m = args[0].match(/(.+)\\(.+)\.(.+)$/); if (err) throw err
Jimp.read(args[0], function (err, lenna) { lenna.getBase64(Jimp.MIME_PNG, function(err, src) {
if (err) throw err; e.sender.webContents.send('bmp-img-comp', [src, args[1]])
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) { ipc.on("exportSettings", function(e, args) {
dialog.showSaveDialog( dialog.showSaveDialog(
null, mainWindow,
{ {
title: "Export", title: "Export",
properties: ["openFile", "createDirectory"], properties: ["openFile", "createDirectory"],
@ -62,13 +62,14 @@ function system(mainWindow, dir, lang, dirname) {
//インポートのダイアログ //インポートのダイアログ
ipc.on("importSettings", function(e, args) { ipc.on("importSettings", function(e, args) {
dialog.showOpenDialog( dialog.showOpenDialog(
null, mainWindow,
{ {
title: "Import", title: "Import",
properties: ["openFile"], properties: ["openFile"],
filters: [{ name: "TheDesk Config", extensions: ["thedeskconfig", "thedeskconfigv2", "json5"] }] filters: [{ name: "TheDesk Config", extensions: ["thedeskconfig", "thedeskconfigv2", "json5"] }]
}, },
fileNames => { fileNames => {
console.log("imported from: ", fileNames)
if (!fileNames) { if (!fileNames) {
return false; return false;
} }
@ -79,7 +80,7 @@ function system(mainWindow, dir, lang, dirname) {
//保存フォルダのダイアログ //保存フォルダのダイアログ
ipc.on("savefolder", function(e, args) { ipc.on("savefolder", function(e, args) {
dialog.showOpenDialog( dialog.showOpenDialog(
null, mainWindow,
{ {
title: "Save folder", title: "Save folder",
properties: ["openDirectory"] properties: ["openDirectory"]
@ -92,7 +93,7 @@ function system(mainWindow, dir, lang, dirname) {
//カスタムサウンドのダイアログ //カスタムサウンドのダイアログ
ipc.on("customSound", function(e, arg) { ipc.on("customSound", function(e, arg) {
dialog.showOpenDialog( dialog.showOpenDialog(
null, mainWindow,
{ {
title: "Custom sound", title: "Custom sound",
properties: ["openFile"], properties: ["openFile"],

View File

@ -205,9 +205,11 @@
<div class="col s12 mize" style="margin-bottom:5px; padding:0;"> <div class="col s12 mize" style="margin-bottom:5px; padding:0;">
<div id="taglist"></div> <div id="taglist"></div>
<div id="preview" class="mize"></div> <div id="preview" class="mize"></div>
<span class=" sml mize"><span data-trans="reply">@@replyMode@@</span>: <span class=" sml mize"><span>@@replyMode@@</span>:
<span id="rec">@@no@@</span>/<span data-trans="file">@@temp@@</span>: <span id="rec">@@no@@</span>/<span>@@temp@@</span>:
<span id="mec">@@nothing@@</span>/@@poll@@:<span id="pollsta">@@no@@</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 id="vis" class="hide">public</span>
</span> </span>
<br> <br>

View File

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

View File

@ -10,6 +10,7 @@
"env": "System Preferences", "env": "System Preferences",
"setlang": "Languages", "setlang": "Languages",
"backup": "Import and export of preferences", "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", "import": "Import",
"export": "Export", "export": "Export",
"hardwareAcceleration": "Disable hardware acceleration", "hardwareAcceleration": "Disable hardware acceleration",
@ -137,6 +138,8 @@
"nothing": "Hidden", "nothing": "Hidden",
"localonly": "Local Only", "localonly": "Local Only",
"zeroWidthEmoji": "Zero-width space when inserting emojis", "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", "keysc": "Keyboard shortcut Preferences",
"iks": "Easy inserter", "iks": "Easy inserter",
"okswarn": "You can insert any letters and emojis with only 3 keys", "okswarn": "You can insert any letters and emojis with only 3 keys",

View File

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

View File

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

View File

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

View File

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

View File

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