aboutsummaryrefslogtreecommitdiff
path: root/replace-photos.php
blob: 97159329f4bde85c57bf13491d5a35938678646a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

require_once __DIR__.'/common.php';

$file = $argv[1] ?? '';
if (!$file)
    fatalError("no file provided");

$str = file_get_contents($file);
$str = iconv('windows-1251', 'utf-8//IGNORE', $str);

try {
    $doc = onEachMessageOrAttachment($str,
        null,
        function (simplehtmldom\HtmlDocument $doc, string $href, simplehtmldom\HtmlNode $link_node) {
            $local_href = '../../'.preg_replace('#^https?://#', '', $href);

            /** @var simplehtmldom\HtmlNode $parent */
            $parent = $link_node->parent();
            $link_node->remove();

            $img = $doc->createElement('img');
            $img->setAttribute('src', $local_href);
            $img->setAttribute('alt', $href);

            $parent->appendChild($doc->createElement('br'));
            $parent->appendChild($img);
        });
} catch (Exception $e) {
    fatalError($e->getMessage());
}

file_put_contents($file, iconv('utf-8', 'windows-1251//IGNORE', $doc->outertext));