ImageUtil   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertText() 0 35 2
A createThumbnail() 0 31 2
A __construct() 0 3 1
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Util;
26
27
use SP\Core\Exceptions\CheckException;
28
use SP\Core\Exceptions\InvalidImageException;
29
use SP\Core\PhpExtensionChecker;
30
31
defined('APP_ROOT') || die();
32
33
/**
34
 * Class ImageUtil para la manipulación de imágenes
35
 *
36
 * @package SP
37
 */
38
final class ImageUtil
39
{
40
    /**
41
     * ImageUtil constructor.
42
     *
43
     * @param PhpExtensionChecker $checker
44
     *
45
     * @throws CheckException
46
     */
47
    public function __construct(PhpExtensionChecker $checker)
48
    {
49
        $checker->checkCurlAvailable(true);
50
    }
51
52
    /**
53
     * Crear miniatura de una imagen
54
     *
55
     * @param $image string La imagen a redimensionar
56
     *
57
     * @return bool|string
58
     * @throws InvalidImageException
59
     */
60
    public function createThumbnail($image)
61
    {
62
        $im = @imagecreatefromstring($image);
63
64
        if ($im === false) {
65
            throw new InvalidImageException(__u('Invalid image'));
66
        }
67
68
        $width = imagesx($im);
69
        $height = imagesy($im);
70
71
        // Calcular el tamaño de la miniatura
72
        $new_width = 48;
73
        $new_height = floor($height * ($new_width / $width));
74
75
        // Crear nueva imagen
76
        $imTmp = imagecreatetruecolor($new_width, $new_height);
0 ignored issues
show
Bug introduced by
$new_height of type double is incompatible with the type integer expected by parameter $height of imagecreatetruecolor(). ( Ignorable by Annotation )

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

76
        $imTmp = imagecreatetruecolor($new_width, /** @scrutinizer ignore-type */ $new_height);
Loading history...
77
78
        // Redimensionar la imagen
79
        imagecopyresized($imTmp, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
0 ignored issues
show
Bug introduced by
$new_height of type double is incompatible with the type integer expected by parameter $dst_h of imagecopyresized(). ( Ignorable by Annotation )

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

79
        imagecopyresized($imTmp, $im, 0, 0, 0, 0, $new_width, /** @scrutinizer ignore-type */ $new_height, $width, $height);
Loading history...
Bug introduced by
It seems like $imTmp can also be of type false; however, parameter $dst_image of imagecopyresized() 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

79
        imagecopyresized(/** @scrutinizer ignore-type */ $imTmp, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Loading history...
80
81
        // Devolver la imagen
82
        ob_start();
83
        imagepng($imTmp);
0 ignored issues
show
Bug introduced by
It seems like $imTmp 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

83
        imagepng(/** @scrutinizer ignore-type */ $imTmp);
Loading history...
84
        $thumbnail = ob_get_contents();
85
        ob_end_clean();
86
87
        imagedestroy($imTmp);
0 ignored issues
show
Bug introduced by
It seems like $imTmp 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

87
        imagedestroy(/** @scrutinizer ignore-type */ $imTmp);
Loading history...
88
        imagedestroy($im);
89
90
        return base64_encode($thumbnail);
91
    }
92
93
    /**
94
     * Convertir un texto a imagen
95
     *
96
     * @param $text string El texto a convertir
97
     *
98
     * @return bool|string
99
     */
100
    public function convertText($text)
101
    {
102
        $width = strlen($text) * 10;
103
104
        $im = @imagecreatetruecolor($width, 30);
105
106
        if ($im === false) {
107
            return false;
108
        }
109
110
        // Colores de la imagen
111
        $bgColor = imagecolorallocate($im, 245, 245, 245);
112
//        $shadowColor = imagecolorallocate($im, 128, 128, 128);
113
        $fgColor = imagecolorallocate($im, 128, 128, 128);
114
115
        imagefilledrectangle($im, 0, 0, $width, 30, $bgColor);
116
117
        // Ruta de la fuente
118
        $font = PUBLIC_PATH . '/vendor/fonts/NotoSans-Regular-webfont.ttf';
119
120
        // Sombra
121
//        imagettftext($im, 14, 0, 13, 23, $shadowColor, $font, $text);
122
123
        // Crear el texto
124
        imagettftext($im, 10, 0, 10, 20, $fgColor, $font, $text);
125
126
        // Devolver la imagen
127
        ob_start();
128
        imagepng($im);
129
        $image = ob_get_contents();
130
        ob_end_clean();
131
132
        imagedestroy($im);
133
134
        return base64_encode($image);
135
    }
136
}