johndatserakis / file-upload-with-preview

🖼 Simple file-upload utility that shows a preview of the uploaded image. Written in TypeScript. No dependencies. Works well with or without a framework.

Home Page:https://johndatserakis.github.io/file-upload-with-preview

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

keep original image size in preview.

WriterStat opened this issue · comments

Hi, ran across this and trying it out. Thanks!

Using it for one image to test,

If I choose a small image height x width the preview displays/fills the entire preview area. This could make it blurry.

I may find this, but thought I'd give a quick ask,

Is there a way to keep the original image size in the preview area? Get the original image dimensions?

Thanks much! Appreciate it.

Didn't see this in the notes.

Best,

Hi there - thanks for checking out the project.

I see what you're saying about not forcing an image that's smaller than the image preview pane to stretch to fill it.

Luckily I think the fix for this should be pretty simple. The image is stretching because of the background-size: cover; property set on the .custom-file-container__image-preview. How about you add the following code to your .css stylesheet so it overwrites the repo's style.

.custom-file-container__image-preview {
  background-size: contain;

  /* You may want to add this too because the background of the image-preview container is set to a white color. */
  background-color: #eee; 
}

Try that and let me know how it goes.

Hi, much thanks for the note back. Welcome!

background-size: contain; will scale the uploaded image preview up so that each side is as large as possible while not exceeding the length of the corresponding side of the container. So if you are using this for one image and the container is 400px by 400px; Then the image will be scaled to that as well. Or if 200px by 300px then the image will be scaled to 200px by 200px. If the image is smaller than the container's smallest bounding side, it's likely to still scale up/blurry to fill to the length of that side.

The preview pane for use with one image would either need to shrink to fit the image size or the image would need to ignore the container size if the container size is fixed. And be centered.

Optimally a preview image for use with one uploaded image would be the size of the original image/keep it's dimensions unless larger than the container. Note: this isn't a problem with multiple files but only when using it on the single image upload. As the multiple creates really small preview images already.

But when using it for the one image upload/preview and using either background-size: cover; or background-size: contain; will still scale a smaller image size up to the preview container size/blurry/lose it's original size.

Anywho looks promising but we've gone in another direction, with something created in house.

Best,

-C

Hey yea I definitely hear what you're saying. In the future we'll take a look at keeping the smaller images at their uploaded size. Thanks for the feedback and good luck!

You as well! Fun project. Always good to see people working in the community and sharing. Best, -C