Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:58:31+00:00 2026-06-13T09:58:31+00:00

I try to replace all images in a html document with inline image (data:image).

  • 0

I try to replace all images in a html document with inline image (data:image).
I’ve a sample code which does not work:

function data_uri($filename) {
$mime = mime_content_type($filename);
$data = base64_encode(file_get_contents($filename));
return "data:$mime;base64,$data";
}

function img_handler($matches) { 
$image_element = $matches[1];    
$pattern = '/(src=["\'])([^"\']+)(["\'])/'; 
$image_element = preg_replace($pattern, create_function( 
$matches, 
$matches[1] . data_uri($matches[2]) . $matches[3]), 
$image_element);     
return $image_element;
}

$content = (many) different img tags
$search = '(<img\s+[^>]+>)';
$content = preg_replace_callback($search, 'img_handler', $content);

Could somebody check this code? Thanks!

UPDATE:
(…) Warning file_get_contents() [function.file-get-contents]: Filename cannot be empty (…)

That means the src url is not in the handler 🙁

UPDATE 2

<?php
function data_uri($filename) {
    $mime = mime_content_type($filename);
    $data = base64_encode(file_get_contents($filename));

    return "data:$mime;base64,$data";
}

function img_handler($matches) { 
$image_element = $matches[0];
$pattern = '/(src=["\'])([^"\']+)(["\'])/'; 
$image_element = preg_replace_callback($pattern, create_function( 
$matchess,
$matchess[1] . data_uri($matchess[2]) . $matchess[3]), 
$image_element); 
return $image_element;
}

$content = '<a href="http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Googlelogoi.png/180px-Googlelogoi.png"><img class="alignnone" src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Googlelogoi.png/180px-Googlelogoi.png" alt="google" width="580" height="326" title="google" /></a>';
$search = '(<img\s+[^>]+>)';
$content = preg_replace_callback($search, 'img_handler', $content);

echo $content;
?>

I’ve upload this test file -> http://goo.gl/vWl9B

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:58:32+00:00Added an answer on June 13, 2026 at 9:58 am

    Your regex is alright. You are using create_function() wrong. And subsequently the inner preg_replace_callback() doesn’t work. The call to data_uri() happens before any regex-replacement takes place, hencewhy the undefined filename error.

    Use a proper callback function:

    $image_element = preg_replace_callback($pattern, "data_uri_callback", $image_element);
    

    Then move your code into there:

    function data_uri_callback($matchess) {
        return  $matchess[1] . data_uri($matchess[2]) . $matchess[3];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I got this function from someone, what it does is replace my image
I want to replace color of an image. For example turn all blue colors
I try to replace a sub-str by the content of a valiable where its
I try to replace few characters in some string 14/04/2010 17:12:11 and get, for
I am using this tutorial to try to replace the default TitleBar with a
I am trying to replace accented characters with regular characters. When I try to
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
try: html = urlopen('http://glbse.com/api/asset/' + asset.name) except: print 'error while updating the price of
I have a rich text field (called description) which containing texts and images I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.