Media Wysiwyg + CKeditor. Replace inline images with custom images programmatically I am trying to replace individual images in old node body with a default image using media + ckeditor programatically so as to save disk space as there are lot of images in server. 

I was successful in replacing image for an image field programmatically. However, I am not able to determine how do I replace those images embedded within a body along with body text while loading a node using node_load.

I intend to replace these images using hook_cron and run replacement operations on older nodes images.
The purpose of this question was to replace old node images with a default image so as to reduce disk space usage, as old nodes are not frequently visited by users.

To achieve this first you load the node using node_load function, and grab the body field:

$node = node_load($nodeID);
$body = isset($node-&gt;body[$node-&gt;language][0]) ?$node-&gt;body[$node-&gt;language][0] : array();

$text = isset($body['value']) ?  $body['value'] : $body['safe_value'];


After this you create DOMDocument object:

$doc = new DOMDocument(null, 'UTF-8');


Here, UTF-8 is for languages that are not Latin/Plain English.

$doc-&gt;loadHTML(mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8'));


Convert your text to HTML entities following UTF-8 format.

$tags = $doc-&gt;getElementsByTagName('img');


You grab all your image tag from body.

Next, Loop through those tags:

foreach ($tags as $tag) {
// Get default Image.
$file_temp = file_get_contents($urlToDefaultImage);
// Get Image information.
$info = pathinfo($urlToDefaultImage);
// Replace if it exists.
$file = file_save_data($file_temp, 'public://' . $info['basename'], FILE_EXISTS_REPLACE);
// Get old image fid.
$oldImage = $tag-&gt;getAttribute('data-fid');
// Remove the file info from db and server if not used by any.
$status = file_delete(file_load($oldImage)),
$tag-&gt;setAttribute('src', file_create_url($file-&gt;uri));
$tag-&gt;setAttribute('data-fid', $file-&gt;fid);
}


After looping through each img tags save the newly generated HTML.

$replacedBody = $doc-&gt;saveHTML();


Remove extra html, body and Doctype from the new body using str_replace:

$replacedBody = str_replace('&lt;html&gt;&lt;body&gt;', '', $replacedBody);
$replacedBody = str_replace('&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;', '', $replacedBody);
$replacedBody = str_replace('&lt;/body&gt;&lt;/html&gt;', '', $replacedBody);


Next update the newly formated body to node object:

$node-&gt;body[$node-&gt;language][0]['value'] = html_entity_decode($replacedBody);
$node-&gt;body[$node-&gt;language][0]['safe_value'] = html_entity_decode($replacedBody);


Save the node

node_save($node);


Bonus: Updating images in node image field.

Grab the field data.

$field = !empty($node-&gt;field_{machine_name}[$node-&gt;language]) ?  $node-&gt;field_{machine_name}[$node-&gt;language] : array();


Here, {machine_name} represents the name for image field you created using Content UI.

// Loop through old images.
foreach($field as $key =&gt; $row) {

    // Check if the image 
    if($row['type'] == 'image') {

        // Get default Image.
        $file_temp = file_get_contents($urlToDefaultImage);

        // Get Image information.
        $info = pathinfo($urlToDefaultImage);

        // Replace if it exists.
        $file = file_save_data($file_temp, 'public://' . $info['basename'], FILE_EXISTS_REPLACE);

        // Change the current image with default.
        $node-&gt;field_{machine_name}[$node-&gt;language][$key] = array(
        'fid' =&gt; $file-&gt;fid,
        'filename' =&gt; $file-&gt;filename,
        'filemime' =&gt; $file-&gt;filemime,
        'uid' =&gt; $file-&gt;uid,
        'uri' =&gt; $file-&gt;uri,
        'status' =&gt; $file-&gt;status,
        'display' =&gt; 1,
        );
    }
}


Performing these operation will remove default image from your db and server and will save disk space. While we need to use file_delete function when removing files from body, it is not needed when when perform this operation while we replace on an image/media field. file_delete function will safely delete the files from server if that file is not used by any other nodes in website, if second parameter is not set to TRUE. While file_manage_delete does the same thing, though it will not lookup for database and check for its existing usage, it will simply release your disk space from that file, result in 404 not found image on other reused nodes.