Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFactory.php
More file actions
Latest commit
186 lines (160 loc) · 5.57 KB
/
Copy pathImageFactory.php
File metadata and controls
186 lines (160 loc) · 5.57 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespaceAigerTeam\ImageTools;
useAigerTeam\ImageTools\Exceptions\FileException;
useAigerTeam\ImageTools\Exceptions\ImageFileException;
/**
* Class ImageFactory
*
* Image factory. Creates images by various ways.
*
* @version 1.0.7
* @author Finesse
* @package AigerTeam\ImageTools
*/
class ImageFactory
{
/**
* @param int $memoryLimit Amount of bytes to which memory limit should be extended. Too small amount may cause
* "not enough memory" error while handling images.
*/
publicfunction__construct( $memoryLimit = 134217728 )
{
@ini_set( 'gd.jpeg_ignore_warning', true );
try {
$this->extendMemoryLimit( $memoryLimit );
} catch ( \Exception$e ) {}
}
/**
* Creates blank image.
*
* @param int $width Image width (px)
* @param int $height Image height (px)
* @param float[]|null $fill Fill color (array with keys `r`, `g`, `b` and optional `a`). If null is provided image
* will be fully transparent.
* @return Image
* @throws \Exception
*
* @since 1.0.0
*/
publicfunctionblank( $width, $height, Array$fill = null )
{
$bitmap = @imagecreatetruecolor( $width, $height );
if ( !$bitmap )
thrownew \Exception( 'Can\'t create blank image. Perhaps not enough RAM.' );
$color = Image::allocateColor( $bitmap, $fill );
$colorParts = imagecolorsforindex( $bitmap, $color );
$isTransparent = !empty( $colorParts[ 'alpha' ] );
@imagealphablending( $bitmap, false );
@imagefill( $bitmap, 0, 0, $color );
returnnewImage( $bitmap, $isTransparent );
}
/**
* Reads image from file.
*
* @param string $file Path to the image file in the filesystem.
* @return Image
* @throws ImageFileException If image can't be retrieved from the file
* @throws FileException If file can't be read
* @throws \Exception In case of other error
*
* @since 1.0.0
*/
publicfunctionopenFile( $file )
{
// Clear file info cache
@clearstatcache( true, $file );
// Does file exist?
if ( !file_exists( $file ) || !is_file( $file ))
thrownewFileException( 'Given file doesn\'t exist or not a file.', $file );
// Is it readable?
if ( !is_readable( $file ))
thrownewFileException( 'Given file isn\'t readable.', $file );
// Is it image?
$size = getimagesize( $file );
if ( $size === false )
thrownewImageFileException( 'Given file isn\'t an image.', $file );
// Retrieve image type
$format = strtolower( substr( $size['mime'], strpos( $size['mime'], '/' ) + 1 ) );
if ($format === 'x-ms-bmp')
$format = 'wbmp';
// Does function that opens this type exist?
$func = 'imagecreatefrom' . $format;
if ( !function_exists( $func ) )
thrownewImageFileException( 'Unknown image type.', $file );
// Open image file
$bitmap = @$func( $file );
if ( !$bitmap )
thrownewImageFileException( 'Can\'t open image file. Perhaps not enough RAM.', $file );
// Create image object
$image = newImage( $bitmap, in_array( $format, Array( 'png', 'gif' ) ) );
// Rotate non-default oriended JPEG
if (
function_exists( 'exif_read_data' ) &&
is_array( $exif = @exif_read_data( $file, 0, true )) &&
isset( $exif[ 'IFD0' ][ 'Orientation' ])
) {
switch( $exif[ 'IFD0' ][ 'Orientation' ]) {
case8: $image = $image->rotate( 90 ); break;
case3: $image = $image->rotate( 180 ); break;
case6: $image = $image->rotate( -90 ); break;
}
}
return$image;
}
/**
* Craetes image from screenshot.
*
* Works only on windows (due to http://php.net/manual/en/function.imagegrabscreen.php#refsect1-function.imagegrabscreen-notes).
*
* @return Image
* @throws \Exception
*
* @since 1.0.5
*/
publicfunctionscreenshot()
{
if ( !function_exists( 'imagegrabscreen' ) )
thrownew \Exception( 'Current PHP assembly can\'t take screenshots.' );
$bitmap = @imagegrabscreen();
if ( !$bitmap )
thrownew \Exception( 'Can\'t take screenshot due to an unknown reason.' );
returnnewImage( $bitmap );
}
/**
* Extend system memory limit to the given value (if current limit is lower).
*
* @param int $minSize Amount of bytes
*/
protectedfunctionextendMemoryLimit( $minSize )
{
$curSize = ini_get( 'memory_limit' );
if ( !is_numeric( $curSize ) ) {
$base = substr( $curSize, 0, -1 );
$power = strtolower( substr( $curSize, -1 ) );
switch ( $power ) {
case'k':
$curSize = $base * 1024; // 640K ought to be enough for anybody
break;
case'm':
$curSize = $base * pow( 1024, 2 );
break;
case'g':
$curSize = $base * pow( 1024, 3 );
break;
case't':
$curSize = $base * pow( 1024, 4 );
break;
case'p':
$curSize = $base * pow( 1024, 5 ); // Someday this will also be not enough
break;
default:
$curSize = 0;
}
} else {
$curSize = (int)$curSize;
}
if ( $minSize > $curSize ) {
ini_set( 'memory_limit', $minSize );
}
}
}