Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Faker\Provider;
4
 
5
/**
6
 * Depends on image generation from http://lorempixel.com/
7
 */
8
class Image extends Base
9
{
10
    /**
11
     * @var string
12
     */
13
    public const BASE_URL = 'https://via.placeholder.com';
14
 
15
    public const FORMAT_JPG = 'jpg';
16
    public const FORMAT_JPEG = 'jpeg';
17
    public const FORMAT_PNG = 'png';
18
 
19
    /**
20
     * @var array
21
     *
22
     * @deprecated Categories are no longer used as a list in the placeholder API but referenced as string instead
23
     */
24
    protected static $categories = [
25
        'abstract', 'animals', 'business', 'cats', 'city', 'food', 'nightlife',
26
        'fashion', 'people', 'nature', 'sports', 'technics', 'transport',
27
    ];
28
 
29
    /**
30
     * Generate the URL that will return a random image
31
     *
32
     * Set randomize to false to remove the random GET parameter at the end of the url.
33
     *
34
     * @example 'http://via.placeholder.com/640x480.png/CCCCCC?text=well+hi+there'
35
     *
36
     * @param int         $width
37
     * @param int         $height
38
     * @param string|null $category
39
     * @param bool        $randomize
40
     * @param string|null $word
41
     * @param bool        $gray
42
     * @param string      $format
43
     *
44
     * @return string
45
     */
46
    public static function imageUrl(
47
        $width = 640,
48
        $height = 480,
49
        $category = null,
50
        $randomize = true,
51
        $word = null,
52
        $gray = false,
53
        $format = 'png'
54
    ) {
55
        trigger_deprecation(
56
            'fakerphp/faker',
57
            '1.20',
58
            'Provider is deprecated and will no longer be available in Faker 2. Please use a custom provider instead',
59
        );
60
 
61
        // Validate image format
62
        $imageFormats = static::getFormats();
63
 
64
        if (!in_array(strtolower($format), $imageFormats, true)) {
65
            throw new \InvalidArgumentException(sprintf(
66
                'Invalid image format "%s". Allowable formats are: %s',
67
                $format,
68
                implode(', ', $imageFormats),
69
            ));
70
        }
71
 
72
        $size = sprintf('%dx%d.%s', $width, $height, $format);
73
 
74
        $imageParts = [];
75
 
76
        if ($category !== null) {
77
            $imageParts[] = $category;
78
        }
79
 
80
        if ($word !== null) {
81
            $imageParts[] = $word;
82
        }
83
 
84
        if ($randomize === true) {
85
            $imageParts[] = Lorem::word();
86
        }
87
 
88
        $backgroundColor = $gray === true ? 'CCCCCC' : str_replace('#', '', Color::safeHexColor());
89
 
90
        return sprintf(
91
            '%s/%s/%s%s',
92
            self::BASE_URL,
93
            $size,
94
            $backgroundColor,
95
            count($imageParts) > 0 ? '?text=' . urlencode(implode(' ', $imageParts)) : '',
96
        );
97
    }
98
 
99
    /**
100
     * Download a remote random image to disk and return its location
101
     *
102
     * Requires curl, or allow_url_fopen to be on in php.ini.
103
     *
104
     * @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.png'
105
     *
106
     * @return bool|string
107
     */
108
    public static function image(
109
        $dir = null,
110
        $width = 640,
111
        $height = 480,
112
        $category = null,
113
        $fullPath = true,
114
        $randomize = true,
115
        $word = null,
116
        $gray = false,
117
        $format = 'png'
118
    ) {
119
        trigger_deprecation(
120
            'fakerphp/faker',
121
            '1.20',
122
            'Provider is deprecated and will no longer be available in Faker 2. Please use a custom provider instead',
123
        );
124
 
125
        $dir = null === $dir ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
126
        // Validate directory path
127
        if (!is_dir($dir) || !is_writable($dir)) {
128
            throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir));
129
        }
130
 
131
        // Generate a random filename. Use the server address so that a file
132
        // generated at the same time on a different server won't have a collision.
133
        $name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true));
134
        $filename = sprintf('%s.%s', $name, $format);
135
        $filepath = $dir . DIRECTORY_SEPARATOR . $filename;
136
 
137
        $url = static::imageUrl($width, $height, $category, $randomize, $word, $gray, $format);
138
 
139
        // save file
140
        if (function_exists('curl_exec')) {
141
            // use cURL
142
            $fp = fopen($filepath, 'w');
143
            $ch = curl_init($url);
144
            curl_setopt($ch, CURLOPT_FILE, $fp);
145
            $success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;
146
            fclose($fp);
147
            curl_close($ch);
148
 
149
            if (!$success) {
150
                unlink($filepath);
151
 
152
                // could not contact the distant URL or HTTP error - fail silently.
153
                return false;
154
            }
155
        } elseif (ini_get('allow_url_fopen')) {
156
            // use remote fopen() via copy()
157
            $success = copy($url, $filepath);
158
 
159
            if (!$success) {
160
                // could not contact the distant URL or HTTP error - fail silently.
161
                return false;
162
            }
163
        } else {
164
            return new \RuntimeException('The image formatter downloads an image from a remote HTTP server. Therefore, it requires that PHP can request remote hosts, either via cURL or fopen()');
165
        }
166
 
167
        return $fullPath ? $filepath : $filename;
168
    }
169
 
170
    public static function getFormats(): array
171
    {
172
        trigger_deprecation(
173
            'fakerphp/faker',
174
            '1.20',
175
            'Provider is deprecated and will no longer be available in Faker 2. Please use a custom provider instead',
176
        );
177
 
178
        return array_keys(static::getFormatConstants());
179
    }
180
 
181
    public static function getFormatConstants(): array
182
    {
183
        trigger_deprecation(
184
            'fakerphp/faker',
185
            '1.20',
186
            'Provider is deprecated and will no longer be available in Faker 2. Please use a custom provider instead',
187
        );
188
 
189
        return [
190
            static::FORMAT_JPG => constant('IMAGETYPE_JPEG'),
191
            static::FORMAT_JPEG => constant('IMAGETYPE_JPEG'),
192
            static::FORMAT_PNG => constant('IMAGETYPE_PNG'),
193
        ];
194
    }
195
}