• 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

90.91
/src/Image/Operation/Retina.php
1
<?php
2

3
namespace Timber\Image\Operation;
4

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

9
/**
10
 * Contains the class for running image retina-izing operations
11
 */
12

13
/**
14
 * Increases image size by a given factor
15
 * Arguments:
16
 * - factor by which to multiply image dimensions
17
 * @property float $factor the factor (ex: 2, 1.5, 1.75) to multiply dimension by
18
 */
19
class Retina extends ImageOperation
20
{
21
    private $factor;
22

23
    /**
24
     * Construct our operation
25
     * @param float   $factor to multiply original dimensions by
26
     */
27
    public function __construct($factor)
28
    {
29
        $this->factor = $factor;
7✔
30
    }
31

32
    /**
33
     * Generates the final filename based on the source's name and extension
34
     *
35
     * @param   string    $src_filename     the basename of the file (ex: my-awesome-pic)
36
     * @param   string    $src_extension    the extension (ex: .jpg)
37
     * @return  string    the final filename to be used (ex: my-awesome-pic@2x.jpg)
38
     */
39
    public function filename($src_filename, $src_extension)
40
    {
41
        $newbase = $src_filename . '@' . $this->factor . 'x'; // add @2x, @3x, @1.5x, etc.
7✔
42
        $new_name = $newbase . '.' . $src_extension;
7✔
43
        return $new_name;
7✔
44
    }
45

46
    /**
47
     * Performs the actual image manipulation,
48
     * including saving the target file.
49
     *
50
     * @param  string $load_filename filepath (not URL) to source file
51
     *                               (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
52
     * @param  string $save_filename filepath (not URL) where result file should be saved
53
     *                               (ex: /src/var/www/wp-content/uploads/my-pic@2x.jpg)
54
     * @return bool                  true if everything went fine, false otherwise
55
     */
56
    public function run($load_filename, $save_filename)
57
    {
58
        // Attempt to check if SVG.
59
        if (ImageHelper::is_svg($load_filename)) {
7✔
60
            return false;
1✔
61
        }
62
        $image = \wp_get_image_editor($load_filename);
6✔
63
        if (!\is_wp_error($image)) {
6✔
64
            $current_size = $image->get_size();
5✔
65
            $src_w = $current_size['width'];
5✔
66
            $src_h = $current_size['height'];
5✔
67
            // Get ratios
68
            $w = $src_w * $this->factor;
5✔
69
            $h = $src_h * $this->factor;
5✔
70
            $image->crop(0, 0, $src_w, $src_h, $w, $h);
5✔
71
            $result = $image->save($save_filename);
5✔
72
            if (\is_wp_error($result)) {
5✔
73
                // @codeCoverageIgnoreStart
74
                Helper::error_log('Error resizing image');
75
                Helper::error_log($result);
76
                return false;
77
                // @codeCoverageIgnoreEnd
78
            }
79
            return true;
5✔
80
        } elseif (isset($image->error_data['error_loading_image'])) {
1✔
81
            Helper::error_log('Error loading ' . $image->error_data['error_loading_image']);
1✔
82
            return false;
1✔
83
        }
84
        Helper::error_log($image);
×
85
        return false;
×
86
    }
87
}
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