Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fns_front_2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenqikuai
fns_front_2
Commits
d66c702b
Commit
d66c702b
authored
Sep 10, 2021
by
chenqikuai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
存储图片压缩代码
parent
d7187807
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
1 deletion
+73
-1
resize.ts
src/utils/resize.ts
+72
-0
tsconfig.json
tsconfig.json
+1
-1
No files found.
src/utils/resize.ts
0 → 100644
View file @
d66c702b
var
fileinput
=
document
.
getElementById
(
'fileinput'
)
var
preview
=
document
.
getElementById
(
'preview'
)
const
max_width
=
100
const
max_height
=
100
var
form
=
document
.
getElementById
(
'form'
)
function
processfile
(
file
:
File
)
{
return
new
Promise
((
resolve
)
=>
{
if
(
!
/image/i
.
test
(
file
.
type
))
{
alert
(
'File '
+
file
.
name
+
' is not an image.'
)
return
false
}
// read the files
var
reader
=
new
FileReader
()
reader
.
readAsArrayBuffer
(
file
)
reader
.
onload
=
function
(
event
:
Event
)
{
// blob stuff
if
(
!
reader
.
result
)
return
var
blob
=
new
Blob
([
reader
.
result
])
// create blob...
window
.
URL
=
window
.
URL
||
window
.
webkitURL
var
blobURL
=
window
.
URL
.
createObjectURL
(
blob
)
// and get it's URL
// helper Image object
var
image
=
new
Image
()
image
.
src
=
blobURL
//preview.appendChild(image); // preview commented out, I am using the canvas instead
image
.
onload
=
function
()
{
// have to wait till it's loaded
var
resized
=
resizeMe
(
image
)
// send it to canvas
resolve
(
resized
)
}
}
})
}
// === RESIZE ====
function
resizeMe
(
img
:
HTMLImageElement
)
{
var
canvas
=
document
.
createElement
(
'canvas'
)
var
width
=
img
.
width
var
height
=
img
.
height
// calculate the width and height, constraining the proportions
if
(
width
>
height
)
{
if
(
width
>
max_width
)
{
//height *= max_width / width;
height
=
Math
.
round
((
height
*=
max_width
/
width
))
width
=
max_width
}
}
else
{
if
(
height
>
max_height
)
{
//width *= max_height / height;
width
=
Math
.
round
((
width
*=
max_height
/
height
))
height
=
max_height
}
}
// resize the canvas and draw the image data into it
canvas
.
width
=
width
canvas
.
height
=
height
var
ctx
=
canvas
.
getContext
(
'2d'
)
ctx
&&
ctx
.
drawImage
(
img
,
0
,
0
,
width
,
height
)
return
canvas
.
toDataURL
(
'image/jpeg'
,
0.7
)
// get the data from canvas as 70% JPG (can be also PNG, etc.)
}
tsconfig.json
View file @
d66c702b
...
@@ -32,7 +32,7 @@
...
@@ -32,7 +32,7 @@
"src/**/*.vue"
,
"src/**/*.vue"
,
"tests/**/*.ts"
,
"tests/**/*.ts"
,
"tests/**/*.tsx"
,
"tests/**/*.tsx"
,
"src/plugins/longPress.js"
"src/plugins/longPress.js"
,
],
],
"exclude"
:
[
"exclude"
:
[
"node_modules"
"node_modules"
...
...
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