Completed
Branch feature/multi-screenshot (3b2450)
by Geza
12:02
created

ScreenshotTaker::getCombinedImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\Service;
4
5
use Behat\Mink\Mink;
6
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * This class is responsible for taking screenshot by using the Mink session
11
 *
12
 * @license http://opensource.org/licenses/MIT The MIT License
13
 */
14
class ScreenshotTaker
15
{
16
    /** @var Mink $mink */
17
    private $mink;
18
19
    /** @var OutputInterface $output */
20
    private $output;
21
22
    /**
23
     * @var array $screenshots
24
     */
25
    private $screenshots;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param Mink $mink
31
     * @param OutputInterface $output
32
     */
33
    public function __construct(Mink $mink, OutputInterface $output)
34
    {
35
        $this->mink = $mink;
36
        $this->output = $output;
37
    }
38
39
    /**
40
     * Save the screenshot as the given filename
41
     */
42
    public function takeScreenshot()
43
    {
44
        try {
45
            $this->screenshots[] = $this->mink->getSession()->getScreenshot();
46
        } catch (\Exception $e) {
47
            $this->output->writeln($e->getMessage());
48
        }        
49
    }
50
51
    public function getCombinedImage()
52
    {
53
        $im = new \Imagick();
54
55
        foreach ($this->screenshots as $screenshot) {
56
            $im->readImageBlob($screenshot);
57
        }
58
59
        /* Append the images into one */
60
        $im->resetIterator();
61
        $combined = $im->appendImages(true);
62
63
        /* Output the image */
64
        $combined->setImageFormat("png");
65
66
        return (string)$combined;
67
    }
68
69
    /**
70
     * Remove previous images
71
     */
72
    public function reset()
73
    {
74
        $this->screenshots = [];
75
    }
76
}
77