Logo   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
B createLogo() 0 45 7
1
<?php
2
3
namespace XoopsModules\Modulebuilder;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * modules class.
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
20
 *
21
 * @since           2.5.7
22
 *
23
 * @author          Txmod Xoops <[email protected]> - <https://xoops.org/>
24
 *
25
 */
26
27
/**
28
 * Class Logo.
29
 */
30
class Logo
31
{
32
    /*
33
    *  @static function getInstance
34
    *  @param null
35
    */
36
37
    /**
38
     * @return Logo
39
     */
40
    public static function getInstance()
41
    {
42
        static $instance = false;
43
        if (!$instance) {
44
            $instance = new self();
45
        }
46
47
        return $instance;
48
    }
49
50
    /*
51
    *  @private static function createLogo
52
    *  @param mixed $logoIcon
53
    *  @param string $moduleDirname
54
    */
55
    /**
56
     * @param $logoIcon
57
     * @param $moduleDirname
58
     *
59
     * @return bool|string
60
     */
61
    /*private static function createLogo($logoIcon, $moduleDirname)
62
    {
63
        if (!\extension_loaded('gd')) {
64
            return false;
65
        } else {
66
            $requiredFunctions = ['imagecreatefrompng', 'imagefttext', 'imagecopy', 'imagepng', 'imagedestroy', 'imagecolorallocate'];
67
            foreach ($requiredFunctions as $func) {
68
                if (!\function_exists($func)) {
69
                    return false;
70
                }
71
            }
72
        }
73
        if (!\file_exists($imageBase = TDMC_IMAGES_LOGOS_PATH.'/empty.png') ||
74
            !\file_exists($font = TDMC_FONTS_PATH.'/VeraBd.ttf') ||
75
            !\file_exists($iconFile = \XOOPS_ICONS32_PATH.'/'.\basename($logoIcon))
76
        ) {
77
            return false;
78
        }
79
        $imageModule = \imagecreatefrompng($imageBase);
80
        $imageIcon = \imagecreatefrompng($iconFile);
81
        // Write text
82
        $textColor = imagecolorallocate($imageModule, 0, 0, 0);
83
        $spaceBorder = (92 - \strlen($moduleDirname) * 7.5) / 2;
84
        imagefttext($imageModule, 8.5, 0, $spaceBorder, 45, $textColor, $font, \ucfirst($moduleDirname), []);
85
        imagecopy($imageModule, $imageIcon, 29, 2, 0, 0, 32, 32);
86
        $logoImg = '/'.$moduleDirname.'_logo.png';
87
        \imagepng($imageModule, TDMC_UPLOAD_IMGMOD_PATH.$logoImg);
88
        \imagedestroy($imageModule);
89
        \imagedestroy($imageIcon);
90
91
        return TDMC_UPLOAD_IMGMOD_URL.$logoImg;
92
    }*/
93
94
    /**
95
     * @param $logoIcon
96
     * @param $moduleDirname
97
     *
98
     * @return bool|string
99
     */
100
    public static function createLogo($logoIcon, $moduleDirname)
101
    {
102
        if (!\extension_loaded('gd')) {
103
            return false;
104
        }
105
        $requiredFunctions = ['imagecreatefrompng', 'imagefttext', 'imagecopy', 'imagepng', 'imagedestroy', 'imagecolorallocate'];
106
        foreach ($requiredFunctions as $func) {
107
            if (!\function_exists($func)) {
108
                return false;
109
            }
110
        }
111
112
        $dirname      = 'modulebuilder';
113
        $iconFileName = \XOOPS_ROOT_PATH . '/Frameworks/moduleclasses/icons/32/' . \basename($logoIcon);
114
115
        //$dirFonts = TDMC_PATH . "/assets/fonts";
116
        //$dirLogos = TDMC_PATH . "/assets/images/logos";
117
        $dirFonts = \XOOPS_ROOT_PATH . '/modules/' . $dirname . '/assets/fonts';
118
        $dirLogos = \XOOPS_ROOT_PATH . '/modules/' . $dirname . '/assets/images/logos';
119
120
        if (!\file_exists($imageBase = $dirLogos . '/empty.png')
121
            || !\file_exists($font = $dirFonts . '/VeraBd.ttf')
122
            || !\file_exists($iconFile = $iconFileName)) {
123
            return false;
124
        }
125
126
        $imageModule = \imagecreatefrompng($imageBase);
127
        $imageIcon   = \imagecreatefrompng($iconFile);
128
129
        // Write text
130
        $textColor     = imagecolorallocate($imageModule, 0, 0, 0);
131
        $spaceToBorder = (92 - mb_strlen($moduleDirname) * 7.5) / 2;
132
        imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, \ucfirst($moduleDirname), []);
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

132
        imagefttext($imageModule, 8.5, 0, /** @scrutinizer ignore-type */ $spaceToBorder, 45, $textColor, $font, \ucfirst($moduleDirname), []);
Loading history...
133
134
        imagecopy($imageModule, $imageIcon, 29, 2, 0, 0, 32, 32);
135
136
        //$targetImage = TDMC_UPLOAD_IMGMOD_URL . "/" . $moduleDirname . "_logo.png";
137
        $targetImage = '/uploads/' . $dirname . '/images/modules/' . $moduleDirname . '_logo.png';
138
139
        \imagepng($imageModule, \XOOPS_ROOT_PATH . $targetImage);
140
141
        \imagedestroy($imageModule);
142
        \imagedestroy($imageIcon);
143
144
        return \XOOPS_URL . $targetImage;
145
    }
146
}
147