Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
electron-printer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhuzhequan
electron-printer
Commits
867cb6f6
Commit
867cb6f6
authored
Jul 19, 2025
by
linjinhong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改问题
parent
d4a4c089
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
39 deletions
+11
-39
src/server/utils/setImage.js
+11
-39
No files found.
src/server/utils/setImage.js
View file @
867cb6f6
...
@@ -12,7 +12,6 @@ const sharp = require("sharp");
...
@@ -12,7 +12,6 @@ const sharp = require("sharp");
*/
*/
async
function
cropImageTransparentEdges
(
inputPath
,
outputPath
,
options
=
{})
{
async
function
cropImageTransparentEdges
(
inputPath
,
outputPath
,
options
=
{})
{
const
threshold
=
options
.
threshold
||
10
;
const
threshold
=
options
.
threshold
||
10
;
const
blockSize
=
options
.
blockSize
||
500
;
// 可调的块大小,根据内存调整
// 确保输出目录存在
// 确保输出目录存在
const
outputDir
=
path
.
dirname
(
outputPath
);
const
outputDir
=
path
.
dirname
(
outputPath
);
...
@@ -40,44 +39,17 @@ async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
...
@@ -40,44 +39,17 @@ async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
let
leftmost
=
width
;
let
leftmost
=
width
;
let
rightmost
=
0
;
let
rightmost
=
0
;
// for (let y = 0; y < height; y++) {
for
(
let
y
=
0
;
y
<
height
;
y
++
)
{
// for (let x = 0; x < width; x++) {
for
(
let
x
=
0
;
x
<
width
;
x
++
)
{
// const alphaIdx = (y * width + x) * channels + channels - 1;
const
alphaIdx
=
(
y
*
width
+
x
)
*
channels
+
channels
-
1
;
// const alpha = data[alphaIdx];
const
alpha
=
data
[
alphaIdx
];
// // 如果像素不是透明的(根据阈值)
// 如果像素不是透明的(根据阈值)
// if (alpha > threshold) {
if
(
alpha
>
threshold
)
{
// topmost = Math.min(topmost, y);
topmost
=
Math
.
min
(
topmost
,
y
);
// bottommost = Math.max(bottommost, y);
bottommost
=
Math
.
max
(
bottommost
,
y
);
// leftmost = Math.min(leftmost, x);
leftmost
=
Math
.
min
(
leftmost
,
x
);
// rightmost = Math.max(rightmost, x);
rightmost
=
Math
.
max
(
rightmost
,
x
);
// }
// }
// }
const
alphaIndexOffset
=
channels
-
1
;
// alpha channel
// 分块扫描
for
(
let
yStart
=
0
;
yStart
<
height
;
yStart
+=
blockSize
)
{
for
(
let
xStart
=
0
;
xStart
<
width
;
xStart
+=
blockSize
)
{
const
blockHeight
=
Math
.
min
(
blockSize
,
height
-
yStart
);
const
blockWidth
=
Math
.
min
(
blockSize
,
width
-
xStart
);
for
(
let
y
=
0
;
y
<
blockHeight
;
y
++
)
{
for
(
let
x
=
0
;
x
<
blockWidth
;
x
++
)
{
const
alphaIdx
=
((
yStart
+
y
)
*
width
+
(
xStart
+
x
))
*
channels
+
alphaIndexOffset
;
const
alpha
=
data
[
alphaIdx
];
// 判断透明度是否大于阈值
if
(
alpha
>
threshold
)
{
// 更新边界
topmost
=
Math
.
min
(
topmost
,
yStart
+
y
);
bottommost
=
Math
.
max
(
bottommost
,
yStart
+
y
);
leftmost
=
Math
.
min
(
leftmost
,
xStart
+
x
);
rightmost
=
Math
.
max
(
rightmost
,
xStart
+
x
);
}
}
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment