blueimp / JavaScript-Load-Image

Load images provided as File or Blob objects or via URL. Retrieve an optionally scaled, cropped or rotated HTML img or canvas element. Use methods to parse image metadata to extract IPTC and Exif tags as well as embedded thumbnail images, to overwrite the Exif Orientation value and to restore the complete image header after resizing.
https://blueimp.github.io/JavaScript-Load-Image/
MIT License
4.45k stars 924 forks source link

add natural size for original image #87

Closed ArcRain closed 6 years ago

ArcRain commented 6 years ago

When we load one thumbnail for image, we want to know the original image size same time. I found the 2nd param in callback get image extra info. So I add a new property in default object.

blueimp commented 6 years ago

Thanks for your contribution, @ArcRain

The loadImage function returns the original image (see API doc), which you can use to retrieve any HTMLImageElement property:

var loadingImage = loadImage(
  'https://blueimp.github.io/Gallery/img/loading.gif',
  function callback (img) {
    console.log(loadingImage.naturalWidth, loadingImage.naturalHeight)
  },
  { maxWidth: 100, canvas: true }
)

Do you have any use case that cannot make use of that return value?

ArcRain commented 6 years ago

I haven’t noticed the return value. You’re right, it’s a good way to retrieve img property. I just think we can get more about original image info from result in callback function.

blueimp commented 6 years ago

Ok, unless you have a solid use case that prevents you from using the return value, I'd prefer to not merge this pull request. Thanks for your understanding.

ArcRain commented 6 years ago

That's OK. Thanks for your reply. :-)

blueimp commented 6 years ago

btw. if you definitely want to have the natural image properties in the callback, you can wrap the function like this:

function customLoadImage(file, callback, options) {
  var loadingImage = loadImage(
    file,
    function customCallback (img, data) {
      data = data || {}
      data.naturalWidth = loadingImage.naturalWidth
      data.naturalHeight = loadingImage.naturalHeight
      callback(img, data)
    },
    options
  )
  return loadingImage
}