{"id":28060,"date":"2022-06-17T08:37:15","date_gmt":"2022-06-17T08:37:15","guid":{"rendered":"http:\/\/capture-any-website-screenshot-using-php"},"modified":"2022-06-17T08:37:15","modified_gmt":"2022-06-17T08:37:15","slug":"capture-any-website-screenshot-using-php","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/","title":{"rendered":"Capture Any Website Screenshot Using PHP"},"content":{"rendered":"<div class=\"wp-block-cloudinary-markdown \"><h2>Introduction<\/h2>\n<p>Screenshots let you capture exactly what you\u2019re seeing on your screen to share with others or reference later. This article will demonstrate how much can be achieved using PHP and also manipulate Cloudinary services with such a service<\/p>\n<h2>Github<\/h2>\n<p>You may also use Github to view the project\u2019s <a href=\"https:\/\/github.com\/musebe\/php-screenshots\/tree\/main\">Github<\/a> repo.<\/p>\n<h2>Phpsandbox<\/h2>\n<p>To see a working application demo, Click here <a href=\"https:\/\/phpsandbox.io\/e\/x\/hl7bc?layout=EditorPreview&amp;defaultPath=%2F&amp;theme=dark&amp;showExplorer=no&amp;openedFiles=\">Phpsandbox<\/a>.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Entry-level html and php knowledge.<\/p>\n<h2>Setting Up the Sample Project<\/h2>\n<p>Create a new folder: <code>website2imgphp<\/code>\nInside the new folder\u2019s terminal use the command <code>composer init<\/code>. You need to have <code>php<\/code> and <code>composer<\/code> downloaded to your machine.<\/p>\n<p>Follow the composer procedure which will help handle the necessary project dependencies. When asked to search for a package, search for <code>Cloudinary<\/code><\/p>\n<h2>Cloudinary<\/h2>\n<p><a href=\"https:\/\/cloudinary.com\/?ap=em\">Cloudinary<\/a> refers to an end-to-end image and video management solution for websites and mobile apps, covering everything from image and video uploads, storage, manipulations, optimizations to delivery. Our app will use the media file online upload feature.\nTo begin, click <a href=\"https:\/\/cloudinary.com\/console\">here<\/a> to set up a new account or log in to an existing one. We use the environment keys from the user dashboard to integrate Cloudinary with our project. We will create a file named <code>env<\/code> and use the guide below to fill in the project configuration.<\/p>\n<pre class=\"js-syntax-highlighted\"><code>      CLOUDINARY_NAME=\n      CLOUDINARY_API_KEY=\n      CLOUDINARY_API_SECRET=\n      GOOGLE_API_KE=\n<\/code><\/pre>\n<p>Our app\u2019s home component will include 2 sections; <code>html<\/code> and <code>php<\/code>. Start by including the following in the <code>index.php<\/code> directory<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\">\"index.php\"\n\n\n<span class=\"hljs-meta\">&lt;!doctype <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"utf-8\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"viewport\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">\"width=device-width, initial-scale=1\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>Website screenshot<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n \n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@4.6.0\/dist\/css\/bootstrap.min.css\"<\/span> <span class=\"hljs-attr\">integrity<\/span>=<span class=\"hljs-string\">\"sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L\/Z6nronJ3oUOFUFpCjEUQouq2+l\"<\/span> <span class=\"hljs-attr\">crossorigin<\/span>=<span class=\"hljs-string\">\"anonymous\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"theme-color\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">\"#7952b3\"<\/span>&gt;<\/span>\n \n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n    \n \n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"container py-3\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"pricing-header p-3 pb-md-4 mx-auto text-center\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"display-4 fw-normal\"<\/span>&gt;<\/span>Website screenshot and save to Cloudinary<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">form<\/span> &gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"form-row\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"form-group col-md-4 offset-4\"<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span>  <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"sr-only\"<\/span>&gt;<\/span>Site<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"form-control\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"site\"<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\">\"https:\/\/site.com\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"form-row\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"form-group col-md-4 offset-4\"<\/span>&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"submit\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"btn btn btn-dark\"<\/span>&gt;<\/span>Capture shot<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">form<\/span>&gt;<\/span>\n \n \n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"display-6 text-center mb-4\"<\/span>&gt;<\/span>Capture shot<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n \n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"col\"<\/span>&gt;<\/span>\n        <span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">if<\/span>(<span class=\"hljs-keyword\">isset<\/span>($_GET&#91;<span class=\"hljs-string\">'site'<\/span>])){ <span class=\"hljs-meta\">?&gt;<\/span><\/span>\n            <span class=\"hljs-comment\">&lt;!-- Display the ima using the screenshot url capture from response --&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">img<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"img-fluid\"<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"&lt;?=$snap&#91;'data']?&gt;\"<\/span> <span class=\"hljs-attr\">alt<\/span>=<span class=\"hljs-string\">\"snap\"<\/span>&gt;<\/span>\n        <span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> }<span class=\"hljs-keyword\">else<\/span> { <span class=\"hljs-meta\">?&gt;<\/span><\/span>\n            <span class=\"hljs-comment\">&lt;!-- If site is not yet set just provide message to enter site name --&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"text-muted text-center mb-4\"<\/span>&gt;<\/span>Site name enter<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n        <span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>  } <span class=\"hljs-meta\">?&gt;<\/span><\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n \n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>The code above creates a webpage titled <code>Website screenshot<\/code> and also imports bootstrap for its css features. We want a user to be able to fill a form with any website URL and receive an image containing the contents of the webpage. The webpage should also be backed up using the cloudinary online feature. The UI looks like the below:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/dogjmmett\/image\/upload\/c_limit,w_2000\/f_auto\/q_auto\/v1655397544\/UI_bcnj3m.png\" alt=\"complete UI\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"1904\" height=\"904\"\/><\/p>\n<p>Now to make the components function.<\/p>\n<h2>Cloudinary<\/h2>\n<p><a href=\"https:\/\/cloudinary.com\/?ap=em\">Cloudinary<\/a> refers an end-to-end image- and video management solution for websites and mobile apps, covering everything from image and video uploads, storage, manipulations, optimizations to delivery. Our app will use the media file online upload feature.\nTo begin, click <a href=\"https:\/\/cloudinary.com\/console\">here<\/a> to set up a new account or log in to an existing one. We use the environment keys from the user dashboard to integrate Cloudinary with our project. We willCreate a file named <code>env<\/code> and use the guide below to fill in the project configuration.<\/p>\n<pre class=\"js-syntax-highlighted\"><code>      CLOUDINARY_NAME=\n      CLOUDINARY_API_KEY=\n      CLOUDINARY_API_SECRET=\n      GOOGLE_API_KE=\n<\/code><\/pre>\n<p>Use autoload to load all the dependencies install with php composer<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n\n<span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-keyword\">include_once<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/vendor\/autoload.php'<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Cloudinary<\/span>\\<span class=\"hljs-title\">Cloudinary<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Cloudinary<\/span>\\<span class=\"hljs-title\">Configuration<\/span>\\<span class=\"hljs-title\">Configuration<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Cloudinary<\/span>\\<span class=\"hljs-title\">Api<\/span>\\<span class=\"hljs-title\">Upload<\/span>\\<span class=\"hljs-title\">UploadApi<\/span>;\n \n<span class=\"hljs-meta\">?&gt;<\/span>\n \n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Confirm that the get request has the required form variable filled.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n<span class=\"hljs-keyword\">if<\/span>(<span class=\"hljs-keyword\">isset<\/span>($_GET&#91;<span class=\"hljs-string\">'site'<\/span>])){\n  \n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Use the following  <a href=\"https:\/\/developers.google.com\/speed\/docs\/insights\/v5\/get-started\">link<\/a> to\nGenerate the Google API key for website insights.\nAttache the api key to the environmental variable so as to use it in the future and ensure the security of the application secrets.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n<span class=\"hljs-keyword\">if<\/span>(<span class=\"hljs-keyword\">isset<\/span>($_GET&#91;<span class=\"hljs-string\">'site'<\/span>])){\n  $api =getenv(<span class=\"hljs-string\">\"GOOGLE_API_KE\"<\/span>);\n    $site =$_GET&#91;<span class=\"hljs-string\">'site'<\/span>];\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Build url to send the request to capture the website details to google\nusing the google api key together with the user input url for the site.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n$adress=<span class=\"hljs-string\">\"https:\/\/pagespeedonline.googleapis.com\/pagespeedonline\/v5\/runPagespeed?url=$site&amp;category=CATEGORY_UNSPECIFIED&amp;strategy=DESKTOP&amp;key=$api\"<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Initialize the curl request with the earlier generated url. In preparation for sending a get request to google with the site url.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n\n\n$curl_init = curl_init($adress);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Setup curls options for the get request<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\ncurl_setopt($curl_init,CURLOPT_RETURNTRANSFER,<span class=\"hljs-keyword\">true<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Execute the curl request and capture the curl response for extraction of the website details\nspecifically the screenshot.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n\n$response = curl_exec($curl_init);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>It is always a good practice to close the curl channels to avoid hoarding server resources.\nafter the response has been received.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\ncurl_close($curl_init);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Decode the json response received into a key-value pair php array.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n$googledata = json_decode($response,<span class=\"hljs-keyword\">true<\/span>);\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Extract image data from the decoded response to get.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n$snapdata = $googledata&#91;<span class=\"hljs-string\">\"lighthouseResult\"<\/span>]&#91;<span class=\"hljs-string\">\"audits\"<\/span>]&#91;<span class=\"hljs-string\">\"full-page-screenshot\"<\/span>]&#91;<span class=\"hljs-string\">\"details\"<\/span>];\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Isolate the captured screenshot from the data extracted in the previous section.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-string\">\"index.php\"<\/span>\n\n$snap =$snapdata&#91;<span class=\"hljs-string\">\"screenshot\"<\/span>];\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Initialize cloudinary instance globally across the application with secret keys from the env<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"> <span class=\"hljs-string\">\"index.php\"<\/span>\n\nConfiguration::instance(&#91;\n\t\t<span class=\"hljs-string\">'cloud'<\/span> =&gt; &#91;\n\t\t\t<span class=\"hljs-string\">'cloud_name'<\/span> =&gt; getenv(<span class=\"hljs-string\">\"CLOUDINARY_NAME\"<\/span>),\n\t\t\t<span class=\"hljs-string\">'api_key'<\/span> =&gt; getenv(<span class=\"hljs-string\">\"CLOUDINARY_API_KEY\"<\/span>),\n\t\t\t<span class=\"hljs-string\">'api_secret'<\/span> =&gt; getenv(<span class=\"hljs-string\">\"CLOUDINARY_API_SECRET\"<\/span>)\n\t\t],\n\t\t<span class=\"hljs-string\">'url'<\/span> =&gt; &#91;\n\t\t\t<span class=\"hljs-string\">'secure'<\/span> =&gt; <span class=\"hljs-keyword\">true<\/span>\n\t\t]\n\t]);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>upload the screenshot base64 data to cloudinary for storage and capture the response to display on\nUI<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"> <span class=\"hljs-string\">\"index.php\"<\/span>\n\n  \t$response2 = (<span class=\"hljs-keyword\">new<\/span> UploadApi())-&gt;upload($snap&#91;<span class=\"hljs-string\">'data'<\/span>], &#91;\n\t\t<span class=\"hljs-string\">'resource_type'<\/span> =&gt; <span class=\"hljs-string\">'image'<\/span>,\n\t\t]\n\t);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Thats it! you are successfully able to capture images from a Web URL. enjoy the experience.<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":41,"featured_media":28061,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[381,134,229,371],"class_list":["post-28060","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-imagecdn","tag-guest-post","tag-php","tag-under-review"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Capture Any Website Screenshot Using PHP<\/title>\n<meta name=\"description\" content=\"Screenshots let you capture exactly what you&#039;re seeing on your screen to share with others or reference later. This article will demonstrate how much can be achieved using PHP and also manipulate Cloudinary services with such a service\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Capture Any Website Screenshot Using PHP\" \/>\n<meta property=\"og:description\" content=\"Screenshots let you capture exactly what you&#039;re seeing on your screen to share with others or reference later. This article will demonstrate how much can be achieved using PHP and also manipulate Cloudinary services with such a service\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-17T08:37:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"512\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Capture Any Website Screenshot Using PHP\",\"datePublished\":\"2022-06-17T08:37:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/\"},\"wordCount\":6,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA\",\"keywords\":[\"(Image)CDN\",\"Guest Post\",\"PHP\",\"Under Review\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2022\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/\",\"name\":\"Capture Any Website Screenshot Using PHP\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA\",\"datePublished\":\"2022-06-17T08:37:15+00:00\",\"description\":\"Screenshots let you capture exactly what you're seeing on your screen to share with others or reference later. This article will demonstrate how much can be achieved using PHP and also manipulate Cloudinary services with such a service\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA\",\"width\":512,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Capture Any Website Screenshot Using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Capture Any Website Screenshot Using PHP","description":"Screenshots let you capture exactly what you're seeing on your screen to share with others or reference later. This article will demonstrate how much can be achieved using PHP and also manipulate Cloudinary services with such a service","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/","og_locale":"en_US","og_type":"article","og_title":"Capture Any Website Screenshot Using PHP","og_description":"Screenshots let you capture exactly what you're seeing on your screen to share with others or reference later. This article will demonstrate how much can be achieved using PHP and also manipulate Cloudinary services with such a service","og_url":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/","og_site_name":"Cloudinary Blog","article_published_time":"2022-06-17T08:37:15+00:00","og_image":[{"width":512,"height":512,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA","type":"image\/png"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/"},"author":{"name":"","@id":""},"headline":"Capture Any Website Screenshot Using PHP","datePublished":"2022-06-17T08:37:15+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/"},"wordCount":6,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA","keywords":["(Image)CDN","Guest Post","PHP","Under Review"],"inLanguage":"en-US","copyrightYear":"2022","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/","url":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/","name":"Capture Any Website Screenshot Using PHP","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA","datePublished":"2022-06-17T08:37:15+00:00","description":"Screenshots let you capture exactly what you're seeing on your screen to share with others or reference later. This article will demonstrate how much can be achieved using PHP and also manipulate Cloudinary services with such a service","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA","width":512,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/capture-any-website-screenshot-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Capture Any Website Screenshot Using PHP"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":""}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681925525\/Web_Assets\/blog\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a\/a91430a144533bd398eab2e293392299d755af46-512x512-1_2806178a5a.png?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=28060"}],"version-history":[{"count":0,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28060\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/28061"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=28060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=28060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=28060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}