Home > Software engineering >  How to convert .eps to .png format in node js?
How to convert .eps to .png format in node js?

Time:09-10

I have .eps image URL , Since I'm working on html ,It will not supported by <img /> tag.Is that possible to convert the .eps to .png/.jpg.If yes please guide me through proper package or method.

CodePudding user response:

You could use http://aheckmann.github.io/gm/:

  1. Install imagemagick
  2. npm add gm
  3. Use script like this:
var gm = require('gm').subClass({ imageMagick: true });

gm("in.eps").write("out.png", function (err) {
  if (!err) {
    console.log('done');
  }
});

CodePudding user response:

As of 2022, there is no browser standards for EPS files, so you will have to convert the file to a different format. Thankfully, since you marked the question as using Node.js, I will assume you have access to server side JavaScript, in which case this becomes quite easy.

Whatever method you use to serve your content, you will need to create an endpoint receiving the URL to convert. For example:

http://localhost:3000/img/convert?url=https://path.to/image.eps&format=jpg

On the server side, obtain the request params url and format, then use GraphicsMagick to stream the file back to the client. For example:

import fs from 'fs';
import gm from 'gm';

const imageMagick = gm.subClass({imageMagick: true});

inside your server route handler:

// req = the server request object
// res = the server response object
const { url, format } = req.query;
const source = fs.createReadStream(url);

imageMagick(source)
  .stream(format)
  .pipe(res);

Then use that endpoint for your images on the client:

<img src="http://localhost:3000/img/convert?url=https://path.to/image.eps&format=jpg" alt="EPS image" />

The key advantage of this solution is a uniform file format for your images, with the possibility of adding more parameters to control the size and quality, etc. of the files you display on the page. I.e. this method allows you to easily create thumbnails, or responsive images for when hi-res images are displayed on small screens or mobile devices, etc.

Obviously, a lot of details have been left out because the question lack specifics, but this should get you started in the right direction.

  • Related