Skip to content

Commit

Permalink
Merge pull request #6 from memeller/bmp_support
Browse files Browse the repository at this point in the history
bmp support
  • Loading branch information
memeller authored Feb 18, 2023
2 parents 4bb3bc8 + 313aaa8 commit aec92a2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 15 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
# Changelog

## v5.5

- Added BMP support (inline and as attachment) - bmp files are converted to jpeg when resizing.

## v5.4

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Since the original repo is no longer mantained and became read-only on 28th of D

## About this extension

After installing each time a compatible (JPG/PNG) image is attached to/inserted into new e-mail the user has the ability to resize the image. The resized image replaces previously inserted / attached one.
Each time a compatible (JPG/PNG/BMP) image is attached to/inserted into new e-mail the user has the ability to resize the image. The resized image replaces previously inserted / attached one.

## New in this fork (vs darkrojan's version):

- Added PL lang support,
- BMP support (converted to JPG when resized),
- PNG support,
- "Automatically rotate images" can now be checked - there was an issue with parameter name which caused the option to be permanently disabled,
- Resampling finally works
Expand Down
16 changes: 13 additions & 3 deletions api/shrunked.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
reader.onloadend = function () {
let dataURL = reader.result;
let headerIndexEnd = dataURL.indexOf(";");
dataURL = reader.result.substring(0, headerIndexEnd) + ";filename=" + encodeURIComponent(destFile.name) + dataURL.substring(headerIndexEnd);
dataURL = reader.result.substring(0, headerIndexEnd) + ";filename=" + encodeURIComponent(changeExtensionIfNeeded(destFile.name)) + dataURL.substring(headerIndexEnd);
resolve(dataURL);
};
reader.readAsDataURL(destFile);
Expand Down Expand Up @@ -145,7 +145,7 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {

if (!indicies.length) {
if (logenabled)
console.log("Not resizing - no attachments were JPEG/PNG and large enough");
console.log("Not resizing - no attachments were JPEG/PNG/BMP and large enough");
attachmentMenuItem.label = localeData.localizeMessage("context.unsupportedFile");
} else if (indicies.length == 1) {
attachmentMenuItem.label = localeData.localizeMessage("context.single");
Expand Down Expand Up @@ -367,10 +367,20 @@ var shrunked = class extends ExtensionCommon.ExtensionAPI {
}
}
};
function changeExtensionIfNeeded(filename) {
let src = filename.toLowerCase();
//if it is a bmp we will save it as jpeg
if (src.startsWith("data:image/bmp") || src.endsWith(".bmp")) {
return src.replace("bmp", "jpg");
}
else
return src;

}
function imageIsAccepted(url) {
let src = url.toLowerCase();
let isJPEG = src.startsWith("data:image/jpeg") || src.endsWith(".jpg") || src.endsWith(".jpeg");
let isPNG = src.startsWith("data:image/png") || src.endsWith(".png");
return isJPEG | isPNG;
let isBMP = src.startsWith("data:image/bmp") || src.endsWith(".bmp");
return isJPEG | isPNG | isBMP;
}
17 changes: 14 additions & 3 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var tabMap = new Map();

async function shouldResize(attachment, checkSize = true) {
if (!attachment.name.toLowerCase().match(/((\.jpe?g)|(\.png))$/)) {
if (!attachment.name.toLowerCase().match(/((\.jpe?g)|(\.png)|(\.bmp))$/)) {
return false;
}
if (!checkSize) {
Expand Down Expand Up @@ -68,6 +68,7 @@ browser.compose.onAttachmentAdded.addListener(async (tab, attachment) => {
}
await browser.compose.updateAttachment(tab.id, attachment.id, {
file: destFile,
name: changeExtensionIfNeeded(destFile.name)
});
});

Expand Down Expand Up @@ -96,7 +97,7 @@ browser.shrunked.onAttachmentContextClicked.addListener(async (tab, indicies) =>
if (destFile === null) {
return;
}
browser.compose.updateAttachment(tab.id, a.id, { file: destFile });
browser.compose.updateAttachment(tab.id, a.id, { file: destFile, name: changeExtensionIfNeeded(destFile.name) });
});
}
}
Expand Down Expand Up @@ -124,7 +125,7 @@ browser.compose.onBeforeSend.addListener(async (tab, details) => {
if (destFile === null) {
return;
}
await browser.compose.updateAttachment(tab.id, a.id, { file: destFile });
await browser.compose.updateAttachment(tab.id, a.id, { file: destFile, name: changeExtensionIfNeeded(destFile.name) });
});
promises.push(promise);
}
Expand Down Expand Up @@ -252,3 +253,13 @@ function cancelResize(tabId) {
browser.tabs.onRemoved.addListener(tabId => {
tabMap.delete(tabId);
});
function changeExtensionIfNeeded(filename) {
let src = filename.toLowerCase();
//if it is a bmp we will save it as jpeg
if (src.startsWith("data:image/bmp") || src.endsWith(".bmp")) {
return src.replace("bmp", "jpg");
}
else
return src;

}
19 changes: 15 additions & 4 deletions compose_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function maybeResizeInline(target) {
}
if (!imageIsAccepted(target)) {
if (logenabled)
console.log("Not resizing - image is not JPEG / PNG");
console.log("Not resizing - image is not JPEG / PNG / BMP");
return;
}
if (target.width < 500 && target.height < 500) {
Expand Down Expand Up @@ -126,7 +126,7 @@ async function maybeResizeInline(target) {
let dataURL = reader.result;
let headerIndexEnd = dataURL.indexOf(";");
dataURL =
reader.result.substring(0, headerIndexEnd) + ";filename=" + encodeURIComponent(destFile.name) + dataURL.substring(headerIndexEnd);
reader.result.substring(0, headerIndexEnd) + ";filename=" + encodeURIComponent(changeExtensionIfNeeded(destFile.name)) + dataURL.substring(headerIndexEnd);
resolve(dataURL);
};
reader.readAsDataURL(destFile);
Expand All @@ -147,10 +147,21 @@ async function maybeResizeInline(target) {
}
}
}

function changeExtensionIfNeeded(filename){
let src=filename.toLowerCase();
//if it is a bmp we will save it as jpeg
if (src.startsWith("data:image/bmp") || src.endsWith(".bmp"))
{
return src.replace("bmp","jpg");
}
else
return src;

}
function imageIsAccepted(image) {
let src = image.src.toLowerCase();
let isJPEG = src.startsWith("data:image/jpeg") || src.endsWith(".jpg") || src.endsWith(".jpeg");
let isPNG = src.startsWith("data:image/png") || src.endsWith(".png");
return isJPEG | isPNG;
let isBMP = src.startsWith("data:image/bmp") || src.endsWith(".bmp");
return isJPEG | isPNG | isBMP;
}
12 changes: 11 additions & 1 deletion content/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async function loadImage(index) {
}

i_previewthumb.src = images[index].url;
l_previewfilename.textContent = images[index].file.name;
l_previewfilename.textContent = changeExtensionIfNeeded(images[index].file.name);
l_previeworiginalfilesize.textContent = browser.i18n.getMessage("preview.originalfilesize", [
humanSize(images[index].file.size),
]);
Expand Down Expand Up @@ -220,3 +220,13 @@ b_cancel.addEventListener("click", async () => {
let thisWindow = await browser.windows.getCurrent();
browser.windows.remove(thisWindow.id);
});
function changeExtensionIfNeeded(filename) {
let src = filename.toLowerCase();
//if it is a bmp we will save it as jpeg
if (src.startsWith("data:image/bmp") || src.endsWith(".bmp")) {
return src.replace("bmp", "jpg");
}
else
return src;

}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "5.4",
"version": "5.5",
"applications": {
"gecko": {
"id": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion modules/ShrunkedImage.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function ShrunkedImage(source, maxWidth, maxHeight, quality, options) {
if (match) {
this.basename = match[1];
} else {
match =/\/([\w.-]+\.(jpe?g|png))$/i.exec(this.sourceURI.spec);
match =/\/([\w.-]+\.(jpe?g|png|bmp))$/i.exec(this.sourceURI.spec);
if (match) {
this.basename = match[1];
}
Expand Down

0 comments on commit aec92a2

Please sign in to comment.