Welcome to WebmasterWorld Guest from 34.204.203.142

Forum Moderators: phranque

Message Too Old, No Replies

Uploading a photo pasted in a contenteditable DIV

     
12:50 am on Jun 27, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1205
votes: 120


I have a contenteditable section... if someone copy-and-pastes a picture, what's the best way for me to post it on my end?

I'm using jQuery, PHP, and Perl, so I can work within any of those.

I'm pretty sure that it will come through as a data URI, like this:

<input src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAg...">


but I'm not sure if these are browser specific or what. And, obviously, these would load inline instead of calling a separate HTTP connection (maybe a good thing?), and wouldn't cache (maybe a bad thing). But they would be compressed via GZIP, so maybe it's worth the trade off of not caching?

So what do you guys and gals think, should I just allow these as-is, or should I try to find a way to upload the image to the server and link to it with a regular <img...> tag?

If I try to turn it in to an actual image, I'm thinking it would just be something like this in Perl... ?

$id = $id_of_post_being_submitted;

$image = $ext = $post_being_submitted;

$image =~ s#src=("|')(data:image/.+?)\1#$2#;
$ext =~ s#data:image/([^;]+)#/$1/;

open $fh, '>' . $id . '.' . $ext or die $!;
binmode $fh;
print $fh $image;
close $fh;

$post_being_submitted =~ s#src=("|')(data:image/.+?)\1#src="https://example.com/$id.$ext"#;


I just typed that for the example, of course, it's not tested or anything. And it doesn't allow for multiple images in the same post, anyway, so it's just a starter idea...
12:54 am on June 27, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1205
votes: 120


Well, in retrospect I think I'd have to do this with a JPG:

use MIME::Base64;

$image_src =~ s#src=("|')(data:image/.+?)\1#$2#;
$image = MIME::Base64::decode_base64($image_src);

open $fh...


So I should count the usage of the module in as a variable, too.
4:28 am on June 27, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:10573
votes: 1125


With Article 13 looming the wings there might be MUCH more processing required before accepting any UGC images. Just sayin'.
4:36 am on June 27, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1205
votes: 120


That's just in the EU, though, right? My target is strictly a small region in the US, and I block most non-US IP addresses at the firewall.

I already allow images in the classifieds, and there's no way that I'm verifying those 2,000 posts a day for copyright... plus, the Telecommunications Decency Act in the US prevents me from having to do so (unless Trump keeps messing with it, but that's a different story). So allowing people to paste images in the message board, too, is a relative drop in the bucket.
2:16 am on June 28, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:10573
votes: 1125


Don't confuse the TDA with copyright! These are not equal!

Art 13 is merely the fore front of what will happen everywhere and don't think otherwise. Prepare for it now.

Trump has nothing to do with it, sorry you even brought that up ... however, politics IS the reason to think twice about what kind of content you will allow users to generate on your platform. If g and fb are worried, so should you be. The times they are a changin'(with apologies to Bob Dylan).

Safe Harbor still exists in the USA, but is under attack internationally from several directions. This is not an immediate threat, but one to be aware of and plan for. Congress is looking into the issue as well.

This is an extreme topic drift and has nothing to do with the OP query. Apologies for that ... but is related in one small way in that accepting images (ugc) in the future might require ADDITIONAL processing besides "how to inject it auto-magically with a few lines of code".
2:27 am on June 28, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1205
votes: 120


Excellent points, tangor! But I guess I'm trying to meet a demand while I can, and I'll have to just deal with policy changes as it becomes necessary :-(
1:27 pm on June 28, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:10573
votes: 1125


Understood. I do feel your pain! (sigh)