Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:15:28+00:00 2026-06-13T22:15:28+00:00

I have a Node.js server which dynamically generates and serves small (200×200) thumbnails from

  • 0

I have a Node.js server which dynamically generates and serves small (200×200) thumbnails from images (640×640) in a database (mongodb). I’m using the node-imagemagick module for thumbnailing.

My code works roughly 95% of the time; about 1 in 20 (or fewer) thumbnailed images are corrupt on the client (iOS), which reports:

JPEG Corrupt JPEG data: premature end of data segment

For the corrupt images, the client displays the top 50% – 75% of the image, and the remainder is truncated.

The behavior is non-deterministic and the specific images which are corrupt changes on a per-request basis.

I’m using the following code to resize the image and output the thumbnail:

im.resize({
  srcData: image.imageData.buffer,
  width: opt_width,
}, function(err, stdout) {
  var responseHeaders = {};
  responseHeaders['content-type'] = 'image/jpeg';
  responseHeaders['content-length'] = stdout.length;
  debug('Writing ', stdout.length, ' bytes.');
  response.writeHead(200, responseHeaders);
  response.write(stdout, 'binary');
  response.end();
});

What could be wrong, here?

Notes:

  1. The problem is not an incorrect content-length header. When I omit the header, the result is the same.
  2. When I do not resize the image, the full-size image always seems to be fine.
  3. In researching this I found this and this StackOverflow questions, which both solved the problem by increasing the buffer size. In my case the images are very small, so this seems unlikely to be responsible.
  4. I was originally assigning stdout to a new Buffer(stdout, 'binary') and writing that. Removing it ('binary' will be deprecated) made no difference.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:15:30+00:00Added an answer on June 13, 2026 at 10:15 pm

    The problem seems to have been due to a slightly older version of node-imagemagick (0.1.2); upgrading to 0.1.3 was the solution.

    In case this is helpful to anyone, here’s the code I used to make Node.js queue up and handle client requests one at a time.

    // Set up your server like normal.
    http.createServer(handleRequest);
    // ...
    
    var requestQueue = [];
    var isHandlingRequest = false;  // Prevent new requests from being handled.
    
    // If you have any endpoints that don't always call response.end(), add them here.
    var urlsToHandleConcurrently = {
      '/someCometStyleThingy': true
    };
    
    function handleRequest(req, res) {
      if (req.url in urlsToHandleConcurrently) {
        handleQueuedRequest(req, res);
        return;
      }
      requestQueue.push([req, res]);  // Enqueue new requests.
      processRequestQueue();          // Check if a request in the queue can be handled.
    }
    
    function processRequestQueue() {
      // Continue if no requests are being processed and the queue is not empty.
      if (isHandlingRequest) return;
      if (requestQueue.length == 0) return;
    
      var op = requestQueue.shift();
    
      var req = op[0], res = op[1];
    
      // Wrap .end() on the http.ServerRequest instance to
      // unblock and process the next queued item.
      res.oldEnd = res.end;
      res.end = function(data) {
        res.oldEnd(data);
        isHandlingRequest = false;
        processRequestQueue();
      };
    
      // Start handling the request, while blocking the queue until res.end() is called.
      isHandlingRequest = true;
      handleQueuedRequest(req, res);
    }
    
    function handleQueuedRequest(req, res) {
      // Your regular request handling code here...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a String which gets its value dynamically from other server. Value of
How to send messages from php to node.js? I have a linux server running
I have node.js server which acts like a proxy. It receives requests and forwards
I have a page on which users connect to my node server with socket.io
I have a Node server which I would like to debug. Once I've started
I have exactly one Node server, which is currently running some code. This code
I have a node.js server which keeps track of all clients connected to it,
I have the following in myserver/index.js (which I run with node): server.configure(function() { server.use(express.methodOverride());
I have a Node.js web server running on an embedded Linux system. For authentication
I have the following node.js server-side code: var app = require('http').createServer(handler) , io =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.