Get a screenshot of a page [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Website screenshots using PHP
I have an application where people watch a stream of content (a video stream) and need to click a button each time something happens (suppose they see a red light).
I want to somehow screenshot the stream at the moment where the user clicks the button.
The problem is that the stream is not mine and I am using an IFrame to another page with the stream. The stream is a flash object.
I need to screenshot the page at the moment of click with the flash content using PHP/Javascript and save it on the server.
I saw something that seems similar to what I need but the solution is using C# and .NET.
Programmatically get a screenshot of a page
EDIT:
Idea, if anyone can explain it best here, how could I do that using a plugin/java applet or something that the user might install when entering the site. (The easier the better).

You can't get a screenshot client-side, and certainly not with PHP. PHP runs on your server, not on the client.
The only way to do this would be to write a browser plugin of some sort, or utilize Java.
See this post: Take Screenshot of Browser via JavaScript (or something else)

After a request to view your website has been made by a browser, your PHP code is run and generates the content of the page. By the time your user sees the page, the PHP has finished running, and the content of the page is static as far as that is concerned (although of course can be changed by other means, like ajax or javascript).
That's why this isn't possible at all with PHP, and it's not possible by another means for security reasons (for example, it could reveal any client-side scripts the user is running, etc.).
The only way a screenshot could be taken would be to render it yourself. The only way I can think of offhand would be to get the time on loading of the page, then the time when the button is pressed, and render the page for this long, but this is by no means foolproof and not really a valid suggestion.
I'm afraid that you'll probably have to redesign this portion of your site.

Since you say that the stream is a Flash object, you could simply read it into a Flash enviroment. Flash would allow you to do what you want - saving screenshots, or, better said, images generated by that Flash, i.e. still frames. However, I am not sure if Flash would allow you to make a screenshot of the display incl the OS enviroment.

Related

PHP: Is it possible to save a website, simulating a manual save?

I understand how to use fopen, file_get_contents, etc - but I'm curious to know if there is a way to generate a "manual save" on a webpage? As if I were to physically right-click > "Save As" > to Desktop?
I'm asking this because I'm trying to learn more about securing data. I didn't think it was possible to hide source code, but I recently found out that can be done by putting the code in JavaScript. So the "hidden code" won't appear in View Source, but it will still appear in Firefox Web Developer Tools and such.
So let's say I'm using JavaScript to secure my source code (as best as can be, that is). If someone were to Ctrl+U (View Source) my webpage (or fopen/file_get_contents), they would see:
<div class="StartofSecretSourceCode"></div>
Instead of:
<div class="StartofSecretSourceCode">
<div class="Something">Some stuff and things</div>
</div>
In order for the user to see the full source code in this section, they would have to inspect the code in Firefox/Firebug/etc or manually save the webpage.
So I'm wondering if it's possible for someone to automate "manually saving" the webpage using PHP (or something else). And if so, I'm curious to know how you would do that in PHP. I hope that my question makes sense >.<
You can use guzzle library or curl to send request and save response (in the same way as browser - literally you can simulate browser) - so you can sent GET request and download page (and its assets like js files) and save it to disc from php side - it is no problem.
However if you want to protect your code dont send it to user but EXECUTE it on server side and return to user only RESULTS (in html file). This is standard and good way of code protection

Monitor certain webpage load times (including all images, css, etc..) using PHP

This is a very broad question, so I'm just looking for the best way of doing this.
I want to periodically monitor certain pages on my website.
I am looking to write a PHP script which will load the page as if it is being loaded in a browser. So that means, it loads all CSS, Javascript, Images, Videos, etc...
I want to just get the load time of these pages and then email the results to myself in a crontab. For this I was going to use microtime() and a phpMailer.
Does anyone know of a script to load a complete page, or have any suggestions on how to go about this?
Thanks.
What if the page has dynamic content? You will also need to execute all the JavaScript and fetch all CSS images to get the final amount of time. I believe that is impossible using only PHP.
A php script you run from the same server you host your site will give you abnormal readings (very low) since it's loading on the first hop essentially. What you really would want to do is run a script from various servers outside of your own. There are also limitations with what php can see ie JS and JQuery etc.
The simplest is to check from your home pc, using jmeter. You set your home browser to use it as a proxy and go to whichever website you want. Jmeter will record statistics. When you are happy you can choose to save the stats.
This avoids the problems of handling JS and JQuery through a script.
This could get very complicated. You'd basically have to parse the HTML, and then there's tons of edge cases, like JS including resources, etc... I would definitely recommend using something like the network tab of Chrome's dev tools instead.

Class or function to automatically generate PDF and Print

Anyone know any Javascript or PHP function to generate PDF and print (printer) automatically.
Excuse my ignorance, I searched on google about it and can not find sufficient documentation.
Many many Thanks Guys
What are you generating your PDF from? I presume that what you want to do is generate a PDF from e.g. a form submission, then print it on the user's computer?
You cannot print from PHP (well there are horrible ways of doing it, but don't) but I doubt it would help you even if you could - it would be printed on the server side, and I imagine you would be wanting to print on the client (i.e. browser) computer.
You can generate PDF's in PHP (have a look at FPDF) and send them to the browser, and you can print a web page in javascript, but to combine the two would be tricky, if it is possible at all. You certainly can't do it without prompting the user.
If you were to generate the PDF, then open it in an iframe, you could maybe call something through javascript to prompt the user with the standard printing options dialog, but that would be as far as it goes. It wouldn't work everywhere, if it worked anywhere, which I somehow doubt.
to create PDFs, theres the great FPDF-library.
printing automatically fortunately isn't possible - just imagine this would be possible and every f***g website could (in addition to the annoying popups and stuff) print out something (advertisements most of the time) on your printer.
EDIT :
if you have control over the clients, you could write a little batch-script like (not tested)
AcroRd32.exe /t %1 printername
and then set pdf-files in your browser to open automatically with this "programm" wich should then print the file without a print-dialogue.
note that you need access to the clients for this and it isn't tested. in theory this works: i did something very similar once to print out labels directly from the browser, but this was a few years ago using WinXP, don't know if this still works on Win7 (or whatever you're using).

Curl preg_match

We are downloading images to our computers when we open new webpages. For example: If a webpage has an image(image.jpg), our computer downloads it while we are surfing that page.
Some webpages are using ajax methods. For example: You don't see an image on the page's source codes, however your computer downloads an image. Because, if you click a link on that page, ajax will be showing that image...
Let me show an example:
<div id="ajax_will_load_image_here"></div>
Okay, how can php curl see (or download) that image? Curl can't see that image when I try to use preg_match function. Actually there is an image. I want to download that image by using php curl. Any advice?
If i understand the question correctly there is no convinient way of doing that.
Your crawler/spider would have to parse the website and evaluate javascript.
There are libraries for that but support is very limited.
There are however methods where an actual browser is used to evaulate the page (without displaying it but setting proper environment variables like resolution etc).
Then the generated source including javascript dom modifications is available.
This is for example how the google search previews are generated.
But if you require user interaction it gets pretty specific and complicated.
I am sorry to dissapoint you, but using curl and preg metch the old school way we used to when javascript was not yet so common wont work.
However for most legit use cases this is more than sufficient and websites are today more and more designed to be non-javascript compliant. Especially the content for crawling purposes. It is a must in search engine optimization, and which website doesnt want that?

How can I convert my current page to pdf after some content dynamically added via AJAX?

I have found some libraries or web services in PHP that does the job. The problem is that the conversion is done when the page is fully loaded, I would like to convert the page to PDF after some content dynamically added via AJAX in onload event.
Thank you very much,
Omar
You could post back document.getElementsByTagName('html')[0].innerHTML to the server (possibly using AJAX) and generate a PDF from that.
When you add content with AJAX, that's only happening on the client's machine, so the server-side PHP conversion code will not work with it. You'll need to come up with a server-side method of including the dynamic content if you want to do the PDF creation on the server.
You could possibly implement an AJAX call that sent the page content/state back to serverside, after the dynamic contens is added, where it could be rendered as a pdf. You might not need all the page, depending on where this "dynamic content" is going to go.
Seems a bit messy, but without knowing the project it's hard to say whether there could be a cleaner method to do what you're trying to do.
This is a client-side requirement due to the Ajax situation. If they're using a Mac they can simply save as PDF but otherwise you have little control over what they can do.
What you can do however for a server-side implementation is keep a record of the page and all their AJAX requests since loading that page, and construct the HTML and PDF on the server from that record if it is requested. Of course this is not a simple task and quite overkill. You'd probably be better off having a different mechanism to create a server-side PDF report of a page other than turning HTML into PDF.
If your page can be updated solely on the client with no trip to the server, you'll have to post back up to your app. That way you'll have all the content and will not have rebuild, which would not be possible if there are client side only interactions.
Wow, thank you everyone I didn't know that this is comunity is so active. To answer Beepcake about the project:
When the page loads the app retrieves, from more than 40 servers, biological information via AJAX request, then a unique view is displayed where you can manipulate the graphic with many options.
So, the cool thing will be to print when the user makes his own version of the graphic. I think that the best solution is to POST the entire HTML with document.getElementsByTagName('html')[0].innerHTML as RoBorg has said and then generate the PDF with a library such as dompdf

Categories