how to take a screenshot of a loaded webpage using php? [duplicate] - php

This question already has answers here:
Website screenshots
(18 answers)
Closed 6 years ago.
I need to take screenshots of few web pages and save them in a folder using php.
I want to do this using a php script, is there a way I can render those webpages and take screenshot of a particular area after the website is fully loadedd? - I'm using windows.
I tried using wkhtmltoimage but it takes low quality screenshots and also it takes the screenshot before the whole page is loaded, so some images of the webpage are not appearing in the screenshot, is there a way to do this using php?
Please help php specialists. Thank you

PHP runs on server side, so to be able to take screenshots you need something to render HTML (a browser-like script) and generate a image of it. This script will do that (wkhtmltoimage too):
HTML2PS:
http://freecode.com/projects/html2ps_php
But I still think the best solution will be using javascript. Of course, only the client will have access to the screenshots, and maybe you can upload the image to the server. A client browser is better than any kind of server script.
If that is the case, I would suggest using HTML2CANVAS:
HTML2CANVAS: http://html2canvas.hertzen.com/
The best server solution will be a linux distribution with a google chrome installation and a local script to take screenshots. That is the only way to get accurate screenshots of a web page that may contain javacript/HTML5/animations and other difficult to render stuff.

JavaScript / HTML5 / Canvas
It could be hard to code it only in PHP. In your case I think I would follow those instructions:
Using HTML5/Canvas/JavaScript to take screenshots
PHP
Or if you really want use only PHP, it should help you:
Website screenshots using PHP

Related

How to run PHP file in github page website? [duplicate]

This question already has an answer here:
Why my contact form (php) does not work with GitHub pages?
(1 answer)
Closed 12 months ago.
So i was adding a .php file to my github website with an email sender form, but when i try running it on my website it makes me download it instead of making it run directly in the browser. I was also trying to research something and i saw something about apache, but i don't know how to do it and how it works.
What i tried is to change the extension to .html but unfortunately, it sended me a 405 error "not allowed".
I'm sure the code is right and the only thing is that it dosn't make me run it.
What i want is that the php file, runs on the browser instead of making me download it.
Assuming that by GitHub Website you mean GitHub Pages...
You can't.
GitHub websites are mostly static - you, as a developer, push html or markdown and github provides/generates static content for viewers/browsers.
If you want to have a PHP-based backend, you need to use a different service. Alternatively, if your PHP code can generate static html, you can run it in a github action and push the html into github pages - but once done, it will stay as is until the next run.
Edit: also check the video on their site, which among other things, says:
There are no databases to set up or servers to configure

run PHP code inside HTML without using xampp and apache? [duplicate]

This question already has answers here:
How can I run a php without a web server?
(4 answers)
Closed 2 years ago.
i know there are many similar questions but tbh none of them really fit.
I have no problems running the php part of my Webpage and everything works fine. Now im asking if there is a way to run my webpage including the php part without using having to start apache and typing in localhost.
I just wanna open up my html document with a browser and display it, nothing else. However then, the php code isnt really executed but its necessary for me as it displays an essential table with data. And no I cant use javascript for a very specific reason. I have to use php for my project but its a bit inconvenient having to start apache everytime and i just want to open up the document with firefox for example.
Unfortunately, this will not be possible as PHP is a server side language. As such, you will have to have an application to serve the content. With HTML, you are able to open it directly and display as the client (Browser) is able to read it directly. Browsers do not read PHP - when the server compiles the PHP, it is only sending the DOM (HTML, etc.) to the browser.
Like other people said, PHP is a server side language so you need to run a server on your machine.
But maybe you could try using small local server like the Symfony Local Server
It should be faster to start than apache.

Simple example on how to render a webpage to an Image on the server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Website screenshots using PHP
I cannot seem to find a simple example of how to render a webpage to an image via a web server.
Right now I have a backend running Nginx and serving a PHP app, I also have a reverse Proxy serving Node on the same backend.
What I need to achieve is an API to which I can hand a URL and have it rendered as a PNG then stock somewhere on the hard drive. (If this whole process could be asynchronous that would be perfect.)
What would be the best approach to achieve this?
You will need a renderer, which I don't think is available in a simple PHP library. You could use the engine of Google Chrome, which is open source and has a reasonably usable API to embed it in other applications. I'm not sure how, but you should be able to make it render to an image, or to a canvas from which you can save it yourself.
Not an easy solution, though. Rendering a website is immensely complex.

Open documents(pdf,doc,docx,txt) in browser page using php (without using google docs viewer) [duplicate]

This question already has answers here:
Is there a PDF parser for PHP? [closed]
(7 answers)
Closed 8 years ago.
My question is that i want to open documents(pdf,doc,docx,txt) in browser page using php (without using google docs viewer) can any one help me?
Some of these are doable. Some, not so much. Let's tackle the low-hanging fruit first.
Text files
You can just wrap the content in <pre> tags after running it through htmlspecialchars.
PDF
There is no native way for PHP to turn a PDF document into HTML and images. Your best bet is probably ImageMagick, a common image manipulation program. You can basically call convert file.pdf file.png and it will convert the PDF file into a PNG image that you can then serve to the user. ImageMagick is installed on many Linux servers. If it's not available on your host's machine, please ask them to install it, most quality hosts shouldn't have a problem with this.
DOC & DOCX
We're getting a bit more tricky. Again, there's no way to do this in pure PHP. The Docvert extension looks like a possible choice, though it requires OpenOffice be installed as well. I was actually going to recommend plain vanilla OpenOffice/LibreOffice as well, because it can do the job directly from the command line. It's very unlikely that a shared host will want to install this. You'll probably need your own dedicated or virtual private server.
In the end, while these options can be made to work, the output quality is not guaranteeable. Overall, this is kind of a bad idea that you should not seriously consider implementing.
I am sure libraries and such exist that can do this. Google could probably help you there more than I can.
For txt files I would suggest breaking lines after a certain number of characters and putting them inside pre tags.
I know people will not be happy about this response, but if you are on a Linux environment and have pdf2html installed you could use shell_exec and call pdf2html.
Note: If you use shell_exec be wary of what you pass to it since it will be executed on the server outside of PHP.
I thought I'd just add that pdfs generally view well in a simple embed tag.
Or use an object so you can have fall backs if it cannot be displayed on the client.

How can I generate a screenshot of a webpage using a server-side script?

I need a server-side script (PHP, Python) to capture a webpage to a PNG, JPG, Tiff, GIF image and resize them to a thumbnail.
What is the best way to accomplish this?
See also:
Web Page Screenshots with PHP?
How can I take a screenshot of a website with PHP and GD?
How might I obtain a Snapshot or Thumbnail of a web page using PHP?
You can probably write something similar to webkit2png, unless your server already runs Mac OS X.
UPDATE: I just saw the link to its Linux equivalent: khtml2png
See also:
Create screenshots of a web page using Python and QtWebKit
Taking automated webpage screenshots with embedded Mozilla
What needs to happen is for a program to render the page and then take an image of the page. This is a very slow and heavy process but it can be done in PHP on Windows.
Also check the comments in the documentation article.
For python I'd recommend reading this article. It highlights some of the solutions.
There are services you can also call (via some API) that will return you an image. But usually they cost (WebShots for example)
You'll need to:
read the webpage and all the its multimedia content (images, flash, etc)
utilize a browser rendering engine to render the webpage
take a screenshot and save it as image
first and third steps are easy, the second step is more challenging ;)
If you are using php, you could use imagegrabscreen (PHP 5 >= 5.2.2). Imagegrabscreen: captures the whole screen.

Categories