I have an html form where users insert an image (input). My users may be not advanced users, so they may want to insert many different types of image format. However, I only want the user to send jpg/gif/png files. One option is just block other file types, however, I was wandering if I could convert the files in the client side (I don’t want to send a very large file, and convert it on the server). Therefore, the only way I can think of is using javacript.
So, does any one know how to make image format conversions in javascript? And put this image result as the value of an html:input?
Thanks!
tl;dr: Don’t bother
This might be theoretically possible, but I would recommend against it very strongly. It’s possible to create a javascript
Imgelement which refers to an URL the user has typed in. You can then draw this image in an HTML5 canvas.You can then manually access the data on the canvas and analyze/convert the image to the approriate format. It might then be possible to send this Base64 or URL-encode to a server which could then return the image to the client.
This is of course completely crazy and should NOT be attempted. This solution would require implementing JPG compression in javascript which, although technically possible, is probably not feasible because of browser constraints (eg. speed).