1

I’m using cropit.js to allow a visitor to upload a large JPEG or TIFF image which is intended for print. I print at 300DPI.

The dimensions of the cropit.js HTML 5 canvas are determined by the visitor (using a numberfield in a previous script, these dimensions are passed as a var to the cropit.js HTML 5 canvas. This all works great.

Some images will not initially be suitable or require resizing by the visitor. Cropit.js should be great for this.

My query: I’d like to show the effective size of an uploaded image, its size in comparison to the HTML 5 Canvas (which is a representation of the final printed image @ 300DPI). The maths / calculations required blow my mind. Can anyone help?

  1. Can code detect screen DPI? No it seems not.
  2. Can a calculation be done to show the effective print size? Seems not.
  3. What maths do I require to display an emulated size (taking into account the resolution of the uploaded image), and the dimensions entered by a visitor?

This can be easily exemplified in Photoshop, where you make a 'canvas' (photoshop canvas size) of say, 210 x 210mm at 300DPI. If you then drop a 1000x2000px image of 72DPI into the canvas... it doesn't quite work, user either has to upsize, and crop. This is the functionality I'd like to mimic with cropit.js

    <script>
  $(function() {
    $('.image-editor').cropit({
      imageState: {
        src: 'imagedir/image1000x2000px.jpeg',
      },
    });

    $('.preview-wrapper').cropit('previewSize', { width: ''+ var_numberfield_width+'', height: ''+var_numberfield-height+'' });

    $('.export').click(function() {
      var imageData = $('.image-editor').cropit('export');
      window.open(imageData);
    });
  });
</script>

As I work on a complete answer I thought I might bring together a load of links I'm finding interesting reading. Might be helpful in future:

Community
  • 1
  • 1
Callum
  • 554
  • 2
  • 7
  • 18
  • 2
    ???canvas?? you mean real world canvas or HTML5 element canvas? DPI has no meaning for the HTML5 canvas. You set a print job by specifying width and height (inch or cm), the DPI is up to the print quality and if a client wants to upload a 1 by 1 pixel image to be printed at 10 by 10 inches you show the print job as 10" by 10" @ 300DPI . If it is a real canvas you mean it must have a physical size, you use the print size divide canvas size to scale the image for the onscreen representation, what ever size that may be. – Blindman67 Oct 15 '16 at 18:24
  • If you're getting fuzzy / blurry images you should check out this post, if you haven't already. http://stackoverflow.com/questions/15661339/how-do-i-fix-blurry-text-in-my-html5-canvas – Larry Turtis Oct 15 '16 at 18:39
  • @Blindman67 - sorry i re-read my question and it seems i wasn't very clear initially. have re-written, yes I believe cropit.js uses HTML Canvas, we're not talking about real world canvases (which i don't print, nasty things they are!). thanks for your input. – Callum Oct 15 '16 at 19:02
  • @LarryTurtis interesting stuff, thanks! – Callum Oct 15 '16 at 19:02
  • This may help you calculate an image size equivalent of output device's resolution (i.e. printer): https://stackoverflow.com/questions/17025603/what-is-the-best-practice-to-export-canvas-with-high-quality-images/17035046#17035046 –  Oct 16 '16 at 14:26
  • @K3N very helpful! – Callum Oct 17 '16 at 09:26

1 Answers1

1

can code detect screen DPI?

Sadly No, there is no way to detect the real physical size of a pixel and hence the display size and DPI. Though you can have a list of known devices/screens/resolutions and ask the client what hardware they have, but that has as many problems as there are different devices.

Can a calculation be done to show the effective print size?

As this related to the first question, the answer is no.

I wonder if there is a clever way, with JQuery, or HTML5 to read the 'dropped' image dimensions, to calculate the DPI and display the relative size of the image within the HTML5 Canvas?

To get the resolution (not DPI) of the image, you just load it

var image = new Image();
image.src = /*dropped image URL*/
image.onload = function(){ // need to wait till it has loaded
     width = image.width;  // number of pixels across
     height = image.height;  // number of pixels down.
}

The DPI is only loosely tied to the resolution. You can not increase the image resolution. Doing so will only blur the image. The image in its dropped form is at the max DPI it will ever be. DO NOT SCALE the image before upload in any way. (Unless it is too big)

The image you display in the canvas is not what you will up load. In the snippet above the image can be drawn at any resolution.

Say your canvas is 1024 by 1024 and the image loaded is 4096 by 2048. You need to show the image on the low res canvas by uniformly scaling it and then rendering it at that scale. Rendering it to the canvas does not effect the image, it still retains its original resolution.

To get the correct scale to show the whole image

 var scale = Math.min(image.width / canvas.width, image.height / canvas.height);
 // to display the image on the canvas
 ctx.drawImage(image,0,0,image.width * scale,image.height * scale);

When working with images on computers forget about the DPI, it is meaningless as you have no way to know how big a dot (pixel) is. DPI is a function of the printer.

You can once the client has selected a print size give an effective DPI (I prefer PPI (pixels per inch)) which is the number pixels divide the print size in inches. For example a reasonable image 5184 by 3456 pixels on a 7" by 5" print will give a DPI of ~740DPI but if you change the print size 16" by 12" the DPI becomes ~432DPI. All that has changed is the print size, the image has not changed at all.

Blindman67
  • 51,134
  • 11
  • 73
  • 136
  • this is incredibly thoughtful. i will need to have a strong coffee in the morning to get my head around your comment. how does a website, such as https://hub.theprintspace.co.uk/OnlinePrintOrders calculate 'effective DPI' - when you upload an image this site (others too) can show a warning if the resolution of the file (intended for print) is too low, compared to dimensions entered by a user. just a query! really interested in your input. – Callum Oct 15 '16 at 21:04
  • @Callum once you have the print size you can then get the DPI which is image.width / print width in inches. Good DPI depends on the viewing distance. Eg an image that covers the size of a 10 story building is ~0.1DPI but as no one gets close its fine. For small print 5by7 anything below 300 or 150 DPI should have a warning. Human vision can resolve 0.02° deg so at 1 meter we can see something as small as 0.3mm (approx 25DPI) – Blindman67 Oct 15 '16 at 21:19
  • totally hear you re. confusion over PPI and DPI. I'll try to re-phrase now. Can you think of a calculation to display the effective PPI once I've define the canvas as (mm size -> pixels @ 300PPI), I drop the image on, which at 100% = 300PPI effectively, but once I upscale the image that changes, say I increase to 200% size, the effective image PPI is now 150. Is there a HTML 5 canvas 'hook' or function to allow display of such a calculation? And an error? Thanks. – Callum Oct 17 '16 at 09:42