3

I want to build a file upload (based on Angular2 - Display image) with angular2 which uploads the original image but displays a resized image as a thumbnail preview.

Before uploading the image has to be displayed as a thumbnail below the file input field. Right now the image element is displaying a base64 string and all works fine... BUT!

getInput(fileInput) {

    const reader  = new FileReader();

    reader.onload = ((e: any) => {
        this.logo = e.target.result;
    });

    reader.readAsDataURL(fileInput.target.files[0]);

}

If I want to change the image size by adding inline css with e.g. width 33px to the image element, the quality of the image is really bad:

<input type="file" class="form-control" (change)="getInput($event)" name="qr.logo"/>
<img [src]="logo" [width]="33" >

Is there a way to resize the image with angular without or with low quality loss?

3
  • no, you can't squeeze a big image in 33px and not suffer some quality loss. Commented May 30, 2017 at 10:18
  • you could use a canvas for better image processing options Commented May 30, 2017 at 10:27
  • yes as mentioned you can't really do much about the quality. I would recommend you however, to use the svg format if it's possible. Its quality and resolution scale extremely better with different sizes than other formats such jpg and png. Commented May 30, 2017 at 12:34

1 Answer 1

0

You can use angular2-img-cropper package to resize the image. https://github.com/cstefanache/angular2-img-cropper

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.