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

timber / timber / 5690593717

pending completion
5690593717

Pull #1617

github

web-flow
Merge f587ceffa into b563d274e
Pull Request #1617: 2.x

4433 of 4433 new or added lines in 57 files covered. (100.0%)

3931 of 4433 relevant lines covered (88.68%)

58.28 hits per line

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

95.83
/src/Image/Operation/ToJpg.php
1
<?php
2

3
namespace Timber\Image\Operation;
4

5
use Timber\Image\Operation as ImageOperation;
6
use Timber\ImageHelper;
7

8
/**
9
 * Implements converting a PNG file to JPG.
10
 * Argument:
11
 * - color to fill transparent zones
12
 */
13
class ToJpg extends ImageOperation
14
{
15
    private $color;
16

17
    /**
18
     * @param string $color hex string of color to use for transparent zones
19
     */
20
    public function __construct($color)
21
    {
22
        $this->color = $color;
9✔
23
    }
24

25
    /**
26
     * @param   string    $src_filename     the basename of the file (ex: my-awesome-pic)
27
     * @param   string    $src_extension    ignored
28
     * @return  string    the final filename to be used (ex: my-awesome-pic.jpg)
29
     */
30
    public function filename($src_filename, $src_extension = 'jpg')
31
    {
32
        $new_name = $src_filename . '.jpg';
9✔
33
        return $new_name;
9✔
34
    }
35

36
    /**
37
     * Performs the actual image manipulation,
38
     * including saving the target file.
39
     *
40
     * @param  string $load_filename filepath (not URL) to source file (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
41
     * @param  string $save_filename filepath (not URL) where result file should be saved
42
     *                               (ex: /src/var/www/wp-content/uploads/my-pic.png)
43
     * @return bool                  true if everything went fine, false otherwise
44
     */
45
    public function run($load_filename, $save_filename)
46
    {
47
        if (!\file_exists($load_filename)) {
7✔
48
            return false;
×
49
        }
50

51
        // Attempt to check if SVG.
52
        if (ImageHelper::is_svg($load_filename)) {
7✔
53
            return false;
1✔
54
        }
55

56
        $ext = \wp_check_filetype($load_filename);
6✔
57
        if (isset($ext['ext'])) {
6✔
58
            $ext = $ext['ext'];
6✔
59
        }
60
        $ext = \strtolower($ext);
6✔
61
        $ext = \str_replace('jpg', 'jpeg', $ext);
6✔
62

63
        $imagecreate_function = 'imagecreatefrom' . $ext;
6✔
64
        if (!\function_exists($imagecreate_function)) {
6✔
65
            return false;
1✔
66
        }
67

68
        $input = $imagecreate_function($load_filename);
5✔
69

70
        list($width, $height) = \getimagesize($load_filename);
5✔
71
        $output = \imagecreatetruecolor($width, $height);
5✔
72
        $c = self::hexrgb($this->color);
5✔
73
        $color = \imagecolorallocate($output, $c['red'], $c['green'], $c['blue']);
5✔
74
        \imagefilledrectangle($output, 0, 0, $width, $height, $color);
5✔
75
        \imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
5✔
76
        \imagejpeg($output, $save_filename);
5✔
77
        return true;
5✔
78
    }
79
}
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