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 Illuminate\Http\Testing;
4
 
5
class FileFactory
6
{
7
    /**
8
     * Create a new fake file.
9
     *
10
     * @param  string  $name
11
     * @param  string|int  $kilobytes
12
     * @param  string|null  $mimeType
13
     * @return \Illuminate\Http\Testing\File
14
     */
15
    public function create($name, $kilobytes = 0, $mimeType = null)
16
    {
17
        if (is_string($kilobytes)) {
18
            return $this->createWithContent($name, $kilobytes);
19
        }
20
 
21
        return tap(new File($name, tmpfile()), function ($file) use ($kilobytes, $mimeType) {
22
            $file->sizeToReport = $kilobytes * 1024;
23
            $file->mimeTypeToReport = $mimeType;
24
        });
25
    }
26
 
27
    /**
28
     * Create a new fake file with content.
29
     *
30
     * @param  string  $name
31
     * @param  string  $content
32
     * @return \Illuminate\Http\Testing\File
33
     */
34
    public function createWithContent($name, $content)
35
    {
36
        $tmpfile = tmpfile();
37
 
38
        fwrite($tmpfile, $content);
39
 
40
        return tap(new File($name, $tmpfile), function ($file) use ($tmpfile) {
41
            $file->sizeToReport = fstat($tmpfile)['size'];
42
        });
43
    }
44
 
45
    /**
46
     * Create a new fake image.
47
     *
48
     * @param  string  $name
49
     * @param  int  $width
50
     * @param  int  $height
51
     * @return \Illuminate\Http\Testing\File
52
     */
53
    public function image($name, $width = 10, $height = 10)
54
    {
55
        return new File($name, $this->generateImage(
56
            $width, $height, pathinfo($name, PATHINFO_EXTENSION)
57
        ));
58
    }
59
 
60
    /**
61
     * Generate a dummy image of the given width and height.
62
     *
63
     * @param  int  $width
64
     * @param  int  $height
65
     * @param  string  $extension
66
     * @return resource
67
     */
68
    protected function generateImage($width, $height, $extension)
69
    {
70
        return tap(tmpfile(), function ($temp) use ($width, $height, $extension) {
71
            ob_start();
72
 
73
            $extension = in_array($extension, ['jpeg', 'png', 'gif', 'webp', 'wbmp', 'bmp'])
74
                ? strtolower($extension)
75
                : 'jpeg';
76
 
77
            $image = imagecreatetruecolor($width, $height);
78
 
79
            call_user_func("image{$extension}", $image);
80
 
81
            fwrite($temp, ob_get_clean());
82
        });
83
    }
84
}