xref: /imagick/tests/functions.inc (revision 9b465f48)
1<?php
2
3/**
4 *
5 * Gets the installed version of ImageMagick and compares the
6 * appropriate version to the installed version. *
7 *
8 * @param $testIm6Version
9 * @param $im7Version
10 * @return int
11 */
12function version_compare_imagemagick($testIm6Version, $im7Version)
13{
14    $versionInfo = \Imagick::getVersion();
15
16    if (array_key_exists("versionString", $versionInfo) == false) {
17        die("skip unable to determine ImageMagick version.");
18    }
19
20    $versionInstalledStringComplete = $versionInfo["versionString"];
21
22    $firstSpace = strpos($versionInstalledStringComplete, ' ');
23    if ($firstSpace === false) {
24        die("Failed to understand version string [$versionInstalledStringComplete] - finding first space");
25    }
26
27    $secondSpace = strpos($versionInstalledStringComplete, ' ', $firstSpace + 1);
28    if ($secondSpace === false) {
29        die("Failed to understand version string [$versionInstalledStringComplete] - finding second space");
30    }
31
32    $versionInstalledString = substr($versionInstalledStringComplete, $firstSpace + 1, $secondSpace - $firstSpace - 1);
33    // echo "versionInstalledString is $versionInstalledString \n";
34
35    $versionToCompare = $im7Version;
36    if (substr($versionInstalledString, 0, 1) === '6') {
37        $versionToCompare = $testIm6Version;
38    }
39
40    return version_compare($versionInstalledString, $versionToCompare);
41}
42
43/**
44 *
45 * Compares the installed version of ImageMagick and returns true if the appropriate
46 * version is greater
47 *
48 * @param $testIm6Version
49 * @param $im7Version
50 * @return bool
51 */
52function isVersionGreaterEqual($testIm6Version, $im7Version)
53{
54    $versionCompare = version_compare_imagemagick($testIm6Version, $im7Version);
55    // echo "version compare for $testIm6Version, $im7Version is $versionCompare \n";
56
57    if ($versionCompare >= 0) {
58        return true;
59    }
60
61    return false;
62}
63
64/**
65 * On some systems, where the standard fonts aren't available, trying
66 * to draw any text fails as the ImageMagick default font is null.
67 *
68 * This function just find a 'sensible' font to use, either from the
69 * preferred list, or just the first one from queryFonts(). That 'probably'
70 * is the right thing to do, as it makes the tests more stable.
71 */
72function findDefaultFont()
73{
74    $knownFonts = [
75        'Courier',
76        'Helvetica',
77        'Times-Roman',
78        'Liberation-Mono',
79        'Utopia',
80    ];
81
82    $fontList = \Imagick::queryFonts();
83
84    foreach ($knownFonts as $knownFont) {
85
86        if (in_array($knownFont, $fontList, true) === true) {
87            return $knownFont;
88        }
89    }
90
91    if (count($fontList) !== 0) {
92        return $fontList[0];
93    }
94
95    throw new \Exception("No fonts available on system, apparently.");
96}
97
98// Find and set a font for the Imagick object
99function setFontForImagick(\Imagick $imagick)
100{
101    $font = findDefaultFont();
102
103    $imagick->setFont($font);
104}
105
106// Find and set a font for the ImagickDraw object
107function setFontForImagickDraw(\ImagickDraw $imagickDraw)
108{
109    $font = findDefaultFont();
110
111    $imagickDraw->setFont($font);
112}
113