Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste image to a web page

I did some research for a wysiwyg editor and found ckeditor that seems to be nice however I need to be able to copy/paste an image to the editor.

I found this web site that do exactly what I need http://pasteboard.co/ so its possible however I cannot find how it's done.

Do you have any ideas or solutions?

I would prefer a solution in pure html5/javascript and avoid any plugin but a silverlight or flash is acceptable too.

like image 571
Yann Lebel Avatar asked Apr 11 '13 23:04

Yann Lebel


People also ask

How do you paste a picture on a website?

To paste the image, move the cursor to where you want to paste and press Ctrl + V or right-click where you want to paste the image and select Paste.

How do I copy an image to my browser?

Right-click on the image and select Properties. Find and highlight the URL address to select it. Right-click and select Copy or press Ctrl + C to copy the image. Paste the address into a new email, text editor, or new browser window.

How do I paste an image into Chrome?

You can take a screenshot of the computer screen, then press Ctrl + V to paste your image into our clipboard extension.


1 Answers

There are two ways you can handle this, the easy way, and the hard way.

The Easy Way: Utilize the Clipboard API. This is an "HTML5" API, but it is only properly supported in Chrome. This will allow you to access a pasted image, from your clipboard, as a Blob. You can then send this Blob to your server via an XHR2 request.

The Hard Way: Unfortunately, this is what you must do for all browsers other than Chrome, and it's not pretty. It involves you creating a hidden content-editable DIV inside of a "paste target element". This will receive the pasted image. You will then need to draw the image onto a <canvas> which will then need to be converted to a Blob. But wait, it gets better. You may also need to proxy cross-domain images (server-side) in some cases (possibly many cases). This may be required if the server hosting the image does not permit CORS requests on the images they host. You can read more about this situation in this MDN article.

A javascript-based uploader I maintain, Fine Uploader, already supports uploading images via paste, but in Chrome only at this time. I figured I would go through the hassle of implementing this in non-Clipboard API browsers if I received enough requests. Quite frankly, though, since handling non-CORS-enabled images in browsers that do not implement the Clipboard API requires proxying the image server-side, it hardly seems like its worth the effort (unless, of course, my user base tells me that they want it).

Hope this helps.

like image 71
Ray Nicholus Avatar answered Oct 06 '22 16:10

Ray Nicholus