LogoGenerator::createLogo()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 6
nop 2
dl 0
loc 45
rs 8.5866
c 0
b 0
f 0
1
<?php namespace XoopsModules\Tdmcreate;
2
3
use XoopsModules\Tdmcreate;
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
 * tdmcreate module.
16
 *
17
 * @copyright       XOOPS Project (https://xoops.org)
18
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
19
 *
20
 * @since           2.5.0
21
 *
22
 * @author          Xoops Team Developement Modules - https://xoops.org
23
 *
24
 * @version         $Id: LogoGenerator.php 12258 2014-01-02 09:33:29Z timgno $
25
 */
26
include_once dirname(dirname(dirname(__DIR__))) . '/mainfile.php';
27
//include_once XOOPS_ROOT_PATH . '/modules/TDMCreate/include/common.php';
28
29
if (function_exists($_GET['f'])) { // get function name and parameter  $_GET['f']($_GET["p"]);
30
    include_once __DIR__ . '/LogoGenerator.php';
31
    $ret = LogoGenerator::createLogo($_GET['iconName'], $_GET['caption']);
32
    phpFunction($ret);
0 ignored issues
show
Bug introduced by
It seems like $ret can also be of type false; however, parameter $val of XoopsModules\Tdmcreate\phpFunction() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

32
    phpFunction(/** @scrutinizer ignore-type */ $ret);
Loading history...
33
} else {
34
    echo 'Method Not Exist';
35
}
36
37
/**
38
 * @param string $val
39
 */
40
function phpFunction($val = '')
41
{      // create php function here
42
    echo $val;
43
}
44
45
/**
46
 * Class LogoGenerator.
47
 */
48
class LogoGenerator
49
{
50
    /**
51
     * @param $logoIcon
52
     * @param $moduleName
53
     *
54
     * @return bool|string
55
     */
56
    public static function createLogo($logoIcon, $moduleName)
57
    {
58
        if (!extension_loaded('gd')) {
59
            return false;
60
        }
61
        $requiredFunctions = ['imagecreatefrompng', 'imagefttext', 'imagecopy', 'imagepng', 'imagedestroy', 'imagecolorallocate'];
62
        foreach ($requiredFunctions as $func) {
63
            if (!function_exists($func)) {
64
                return false;
65
            }
66
        }
67
68
        $dirname = 'tdmcreate';
69
        $iconFileName = XOOPS_ROOT_PATH . '/Frameworks/moduleclasses/icons/32/' . basename($logoIcon);
70
71
        //$dirFonts = TDMC_PATH . "/assets/fonts";
72
        //$dirLogos = TDMC_PATH . "/assets/images/logos";
73
        $dirFonts = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/assets/fonts';
74
        $dirLogos = XOOPS_ROOT_PATH . '/modules/' . $dirname . '/assets/images/logos';
75
76
        if (!file_exists($imageBase = $dirLogos . '/empty.png') ||
77
            !file_exists($font = $dirFonts . '/VeraBd.ttf') ||
78
            !file_exists($iconFile = $iconFileName)) {
79
            return false;
80
        }
81
82
        $imageModule = imagecreatefrompng($imageBase);
83
        $imageIcon = imagecreatefrompng($iconFile);
84
85
        // Write text
86
        $textColor = imagecolorallocate($imageModule, 0, 0, 0);
0 ignored issues
show
Bug introduced by
It seems like $imageModule can also be of type false; however, parameter $image of imagecolorallocate() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

86
        $textColor = imagecolorallocate(/** @scrutinizer ignore-type */ $imageModule, 0, 0, 0);
Loading history...
87
        $spaceToBorder = (92 - mb_strlen($moduleName) * 7.5) / 2;
88
        imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, $moduleName, []);
0 ignored issues
show
Bug introduced by
It seems like $imageModule can also be of type false; however, parameter $image of imagefttext() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

88
        imagefttext(/** @scrutinizer ignore-type */ $imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, $moduleName, []);
Loading history...
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

88
        imagefttext($imageModule, 8.5, 0, /** @scrutinizer ignore-type */ $spaceToBorder, 45, $textColor, $font, $moduleName, []);
Loading history...
89
90
        imagecopy($imageModule, $imageIcon, 29, 2, 0, 0, 32, 32);
0 ignored issues
show
Bug introduced by
It seems like $imageModule can also be of type false; however, parameter $dst_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

90
        imagecopy(/** @scrutinizer ignore-type */ $imageModule, $imageIcon, 29, 2, 0, 0, 32, 32);
Loading history...
Bug introduced by
It seems like $imageIcon can also be of type false; however, parameter $src_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

90
        imagecopy($imageModule, /** @scrutinizer ignore-type */ $imageIcon, 29, 2, 0, 0, 32, 32);
Loading history...
91
92
        //$targetImage = TDMC_UPLOAD_IMGMOD_URL . "/" . $moduleName . "_logo.png";
93
        $targetImage = '/uploads/' . $dirname . '/images/modules/' . $moduleName . '_logo.png';
94
95
        imagepng($imageModule, XOOPS_ROOT_PATH . $targetImage);
0 ignored issues
show
Bug introduced by
It seems like $imageModule can also be of type false; however, parameter $image of imagepng() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

95
        imagepng(/** @scrutinizer ignore-type */ $imageModule, XOOPS_ROOT_PATH . $targetImage);
Loading history...
96
97
        imagedestroy($imageModule);
0 ignored issues
show
Bug introduced by
It seems like $imageModule can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        imagedestroy(/** @scrutinizer ignore-type */ $imageModule);
Loading history...
98
        imagedestroy($imageIcon);
99
100
        return XOOPS_URL . $targetImage;
101
    }
102
}
103