How to use the image-size function in image-size

To help you get started, we’ve selected a few image-size examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github opencollective / opencollective-website / server / src / controllers / banner.js View on Github external
.then(responses => {
      const images = [];
      for (let i=0;i 0 && posX + avatarWidth + margin > imageWidth) {
          posY += (avatarHeight + margin);
          posX = margin;
        }
        const image = `<img xlink:href="data:${contentType};base64,${base64data}" height="${avatarHeight}" width="${avatarWidth}" y="${posY}" x="${posX}">`;
        const imageLink = `<a id="${user.username}" xlink:href="${website.replace(/&amp;/g,'&amp;')}">${image}</a>`;
        images.push(imageLink);
        posX += avatarWidth + margin;
      }
github ditojs / dito / packages / server / src / storage / ImageSizeTransform.js View on Github external
_transform(chunk, encoding, callback) {
    this.length += chunk.length
    if (!this.done) {
      if (this.limit && this.length >= this.limit) {
        this.error = new Error('Unable do determine image size: Limit reached')
        this.done = true
      } else {
        this.buffer = this.buffer
          ? Buffer.concat([this.buffer, chunk])
          : chunk
        try {
          const size = imageSize(this.buffer)
          this.error = null
          this.done = true
          this.emit('size', size)
        } catch (err) {
          // Do not emit the error right away. Instead keep trying when new
          // data is received until either the image size can be determined,
          // or the end of the stream is reached and `_flush()` is called.
          this.error = err
        }
      }
    }
    callback(null, chunk)
  }
github opencollective / opencollective-frontend / src / server / lib / image-generator.js View on Github external
if (!responses[i]) continue;
        const { headers } = responses[i][0];
        const rawData = responses[i][1];
        const user = users[i];
        if (!user) continue;

        const contentType = headers['content-type'];
        const website =
          options.linkToProfile || !user.website
            ? `${WEBSITE_URL}/${user.slug}`
            : user.website;
        const base64data = Buffer.from(rawData).toString('base64');
        let avatarWidth = avatarHeight;
        try {
          // We make sure the image loaded properly
          const dimensions = sizeOf(rawData);
          avatarWidth = Math.round(
            (dimensions.width / dimensions.height) * avatarHeight,
          );
        } catch (e) {
          // Otherwise, we skip it
          logger.warn(
            'Cannot get the dimensions of the avatar of %s.',
            user.slug,
            { image: user.image },
          );
          continue;
        }

        if (imageWidth > 0 && posX + avatarWidth + margin > imageWidth) {
          posY += avatarHeight + margin;
          posX = margin;
github triplecanopy / b-ber / packages / b-ber-tasks / src / cover / index.js View on Github external
generateCoverXHTML() {
        // get the image dimensions, and pass them to the coverSVG template
        const { width, height } = sizeOf(this.coverImagePath)
        const href = `images/${encodeURIComponent(this.coverEntry)}`
        const svg = Xhtml.cover({ width, height, href })

        // set the content string to be written once resolved
        this.coverXHTMLContent = Template.render(svg, Xhtml.body())
        log.info('cover build [cover.xhtml]')
    }
github opencollective / opencollective-website / server / src / controllers / banner.js View on Github external
request({url: imageUrl, encoding: null}, (err, r, data) =&gt; {
        if (err) {
          return res.status(500).send(`Unable to fetch ${imageUrl}`);
        }
        const contentType = r.headers['content-type'];

        const imageHeight = Math.round(maxHeight / 2);
        let imageWidth = 64;
        if (tier.match(/sponsor/)) {
          try {
            const dimensions = sizeOf(data);
            imageWidth = Math.round(dimensions.width / dimensions.height * imageHeight);
          } catch (e) {
            console.error("Unable to get image dimensions for ", imageUrl);
            return res.status(500).send(`Unable to fetch ${imageUrl}`);
          }
        }

        const base64data = new Buffer(data).toString('base64');
        const svg = `<svg height="${imageHeight}" width="${imageWidth}" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
          <image xlink:href="data:${contentType};base64,${base64data}" height="${imageHeight}" width="${imageWidth}"></image>
        </svg>`;
        res.setHeader('Cache-Control', 'public, max-age=300');
        res.setHeader('content-type','image/svg+xml;charset=utf-8');
        return res.send(svg);
      });
    } else {
github onmyway133 / IconGenerator / src / components / InputComponent.js View on Github external
makeImageDescriptionElement() {
    let text
    if (this.props.file !== null) {
      const size = sizeOf(this.props.file.path)
      const sizeDescription = size.width + 'x' + size.height
      text = this.props.file.name + ' (' + sizeDescription + ')'
    } else if (this.props.error !== null) {
      text = this.props.error
    } else {
      text = 'Drag image onto the above box. Prefer 1024x1024 or larger'
    }
    
    const styles = {
      div: {
        display: 'flex',
        justifyContent: 'center',
        marginTop: '10px'
      },
      text: {
        textAlign: 'center'
github fa7ad / nfeh / src / components / PicturesView.js View on Github external
renderables = _.sortBy(images, [it =&gt; {
        const {width, height} = sizeOf(it)
        return (height / width)
      }]).map((image, idx) =&gt; (
github MixinNetwork / desktop-app / src / utils / attachment_util.js View on Github external
export async function putAttachment(imagePath, mimeType, category, processCallback, sendCallback, errorCallback) {
  const { localPath, name } = processAttachment(imagePath, mimeType, category)
  var mediaWidth = null
  var mediaHeight = null
  var thumbImage = null
  if (category.endsWith('_IMAGE')) {
    const dimensions = sizeOf(localPath)
    mediaWidth = dimensions.width
    mediaHeight = dimensions.height
    thumbImage =
      'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAAA3NCSVQICAjb4U/gAAAAYUlEQVRoge3PQQ0AIBDAMMC/tBOFCB4Nyapg2zOzfnZ0wKsGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAa0BrQGtAub6QLkWqfRyQAAAABJRU5ErkJggg=='
  }
  var buffer = fs.readFileSync(localPath)
  var key
  var digest
  const message = {
    name: name,
    mediaSize: buffer.byteLength,
    mediaWidth: mediaWidth,
    mediaHeight: mediaHeight,
    mediaUrl: `file://${localPath}`,
    mediaMimeType: mimeType,
    thumbImage: thumbImage
github kingdido999 / atogatari / server / src / apis / screenshot.js View on Github external
if (!SUPPORTED_TYPES.includes(file.mime)) {
    ctx.throw(400, `${file.filename} has invalid file type ${file.mime}.`)
  }

  const filenames = {
    small: uuid() + '.jpg',
    medium: uuid() + '.jpg',
    large: uuid() + '.jpg',
    original: uuid() + path.extname(file.filename)
  }

  fileMap.set(file.filename, filenames)

  const fileOriginal = `${UPLOAD_PATH}/${filenames.original}`
  await writeFile(file, fileOriginal)
  const fileSize = sizeOf(fileOriginal)

  if (fileSize.width &lt; WIDTH_MINIMUM &amp;&amp; fileSize.height &lt; HEIGHT_MINIMUM) {
    fileMap.forEach(filenames =&gt;
      fs.unlinkSync(`${UPLOAD_PATH}/${filenames.original}`)
    )
    ctx.throw(
      400,
      `File "${file.filename}" is ${fileSize.width}x${fileSize.height} pixels. 
      Valid screenshot must have width &gt;= ${WIDTH_MINIMUM}px or height &gt;= ${HEIGHT_MINIMUM}px.`
    )
  }

  if (fileSize.width &gt; WIDTH_MAXIMUM || fileSize.height &gt; HEIGHT_MAXIMUM) {
    fileMap.forEach(filenames =&gt;
      fs.unlinkSync(`${UPLOAD_PATH}/${filenames.original}`)
    )

image-size

get dimensions of any image file

MIT
Latest version published 4 months ago

Package Health Score

80 / 100
Full package analysis