forked from php-collective/file-storage-image-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
140 lines (110 loc) · 4.32 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use PhpCollective\Infrastructure\Storage\Factories\LocalFactory;
use PhpCollective\Infrastructure\Storage\FileFactory;
use PhpCollective\Infrastructure\Storage\PathBuilder\PathBuilder;
use PhpCollective\Infrastructure\Storage\Processor\Image\ImageProcessor;
use PhpCollective\Infrastructure\Storage\Processor\Image\ImageVariantCollection;
use PhpCollective\Infrastructure\Storage\FileStorage;
use PhpCollective\Infrastructure\Storage\StorageAdapterFactory;
use PhpCollective\Infrastructure\Storage\StorageService;
use Intervention\Image\ImageManager;
/*******************************************************************************
* Just a Utility function for this example and some output
******************************************************************************/
function readableSize(int $size, int $precision = 2)
{
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {}
return round($size, $precision) . ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][$i];
}
function memoryOutput()
{
echo PHP_EOL;
echo 'Peak Memory: ' . readableSize(memory_get_peak_usage(true)) . PHP_EOL;
echo 'Memory: ' . readableSize(memory_get_usage(true)) . PHP_EOL;
echo PHP_EOL;
}
memoryOutput();
/*******************************************************************************
* Configuring the stores - Your DI container or bootstrapping should do this
******************************************************************************/
$ds = DIRECTORY_SEPARATOR;
$storageService = new StorageService(
new StorageAdapterFactory()
);
$storageService->setAdapterConfigFromArray([
'local' => [
'class' => LocalFactory::class,
'options' => [
'root' => '.' . $ds . 'tmp' . $ds . 'storage1' . $ds
]
],
'local2' => [
'class' => LocalFactory::class,
'options' => [
'root' => '.' . $ds . 'tmp' . $ds . 'storage2' . $ds
]
]
]);
/*******************************************************************************
* Build services - Your DI container should do this for you
******************************************************************************/
$pathBuilder = new PathBuilder();
$fileStorage = new FileStorage(
$storageService,
$pathBuilder
);
$imageManager = new ImageManager([
'driver' => 'gd'
]);
$imageProcessor = new ImageProcessor(
$fileStorage,
$pathBuilder,
$imageManager
);
/*******************************************************************************
* Working with files
*
* This is a very exhaustive example for demonstrating what can bed done,
* setting the id would be already enough!
******************************************************************************/
$file = FileFactory::fromDisk('./tests/Fixtures/titus.jpg', 'local')
->withUuid('914e1512-9153-4253-a81e-7ee2edc1d973')
->withFilename('foobar.jpg')
->addToCollection('avatar')
->belongsToModel('User', '1')
->withMetadata([
'one' => 'two',
'two' => 'one'
])
->withMetadataKey('bar', 'foo');
$file = $fileStorage->store($file);
echo var_export($file->toArray(), true);
echo PHP_EOL . PHP_EOL;
/*******************************************************************************
* Creating manipulated versions of the file
******************************************************************************/
$collection = ImageVariantCollection::create();
$collection->addNew('resizeAndFlip')
->flipHorizontal()
->resize(300, 300)
->optimize();
$collection->addNew('crop')
->crop(100, 100);
$file = $file->withVariants($collection->toArray());
$file = $imageProcessor
->processOnlyTheseVariants([
//'resizeAndFlip'
])
->process($file);
echo var_export($file->toArray(), true);
echo PHP_EOL;
/*******************************************************************************
* Removing the file
******************************************************************************/
//$fileStorage->remove($file);
/*******************************************************************************
* Just some output
******************************************************************************/
memoryOutput();