-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCloner.php
109 lines (95 loc) · 3.41 KB
/
Cloner.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace XoopsModules\Publisher;
/**
* Class Cloner
*/
class Cloner
{
// recursive cloning script
/**
* @param $path
*/
public static function cloneFileFolder($path)
{
global $patKeys;
global $patValues;
$newPath = \str_replace($patKeys[0], $patValues[0], $path);
if (\is_dir($path)) {
// create new dir
if (!\mkdir($newPath) && !\is_dir($newPath)) {
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $newPath));
}
// check all files in dir, and process it
$handle = \opendir($path);
if ($handle) {
while (false !== ($file = \readdir($handle))) {
if (0 !== \mb_strpos($file, '.')) {
self::cloneFileFolder("{$path}/{$file}");
}
}
\closedir($handle);
}
} else {
$noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
if (\in_array(\mb_strtolower(\pathinfo($path, \PATHINFO_EXTENSION)), $noChangeExtensions, true)) {
// image
\copy($path, $newPath);
} else {
// file, read it
$content = file_get_contents($path);
$content = \str_replace($patKeys, $patValues, $content);
file_put_contents($newPath, $content);
}
}
}
/**
* @param $dirname
*
* @return bool
*/
public static function createLogo($dirname)
{
if (!\extension_loaded('gd')) {
return false;
}
$requiredFunctions = [
'imagecreatefrompng',
'imagecolorallocate',
'imagefilledrectangle',
'imagepng',
'imagedestroy',
'imagefttext',
'imagealphablending',
'imagesavealpha',
];
foreach ($requiredFunctions as $func) {
if (!\function_exists($func)) {
return false;
}
}
// unset($func);
if (!\file_exists($imageBase = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'))
|| !\file_exists($font = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/VeraBd.ttf'))) {
return false;
}
$imageModule = \imagecreatefrompng($imageBase);
// save existing alpha channel
imagealphablending($imageModule, false);
imagesavealpha($imageModule, true);
//Erase old text
$greyColor = \imagecolorallocate($imageModule, 237, 237, 237);
\imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
// Write text
$textColor = \imagecolorallocate($imageModule, 0, 0, 0);
$spaceToBorder = (int)((80 - \mb_strlen($dirname) * 6.5) / 2);
\imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, \ucfirst($dirname), []);
// Set transparency color
//$white = imagecolorallocatealpha($imageModule, 255, 255, 255, 127);
//imagefill($imageModule, 0, 0, $white);
//imagecolortransparent($imageModule, $white);
\imagepng($imageModule, $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'));
\imagedestroy($imageModule);
return true;
}
}