Passed
Branch master (acf800)
by Michael
02:26
created

Cloner::cloneFileFolder()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 7
nop 1
dl 0
loc 32
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Publisher;
4
5
use XoopsModules\Publisher;
6
/**
7
 * Class Cloner
8
 */
9
class Cloner {
10
    // recursive cloning script
11
    /**
12
     * @param $path
13
     */
14
    public static function cloneFileFolder($path)
15
    {
16
        global $patKeys;
17
        global $patValues;
18
19
        $newPath = str_replace($patKeys[0], $patValues[0], $path);
20
21
        if (is_dir($path)) {
22
            // create new dir
23
            if (!mkdir($newPath) && !is_dir($newPath)) {
24
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $newPath));
25
            }
26
27
            // check all files in dir, and process it
28
            if ($handle = opendir($path)) {
29
                while (false !== ($file = readdir($handle))) {
30
                    if (0 !== mb_strpos($file, '.')) {
31
                        self::cloneFileFolder("{$path}/{$file}");
32
                    }
33
                }
34
                closedir($handle);
35
            }
36
        } else {
37
            $noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
38
            if (in_array(mb_strtolower(pathinfo($path, PATHINFO_EXTENSION)), $noChangeExtensions, true)) {
39
                // image
40
                copy($path, $newPath);
41
            } else {
42
                // file, read it
43
                $content = file_get_contents($path);
44
                $content = str_replace($patKeys, $patValues, $content);
45
                file_put_contents($newPath, $content);
46
            }
47
        }
48
    }
49
50
    /**
51
     * @param $dirname
52
     *
53
     * @return bool
54
     */
55
    public static function createLogo($dirname)
56
    {
57
        if (!extension_loaded('gd')) {
58
            return false;
59
        }
60
        $requiredFunctions = [
61
            'imagecreatefrompng',
62
            'imagecolorallocate',
63
            'imagefilledrectangle',
64
            'imagepng',
65
            'imagedestroy',
66
            'imagefttext',
67
            'imagealphablending',
68
            'imagesavealpha',
69
        ];
70
        foreach ($requiredFunctions as $func) {
71
            if (!function_exists($func)) {
72
                return false;
73
            }
74
        }
75
        //            unset($func);
76
77
        if (!file_exists($imageBase = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'))
78
            || !file_exists($font = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/VeraBd.ttf'))) {
79
            return false;
80
        }
81
82
        $imageModule = imagecreatefrompng($imageBase);
83
        // save existing alpha channel
84
        imagealphablending($imageModule, false);
85
        imagesavealpha($imageModule, true);
86
87
        //Erase old text
88
        $greyColor = imagecolorallocate($imageModule, 237, 237, 237);
89
        imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
90
91
        // Write text
92
        $textColor     = imagecolorallocate($imageModule, 0, 0, 0);
93
        $spaceToBorder = (80 - mb_strlen($dirname) * 6.5) / 2;
94
        imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, ucfirst($dirname), []);
0 ignored issues
show
Bug introduced by
$spaceToBorder of type double is incompatible with the type integer expected by parameter $x of imagefttext(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        imagefttext($imageModule, 8.5, 0, /** @scrutinizer ignore-type */ $spaceToBorder, 45, $textColor, $font, ucfirst($dirname), []);
Loading history...
95
96
        // Set transparency color
97
        //$white = imagecolorallocatealpha($imageModule, 255, 255, 255, 127);
98
        //imagefill($imageModule, 0, 0, $white);
99
        //imagecolortransparent($imageModule, $white);
100
101
        imagepng($imageModule, $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'));
102
        imagedestroy($imageModule);
103
104
        return true;
105
    }
106
}
107