-2

Hi there I works with simple php parser to save imgs form external server...So I want to get an normal src of an picture but it seem below img elements has an unusuall src ...

Is there anyway to turn this code to normal src or at least first save it in my server?

Note: text in src is too long...more than 170000 chars...I removed most of them to insert here to show you...

<img style="display: block; margin-left: auto; margin-right: auto;" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAZAAA/+4ADkFkb2JlAGTAAAAAAf/bAIQAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQICAgICAgICAgICAwMDAwMDAwMDAwEBAQEBAQECAQECAgIBAgIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMD/8AAEQgBZQJ2AwERAAIRAQMRAf/EANcAAAAFBQEAAAAAAAAAAAAAAAIDBAgJAQUGBwoAAQAABgMBAQAAAAAAAAAAAAACAwQFBgcAAQgJChAAAQMCBAQEAwUFBQQFCQQLAQIDBBEFACESBjFBEwdRYSIIcRQJgZGhMhWxwdFCI/DhUjMW8WJyJJKistIXgkNTdLQldTcYwo1J+AH8U2v8uqLpH6v5jprx0gEfAaySD8RiOkWfmihfp9w/igf0u9f//Z" alt="">
1
  • Read SRC -> Decode it to base 64 -> Now you have image -> save this file to folder Apr 30, 2015 at 6:18

3 Answers 3

1

Copy all of the src text after base64, and use php's base64_decode function to decode it. Once there you can write it to a jpg file if you want.

1
<?php
  echo base64_encode(file_get_contents("../images/folder16.gif"))
?>

You can use PHP function to get the image coded. See this working example :

<?php 
  $img = base64_encode(file_get_contents("https://www.google.co.in/images/srpr/logo11w.png"));
echo "<img src='data:image/gif;base64,".$img."' />";

?>
-1

Follow the following steps

<?php

// [1] Prepare your page HTML content
$html = '<img src="data:image/png;base64,image_[1]_valid_base_64_encoded_string">';
$html .= '<img src="data:image/gif;base64,image_[2]_valid_base_64_encoded_string">';
$html .= '<img src="data:image/jpeg;base64,image_[3]_valid_base_64_encoded_string">';
$html .= '<img src="data:image/jpg;base64,image_[4]_valid_base_64_encoded_string">';

// [2] Get all src attributes
$xpath = new DOMXPath(@DOMDocument::loadHTML($html));
$src = $xpath->evaluate("//img/@src");

// [3] Loop src attributes and push image info to $images arary
$images = array();
foreach ($src as $attr)
{
    $data = explode('/', $attr->value);
    $data = str_replace(';', ',', $data[1]);

    list($extension, $type, $encoded_string) = explode(',', $data);

    // push to images array
    $images[] = array(
        'extension' => strtolower($extension),
        'image_base64' => $encoded_string,
    );
}

// results
echo '<pre>';
print_r($images);
echo '</pre>';

// [4] Move images to directory
// @file_put_contents("path/to/dir/image_name.$extension",  base64_decode($encoded_string));

// print_r($images) output

Array
(
    [0] => Array
        (
            [extension] => png
            [image_base64] => image_[1]_valid_base_64_encoded_string
        )

    [1] => Array
        (
            [extension] => gif
            [image_base64] => image_[2]_valid_base_64_encoded_string
        )

    [2] => Array
        (
            [extension] => jpeg
            [image_base64] => image_[3]_valid_base_64_encoded_string
        )

    [3] => Array
        (
            [extension] => jpg
            [image_base64] => image_[4]_valid_base_64_encoded_string
        )

)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.