swoole_framework /libs/code/get_css_image.php

Language PHP Lines 60
MD5 Hash 3ddc6409ae2d7c0cd512d5d7b8dc26c0
Repository https://github.com/monkeycraps/swoole_framework.git View Raw File
 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
if($argc < 5)
{
    echo "php ".basename(__FILE__)," css文件目录 远程URL 目标文件夹 目标本地路径\n";exit;
}
$dir = $argv[1];
$remote = $argv[2];
$dest = $argv[3];
$csspath = $argv[4];

$files = scandir($dir);

foreach($files as $f)
{
    if(substr($f, -4, 4)=='.css')
    {
        parseCssFile($dir.'/'.$f);
    }
}

function parseCssFile($f)
{
    global $remote, $dest, $csspath;
    $patten = '#url\s*\(\s*(.*)\s*\)#';
    $content = file_get_contents($f);
    $match = array();
    $n = preg_match_all($patten, $content, $match);
    if($n > 0)
    {
        foreach($match[1] as $_m)
        {
            $m = trim($_m, "'\" \r\n");
            if($m[0]!='/') $m = '/'.$m;
            $_fd = $dest.$m;
            $_fs = $remote.$m;
            if(!is_dir(dirname($_fd)))
            {
                mkdir(dirname($_fd), 0777, true);
            }

            if(is_file($_fd))
            {
                echo "file exists.[$_fd]\n";
                continue;
            }
            str_replace($m, $csspath.$m, $content);
            if(copy($_fs, $_fd))
            {
                echo "download remote file[$_fs] to [$_fd].\n";
                //var_dump($m, $csspath.$m);
            }
            else
            {
                echo "download fail.file[$_fs] to [$_fd].\n";
            }
        }
    }
    file_put_contents($f, $content);
    echo "parse ok. [$f]\n";
}
Back to Top