• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-library / 3799187254

pending completion
3799187254

push

github

webeweb
Update CHANGELOG

48790 of 48928 relevant lines covered (99.72%)

48.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/image/Helper/ImageHelper.php
1
<?php
2

3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Library\Image\Helper;
13

14
use Imagick;
15
use ImagickException;
16
use RuntimeException;
17
use WBW\Library\Image\Factory\ImageFactory;
18
use WBW\Library\Image\Model\Image;
19
use WBW\Library\Image\Model\ImageInterface;
20

21
/**
22
 * Image helper.
23
 *
24
 * @author webeweb <https://github.com/webeweb>
25
 * @package WBW\Library\Image\Helper
26
 */
27
class ImageHelper {
28

29
    /**
30
     * Encode an image into base 64.
31
     *
32
     * @param string|null $uri The URI.
33
     * @return string|null Returns the image encoded into base 64.
34
     */
35
    public static function base64Encode(?string $uri): ?string {
36

37
        if (null === $uri) {
14✔
38
            return null;
7✔
39
        }
40

41
        $data = file_get_contents($uri);
14✔
42

43
        $stream = fopen("php://memory", "w+b");
14✔
44
        fwrite($stream, $data);
14✔
45

46
        $mime = mime_content_type($stream);
14✔
47

48
        fclose($stream);
14✔
49

50
        return sprintf("data:%s;base64,%s", $mime, base64_encode($data));
14✔
51
    }
52

53
    /**
54
     * Resize.
55
     *
56
     * @param int $newWidth The new width.
57
     * @param int $newHeight The new height.
58
     * @param string $pathname The pathname.
59
     * @return bool Returns true in case of success, false otherwise.
60
     * @throws RuntimeException Throws a runtime exception if the re-sampled copy failed.
61
     */
62
    public static function resize(Image $image, int $newWidth, int $newHeight, string $pathname): bool {
63

64
        [$w, $h] = ImageFactory::newDimensions($image, $newWidth, $newHeight);
14✔
65

66
        $input  = ImageFactory::newInputStream($image);
14✔
67
        $output = ImageFactory::newOutputStream($image, $w, $h);
14✔
68
        if (null === $output) {
14✔
69
            throw new RuntimeException("Failed to create true color image");
×
70
        }
71

72
        $result = imagecopyresampled($output, $input, 0, 0, 0, 0, $w, $h, $image->getWidth(), $image->getHeight());
14✔
73
        if (false === $result) {
14✔
74
            throw new RuntimeException("Failed to copy re-sampled image");
×
75
        }
76

77
        return static::saveOutputStream($image, $output, $pathname);
14✔
78
    }
79

80
    /**
81
     * Save an output stream.
82
     *
83
     * @param Image $image the image.
84
     * @param resource $outputStream The output stream.
85
     * @param string $pathname The pathname.
86
     * @return bool Returns true in case of success, false otherwise.
87
     */
88
    protected static function saveOutputStream(Image $image, $outputStream, string $pathname): bool {
89

90
        switch ($image->init()->getMimeType()) {
21✔
91

92
            case ImageInterface::MIME_TYPE_JPEG:
3✔
93
                return imagejpeg($outputStream, $pathname);
21✔
94

95
            case ImageInterface::MIME_TYPE_PNG:
1✔
96
                return imagepng($outputStream, $pathname);
7✔
97
        }
98

99
        return false;
×
100
    }
101

102
    /**
103
     * SVG to PNG.
104
     *
105
     * @param string $filename The filename.
106
     * @param int|null $width The width.
107
     * @param int|null $height The height.
108
     * @return string|null Returns the SVG converted into PNG.
109
     * @throws ImagickException Throws an Imagick exception if an error occurs.
110
     */
111
    public static function svg2png(string $filename, int $width = null, int $height = null): ?string {
112

113
        $image = new Imagick();
7✔
114
        $image->setBackgroundColor("transparent");
7✔
115
        $image->readImage($filename);
7✔
116
        $image->setImageFormat("png");
7✔
117

118
        if (null !== $width && null !== $height) {
7✔
119
            $image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
×
120
        }
121

122
        $png = $image->getImageBlob();
7✔
123
        $image->clear();
7✔
124

125
        return $png;
7✔
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc