PHP - Get raw image data from image in a HTML document


How do i properly grab an image that is displayed in a HTML document and feed it to PHP to be read as image binary. I do not have direct access to the image file. The image i am trying to grab is fed to the client with HTML via PHP and printed in HTML format and using an<img> tag to display the image. The src is just a link to the same page i am currently on. The link is a GET request.

The link looks like this:


This does not return the actual image with image MIME types. But rather a HTML displaying the image.

I do not have access to the source code in GETIMAGE.php file. This is encrypted as i am using a portal solution that is licensed.

This is the source that is returned from the GETIMAGE.php script:

  <meta name="viewport" content="width=device-width">
  <title>GETIMAGE.php (80×112)</title>
  <style type="text/css"></style>
<body >
  <img src=";path=Path/To/Image.png">




Without seeing your script, it is hard to figure out what you are looking for. Let's assume the page generates output like this:

<img src="" />

Using this excellent DOM Parsing Library, we can do something like this:

$html = file_get_html('GETIMAGE.php?type=small&path=/path/to/image.png');
$pictures = array();
foreach($html->find('img') as $element) 
   $pictures[] = $element->src;

foreach ($pictures as $picture) {
   $data = file_get_contents($picture);
   ## Do something with the data.

Then you will have an array of all pictures in$pictures.

Good luck.




You can use file_get_contents() method to get the data.

Here you can use




Don't know if you ever found an answer, but I finally did. The data that was being received by file_get_contents - or any CURL method - was actually returning data in a gzip format. When I saved the output to a file and extracted it as a gzip archive, the image was there.

People are also looking for solutions to the problem: php - preg_replace to change drupal style links to standard a tags


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.