Link to home
Start Free TrialLog in
Avatar of DMTrump
DMTrumpFlag for United States of America

asked on

using html2canvas to capture external webpage

At http://html2canvas.hertzen.com/screenshots.html the author of html2canvas demonstrates using his code to capture screenshots of external webpages.  No where that I can find does he explain or demonstrate how he makes that page work.  

There doesn't seem to be an answer to that here in EX-EX but on a concurrent site I found this conversation:

<concurrent site>------------------------------------------------------------------------------------
but i want to be able to pass an url and make a screenshot of this page. Like they do in their example page on: http://html2canvas.hertzen.com/screenshots.html

If anyone could give me a hint how to get started with that, I would be really glad.
       
Like in php i would call file_get_contents() to get the page and then echo it and use it in the ajax-success-callback? Did i get this right? –  movsky Dec 18 '12 at 12:23
       
I don't remember the PHP offhand, sorry. –  DNS Dec 18 '12 at 12:51
       
hm, okay, could you at least tell me how the call for html2canvas would look like: I have now something like: $.ajax({ type: "POST", url: "ajax.php", data: { action: 'getPageContent', url: 'google.de'; } }).done(function(html) { // How to use it from here? }); –  movsky Dec 18 '12 at 13:03
       
I don't know; it depends on how you've created your script, but that looks roughly correct. I would take a look through the jQuery AJAX docs. –  DNS Dec 18 '12 at 13:05
---------------------------------------------------------------------------------</concurrent site>

There were no further comments to that thread and I'm lost as to how to implement that suggestion from movsky .  And as usual, the concurrent site thread just petered out without resolution.  (That's why I love Ex-Ex and hate S/O!)

Can anyone help me recreate the functionality of http://html2canvas.hertzen.com/screenshots.html ?
Avatar of DMTrump
DMTrump
Flag of United States of America image

ASKER

Thanks, leakim971 - I appreciate the help and will follow that rule from now on....
Avatar of leakim971
// How to use it from here?

Open in new window


imagine you've a div in your page :
<div id="put_my_page_here"></div>

.done(function(html) { $("#put_my_page_here").html(html) });
Avatar of DMTrump

ASKER

Thanks, but I'm not sure that would do what I want.  The user of my page needs to be able to type in the name of an external site and get an image of that page that I can then pass on to another function.  I want my app's users to be able to create a small thumbnail of the logo or other identifying portion of another web page.

Say my site is at www.mysite.com and my user wants to snip a portion of the home page at www.cnn.com.  The user will type www.cnn.com into an input and click a button to put the top 200 pixels of cnn.com (or some other site) into an image then use my cropping tool to enlarge it and select the small portion of the page that contains the CCN logo.  See my stub page at www.theswpt.com.  The example cnn page (for example purposes only) is a jpg file already on my server.
you need help of a server (called proxy) to do that, it's not possible using only javascript

see limitations here : http://html2canvas.hertzen.com/documentation.html

once you understand that, you can use a proxy using file_get_contents(), you pass the URL of the site the user want to crop, it send back the HTML to you, you pass this HTML to htm2canvas to let it do the job as usual
Avatar of DMTrump

ASKER

Yes, I understand that limitation but I don't know how to do that.  I am hoping to see some example code  - especially the coupling between the proxy and ajax to put the image into the my target
eh eh, don't think it's something complicated

php proxy (myproxy.php) :
<?php
   echo file_get_contents( $_POST["url_prodived_by_user"] );
?>

The code example you're looking for is here : http://html2canvas.hertzen.com/site/console.js

the iframeLoad function
and
the  $('#getscreenshot').click(function(e){ // bla bla part });
Avatar of DMTrump

ASKER

So my user submits a form that returns the url of the site desired in $siteurl

and I do the following

$im = file_get_contents($siteurl);

imagejpeg($im, 'pageimage.jpg');

// Free up memory
imagedestroy($im);
Avatar of DMTrump

ASKER

Then I pass pageimage.jpg to my cropper?

oops!

We crossed our posts and I'm now studying the console.js code.
your idea is good too
Avatar of DMTrump

ASKER

Okay, I'm going to try this out - thanks for the help so far - I may have more questions later so I'm not going to close the question yet.  (I have to go pick my little granddaughter from school in a few minutes!  That's even more fun than coding!!!!!)
This will retrieve the rendered HTML -- what you would expect to see in view source.

$im = file_get_contents($siteurl);

That's probably not what you want.  What you might want instead is to load the remote site into an iFrame and then use something like imageGrabScreen().  I say might because I do not know if this will work -- I've never tried it, so it will be a bit of a research project.
no, Sir Paseur, it will not work, you will get a security error
Oh, well... Like I said, I've never tried it.
The error is : Uncaught Error: SecurityError: DOM Exception 18
Avatar of DMTrump

ASKER

I've written many thousands of lines of embedded instrument code in C, C++ and 68HC11 Assembly.  I've written and published a dozen or so windows apps in Delphi, and created several fairly complex web apps in PHP and I'm just completely lost trying to put something together from these snippets of javascript that actually works.  In spite of all that experience, I'm a rank beginner in Javascript!

I give up for now!

I've found a commercial service (grabzit) that will do temporarily.  I still will need this code someday soon and a person who can give me a bare bones php page that accepts the POST of a URL from a form in another page and saves an image of the url's page to the server woth a random filename will get my 500 points.  So I'm letting this question ride for a while.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DMTrump

ASKER

Thanks for all the effort you put into trying to help me.  I have decided that the commercial solution better fits my needs.  It is more inclusive of the sites it can handle for my clients than html2canvas claims to be.  That said, I'll still work this through someday as I won't admit defeat.  But I'm in a rush and I'll go with Grabzit for this task.  Thanks a lot, anyway.
Thank you Sir Paseur for your kind words.