11import imagemin from 'imagemin'
22import mozjpeg from 'imagemin-mozjpeg'
3- import optipng from 'imagemin-optipng'
43import { CompressOptions , ImgInfo } from '../utils/interfaces'
54import { getImageBuffer } from '../utils/getImage'
65var images = require ( "images" ) ;
@@ -13,14 +12,14 @@ export function lubanCompress({ ctx, info }: CompressOptions): Promise<ImgInfo>
1312 }*/
1413
1514
16- function computeInSampleSize ( srcWidth : number , srcHeight :number ) {
15+ function computeInSampleSize ( srcWidth : number , srcHeight : number ) {
1716 srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth ;
1817 srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight ;
1918
2019 var longSide = Math . max ( srcWidth , srcHeight ) ;
2120 var shortSide = Math . min ( srcWidth , srcHeight ) ;
2221
23- var scale = ( shortSide / longSide ) ;
22+ var scale = ( shortSide / longSide ) ;
2423 if ( scale <= 1 && scale > 0.5625 ) {
2524 if ( longSide < 1664 ) {
2625 return 1 ;
@@ -34,7 +33,7 @@ function computeInSampleSize(srcWidth :number, srcHeight:number) {
3433 } else if ( scale <= 0.5625 && scale > 0.5 ) {
3534 return longSide / 1280 == 0 ? 1 : longSide / 1280 ;
3635 } else {
37- return Math . ceil ( longSide / ( 1280.0 / scale ) ) ;
36+ return Math . ceil ( longSide / ( 1280.0 / scale ) ) ;
3837 }
3938 }
4039
@@ -45,14 +44,14 @@ function computeInSampleSize(srcWidth :number, srcHeight:number) {
4544 }
4645
4746 return getImageBuffer ( ctx , info . url )
48- . then ( ( buffer ) => {
49- ctx . log . warn ( '原始文件大小:' + Math . round ( buffer . length / 1024 ) + "k" )
50- if ( isJpg ( buffer ) ) {
51- ctx . log . warn ( '本身就是jpg,不用转换:' + info . url )
47+ . then ( ( buffer ) => {
48+ ctx . log . warn ( '原始文件大小:' + Math . round ( buffer . length / 1024 ) + "k" )
49+ if ( isJpg ( buffer ) ) {
50+ ctx . log . warn ( '本身就是jpg,不用转换:' + info . url )
5251 return buffer
5352 }
54- ctx . log . warn ( 'luban 格式转换为jpg:' + info . url )
55- return images ( buffer ) . encode ( "jpg" ) //, {operation:90}
53+ ctx . log . warn ( 'luban 格式转换为jpg:' + info . url )
54+ return images ( buffer ) . encode ( "jpg" ) //, {operation:90}
5655 } )
5756 /*.then((buffer)=>{
5857 var image2 = images(buffer)
@@ -79,31 +78,31 @@ function computeInSampleSize(srcWidth :number, srcHeight:number) {
7978 return image2.resize(conpressWidth).encode("jpg")//, {operation:90}
8079 })*/
8180 . then ( ( buffer ) => {
82- ctx . log . warn ( '文件大小:' + Math . round ( buffer . length / 1024 ) + "k" )
81+ ctx . log . warn ( '文件大小:' + Math . round ( buffer . length / 1024 ) + "k" )
8382
8483 var image2 = images ( buffer )
85- ctx . log . warn ( '图片尺寸:' + image2 . width ( ) + "x" + image2 . height ( ) )
84+ ctx . log . warn ( '图片尺寸:' + image2 . width ( ) + "x" + image2 . height ( ) )
8685 //todo 关键在于获取图片本身的宽高
87- var sample = Math . round ( computeInSampleSize ( image2 . width ( ) , image2 . height ( ) ) )
88- var filesize = Math . round ( buffer . length / 1024 )
89- var longsize = image2 . width ( ) > image2 . height ( ) ? image2 . width ( ) :image2 . height ( )
86+ var sample = Math . round ( computeInSampleSize ( image2 . width ( ) , image2 . height ( ) ) )
87+ var filesize = Math . round ( buffer . length / 1024 )
88+ var longsize = image2 . width ( ) > image2 . height ( ) ? image2 . width ( ) : image2 . height ( )
9089 var sampleSize = [ '1x1' ] ;
91- if ( filesize > 100 && sample > 1 ) {
92- if ( longsize > 3000 && filesize < 700 ) {
90+ if ( filesize > 100 && sample > 1 ) {
91+ if ( longsize > 3000 && filesize < 700 ) {
9392
94- } else {
95- sampleSize = [ sample + 'x' + sample ]
93+ } else {
94+ sampleSize = [ sample + 'x' + sample ]
9695 }
9796
9897 }
99- ctx . log . warn ( 'sampleSize:' + sampleSize [ 0 ] )
98+ ctx . log . warn ( 'sampleSize:' + sampleSize [ 0 ] )
10099
101100 return imagemin . buffer ( buffer , {
102- plugins : [ mozjpeg ( { quality : 75 , sample :sampleSize } ) ] , //, optipng({ optimizationLevel: 5 })//, sample:sampleSize
101+ plugins : [ mozjpeg ( { quality : 75 , sample : sampleSize } ) ] , //, optipng({ optimizationLevel: 5 })//, sample:sampleSize
103102 } )
104103 } )
105104 . then ( ( buffer ) => {
106- ctx . log . warn ( '最后mozjpeg compress in success,最终文件大小:' + Math . round ( buffer . length / 1024 ) + "k" )
105+ ctx . log . warn ( '最后mozjpeg compress in success,最终文件大小:' + Math . round ( buffer . length / 1024 ) + "k" )
107106 return {
108107 ...info ,
109108 buffer,
0 commit comments