@@ -20,7 +20,9 @@ import {
20
20
multiSignEmbed ,
21
21
embedDocId ,
22
22
pdfNewWidthFun ,
23
- signPdfFun
23
+ signPdfFun ,
24
+ calculateImgAspectRatio ,
25
+ onImageSelect
24
26
} from "../utils/Utils" ;
25
27
import Loader from "./component/loader" ;
26
28
import HandleError from "./component/HandleError" ;
@@ -476,78 +478,31 @@ function PdfRequestFiles() {
476
478
//function for image upload or update
477
479
const onImageChange = ( event ) => {
478
480
if ( event . target . files && event . target . files [ 0 ] ) {
479
- const imageType = event . target . files [ 0 ] . type ;
480
- const reader = new FileReader ( ) ;
481
- reader . readAsDataURL ( event . target . files [ 0 ] ) ;
482
- reader . onloadend = function ( e ) {
483
- let width , height ;
484
- const image = new Image ( ) ;
485
- image . src = e . target . result ;
486
- image . onload = function ( ) {
487
- width = image . width ;
488
- height = image . height ;
489
- const aspectRatio = 460 / 184 ;
490
- const imgR = width / height ;
491
-
492
- if ( imgR > aspectRatio ) {
493
- width = 460 ;
494
- height = 460 / imgR ;
495
- } else {
496
- width = 184 * imgR ;
497
- height = 184 ;
498
- }
499
- setImgWH ( { width : width , height : height } ) ;
500
- imageRef . current . style . width = `${ width } px` ;
501
- imageRef . current . style . height = `${ height } px` ;
502
- } ;
503
-
504
- image . src = reader . result ;
505
-
506
- setImage ( { src : image . src , imgType : imageType } ) ;
507
- } ;
481
+ onImageSelect ( event , setImgWH , setImage ) ;
508
482
}
509
483
} ;
510
484
//function for upload stamp image
511
485
const onSaveImage = ( ) => {
512
486
const currentSigner = signerPos . filter (
513
487
( data ) => data . signerObjId === signerObjectId
514
488
) ;
515
-
516
489
const i = currentSigner [ 0 ] . placeHolder . findIndex ( ( object ) => {
517
490
return object . pageNumber === pageNumber ;
518
491
} ) ;
519
492
const updateFilter = currentSigner [ 0 ] . placeHolder [ i ] . pos . filter (
520
493
( data ) =>
521
494
data . key === signKey && data . Width && data . Height && data . SignUrl
522
495
) ;
523
-
496
+ let getIMGWH = calculateImgAspectRatio ( imgWH ) ;
524
497
if ( updateFilter . length > 0 ) {
525
- let newWidth , nweHeight ;
526
- const aspectRatio = imgWH . width / imgWH . height ;
527
498
const getXYdata = currentSigner [ 0 ] . placeHolder [ i ] . pos ;
528
- if ( aspectRatio === 1 ) {
529
- newWidth = aspectRatio * 100 ;
530
- nweHeight = aspectRatio * 100 ;
531
- } else if ( aspectRatio < 2 ) {
532
- newWidth = aspectRatio * 100 ;
533
- nweHeight = 100 ;
534
- } else if ( aspectRatio > 2 && aspectRatio < 4 ) {
535
- newWidth = aspectRatio * 70 ;
536
- nweHeight = 70 ;
537
- } else if ( aspectRatio > 4 ) {
538
- newWidth = aspectRatio * 40 ;
539
- nweHeight = 40 ;
540
- } else if ( aspectRatio > 5 ) {
541
- newWidth = aspectRatio * 10 ;
542
- nweHeight = 10 ;
543
- }
544
499
const getPosData = getXYdata ;
545
500
const addSign = getPosData . map ( ( url , ind ) => {
546
501
if ( url . key === signKey ) {
547
502
return {
548
503
...url ,
549
- Width : newWidth ,
550
- Height : nweHeight ,
504
+ Width : getIMGWH . newWidth ,
505
+ Height : getIMGWH . newHeight ,
551
506
SignUrl : image . src ,
552
507
ImageType : image . imgType
553
508
} ;
@@ -571,32 +526,12 @@ function PdfRequestFiles() {
571
526
572
527
const getPosData = getXYdata ;
573
528
574
- const aspectRatio = imgWH . width / imgWH . height ;
575
-
576
- let newWidth , nweHeight ;
577
- if ( aspectRatio === 1 ) {
578
- newWidth = aspectRatio * 100 ;
579
- nweHeight = aspectRatio * 100 ;
580
- } else if ( aspectRatio < 2 ) {
581
- newWidth = aspectRatio * 100 ;
582
- nweHeight = 100 ;
583
- } else if ( aspectRatio > 2 && aspectRatio < 4 ) {
584
- newWidth = aspectRatio * 70 ;
585
- nweHeight = 70 ;
586
- } else if ( aspectRatio > 4 ) {
587
- newWidth = aspectRatio * 40 ;
588
- nweHeight = 40 ;
589
- } else if ( aspectRatio > 5 ) {
590
- newWidth = aspectRatio * 10 ;
591
- nweHeight = 10 ;
592
- }
593
-
594
529
const addSign = getPosData . map ( ( url , ind ) => {
595
530
if ( url . key === signKey ) {
596
531
return {
597
532
...url ,
598
- Width : newWidth ,
599
- Height : nweHeight ,
533
+ Width : getIMGWH . newWidth ,
534
+ Height : getIMGWH . newHeight ,
600
535
SignUrl : image . src ,
601
536
ImageType : image . imgType
602
537
} ;
@@ -622,10 +557,22 @@ function PdfRequestFiles() {
622
557
//function for save button to save signature or image url
623
558
const onSaveSign = ( isDefaultSign ) => {
624
559
const signatureImg = isDefaultSign ? defaultSignImg : signature ;
560
+ const isSign = true ;
561
+ let getIMGWH ;
625
562
setIsSignPad ( false ) ;
626
563
setIsImageSelect ( false ) ;
627
564
setImage ( ) ;
628
-
565
+ if ( isDefaultSign ) {
566
+ const img = new Image ( ) ;
567
+ img . src = defaultSignImg ;
568
+ if ( img . complete ) {
569
+ let imgWH = {
570
+ width : img . width ,
571
+ height : img . height
572
+ } ;
573
+ getIMGWH = calculateImgAspectRatio ( imgWH ) ;
574
+ }
575
+ }
629
576
const currentSigner = signerPos . filter (
630
577
( data ) => data . signerObjId === signerObjectId
631
578
) ;
@@ -639,15 +586,36 @@ function PdfRequestFiles() {
639
586
updateFilter = currentSigner [ 0 ] . placeHolder [ i ] . pos . filter (
640
587
( data ) => data . key === signKey && data . SignUrl
641
588
) ;
642
-
589
+ const getXYdata = currentSigner [ 0 ] . placeHolder [ i ] . pos ;
590
+ const getPosData = getXYdata ;
591
+ const posWidth = isDefaultSign
592
+ ? getIMGWH . newWidth
593
+ : isSign && getPosData [ 0 ] . ImageType
594
+ ? 150
595
+ : getPosData [ 0 ] . Width
596
+ ? getPosData [ 0 ] . Width
597
+ : 150 ;
598
+ const posHidth = isDefaultSign
599
+ ? getIMGWH . newHeight
600
+ : isSign && getPosData [ 0 ] . ImageType
601
+ ? 60
602
+ : getPosData [ 0 ] . Height
603
+ ? getPosData [ 0 ] . Height
604
+ : 60 ;
643
605
if ( updateFilter . length > 0 ) {
644
606
updateFilter [ 0 ] . SignUrl = signatureImg ;
607
+ updateFilter [ 0 ] . Width = posWidth ;
608
+ updateFilter [ 0 ] . Height = posHidth ;
645
609
} else {
646
- const getXYdata = currentSigner [ 0 ] . placeHolder [ i ] . pos ;
647
- const getPosData = getXYdata ;
648
610
const addSign = getPosData . map ( ( url , ind ) => {
649
611
if ( url . key === signKey ) {
650
- return { ...url , SignUrl : signatureImg } ;
612
+ return {
613
+ ...url ,
614
+ SignUrl : signatureImg ,
615
+ Width : posWidth ,
616
+ Height : posHidth ,
617
+ ImageType : "sign"
618
+ } ;
651
619
}
652
620
return url ;
653
621
} ) ;
0 commit comments