dip s

dip s

  • NA
  • 256
  • 81.6k

How to Print dynamic text on an image and save that image(with text).

Jul 17 2020 4:58 AM
In my application I have a modal. On that modal there is a button to select image. User will select image. That image i have to show in a fixed size container on modal. Then user will write some text(or notes) in a textbox, which he wants to print on image and then clicks on image.
 So that text will be printed at position where user has clicked.
Now I have to save this image with text written on it. So next time whenever I open this image it will show me image with text at same position.
My code is as follows
This is function to select image. 
  1. onSelectFile(event: any) {  
  2.    if (event.target.files && event.target.files[0]) {  
  3.      const reader = new FileReader();  
  4.     // called once readAsDataURL is completed  
  5.      reader.onload = (event: any) => {   
  6.        this.url = event.target.result;  
  7.      };  
  8.      reader.readAsDataURL(event.target.files[0]); // read file as data url  
  9.    }  
  10.  }  
 
This function is used to get x and y coordinated where user has clicked and then i have created element at runtime and I am assiging user entered text to this newly created label element.
  1. getcoordinates(event) {  
  2.    console.log(event.offsetX);  
  3.    console.log(event.offsetY);  
  4.    this.X = event.pageX - event.offsetX - 30;  
  5.    this.Y = event.pageY - event.offsetY + 15;  
  6.    const relativeX = (event.pageX);  
  7.    const relativeY = (event.pageY);  
  8.    const txt = document.createElement("LABEL");  
  9.    txt.style.cssText = "position: fixed; color: red";  
  10.    txt.setAttribute("id""txtarea");  
  11.    txt.innerHTML = this.itemToEditImage.Text;  
  12.    txt.style.left = relativeX + 'px';  
  13.    txt.style.top = relativeY + 'px';   
  14.    txt.style.zIndex = "9000";  
  15.    const parent = document.getElementsByClassName('container');  
  16.    parent.item(0).appendChild(txt);  
  17.  }  
 HTML code is 
 
 
  1. <div class="row">  
  2.        <div class="col-md-5">  
  3.               <div class="row">  
  4.                   <div class="col-md-3">  
  5.                           <input type="file" (change)="onSelectFile($event)">  
  6.                    </div>  
  7.               </div>  
  8.                <br>  
  9.               <div class="row">  
  10.                     <div class="col-md-1">  
  11.                       <input type="text" id="TextData" name="TextData" [(ngModel)]="itemToEditImage.Text">  
  12.                        </div>  
  13.                </div>  
  14.                  <br>   
  15.         </div>  
  16.         <div class="col-md-7">  
  17.             <div class="container my-custom-scrollbar d-inline-block">  
  18.               <img [src]="url" (click)="getcoordinates($event)" style="width: 100%; height: 100%; object-fit: scale-down;">  
  19.               </div>  
  20.          </div>  
  21.  </div>  
 CSS file
  1. .my-custom-scrollbar {  
  2.         position: relative;  
  3.         width: 500px;  
  4.         height: 500px;  
  5.         overflow: auto;  
  6.     }  
  7.       
  8.     .caption {  
  9.         color: red;  
  10.         font-weight: bold;  
  11.     }   
 
But the positon where user has clicked and position where text is printed are not matching. It does not print text at exact position where user has clicked. So how to pirnt text on image? Also how to save this image with text; at given path? Thank you in advance.
 
 

Answers (2)