Checker::isBase64()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
/**
4
 * This file is part of the Base64 Handler library.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * PHP Version 7
10
 *
11
 * LICENSE: This source file is subject to the MIT license that is available
12
 * through the world-wide-web at the following URI:
13
 * http://opensource.org/licenses/mit-license.php
14
 *
15
 * @category Src
16
 * @package  Normeno\Base64Hanlder
17
 * @author   Nicolas Ormeno <[email protected]>
18
 * @license  http://opensource.org/licenses/mit-license.php MIT License
19
 * @link     https://github.com/normeno/base64_handler
20
 */
21
namespace Normeno\Base64Handler;
22
23
/**
24
 * Checker Class
25
 *
26
 * @category src
27
 * @package  Normeno\Base64Handler
28
 * @author   Nicolas Ormeno <[email protected]>
29
 * @license  http://opensource.org/licenses/mit-license.php MIT License
30
 * @link     https://github.com/normeno/base64_handler
31
 */
32
class Checker
33
{
34
    /**
35
     * Check if string is a base64 string
36
     *
37
     * @param string $str base64 string
38
     *
39
     * @return bool
40
     */
41
    public static function isBase64($str) {
42
        try {
43
            return ( base64_encode(base64_decode($str, true)) === $str) ? true : false;
44
        } catch (\Exception $e) {
45
            return false;
46
        }
47
    }
48
49
    /**
50
     * Check if base64 string is an image
51
     *
52
     * @see https://stackoverflow.com/a/12658754/2901396
53
     * @param string $str base64 string
54
     *
55
     * @return bool
56
     */
57
    public static function isBase64Image($str)
58
    {
59
        $str = Utils::clearString($str);
60
        $img = @imagecreatefromstring(base64_decode($str));
61
62
        if (!$img) {
63
            return false;
64
        }
65
66
        imagepng($img, 'tmp.png');
67
        $info = getimagesize('tmp.png');
68
69
        unlink('tmp.png');
70
71
        if ($info[0] > 0 && $info[1] > 0 && $info['mime']) {
72
            return true;
73
        }
74
75
        return false;
76
    }
77
}