Skip to content

Commit 1c7e658

Browse files
committed
Add support for string-based file loading and saving.
1 parent 88dfe8b commit 1c7e658

File tree

3 files changed

+344
-15
lines changed

3 files changed

+344
-15
lines changed

.gitignore

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
#################
2+
## PHPStorm
3+
#################
4+
.idea/
5+
6+
#################
7+
## Eclipse
8+
#################
9+
10+
*.pydevproject
11+
.project
12+
.metadata
13+
bin/
14+
tmp/
15+
*.tmp
16+
*.bak
17+
*.swp
18+
*~.nib
19+
local.properties
20+
.classpath
21+
.settings/
22+
.loadpath
23+
24+
# External tool builders
25+
.externalToolBuilders/
26+
27+
# Locally stored "Eclipse launch configurations"
28+
*.launch
29+
30+
# CDT-specific
31+
.cproject
32+
33+
# PDT-specific
34+
.buildpath
35+
36+
37+
#################
38+
## Visual Studio
39+
#################
40+
41+
## Ignore Visual Studio temporary files, build results, and
42+
## files generated by popular Visual Studio add-ons.
43+
44+
# User-specific files
45+
*.suo
46+
*.user
47+
*.sln.docstates
48+
49+
# Build results
50+
51+
[Dd]ebug/
52+
[Rr]elease/
53+
x64/
54+
build/
55+
[Bb]in/
56+
[Oo]bj/
57+
58+
# MSTest test Results
59+
[Tt]est[Rr]esult*/
60+
[Bb]uild[Ll]og.*
61+
62+
*_i.c
63+
*_p.c
64+
*.ilk
65+
*.meta
66+
*.obj
67+
*.pch
68+
*.pdb
69+
*.pgc
70+
*.pgd
71+
*.rsp
72+
*.sbr
73+
*.tlb
74+
*.tli
75+
*.tlh
76+
*.tmp
77+
*.tmp_proj
78+
*.log
79+
*.vspscc
80+
*.vssscc
81+
.builds
82+
*.pidb
83+
*.log
84+
*.scc
85+
86+
# Visual C++ cache files
87+
ipch/
88+
*.aps
89+
*.ncb
90+
*.opensdf
91+
*.sdf
92+
*.cachefile
93+
94+
# Visual Studio profiler
95+
*.psess
96+
*.vsp
97+
*.vspx
98+
99+
# Guidance Automation Toolkit
100+
*.gpState
101+
102+
# ReSharper is a .NET coding add-in
103+
_ReSharper*/
104+
*.[Rr]e[Ss]harper
105+
106+
# TeamCity is a build add-in
107+
_TeamCity*
108+
109+
# DotCover is a Code Coverage Tool
110+
*.dotCover
111+
112+
# NCrunch
113+
*.ncrunch*
114+
.*crunch*.local.xml
115+
116+
# Installshield output folder
117+
[Ee]xpress/
118+
119+
# DocProject is a documentation generator add-in
120+
DocProject/buildhelp/
121+
DocProject/Help/*.HxT
122+
DocProject/Help/*.HxC
123+
DocProject/Help/*.hhc
124+
DocProject/Help/*.hhk
125+
DocProject/Help/*.hhp
126+
DocProject/Help/Html2
127+
DocProject/Help/html
128+
129+
# Click-Once directory
130+
publish/
131+
132+
# Publish Web Output
133+
*.Publish.xml
134+
*.pubxml
135+
*.publishproj
136+
137+
# NuGet Packages Directory
138+
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
139+
#packages/
140+
141+
# Windows Azure Build Output
142+
csx
143+
*.build.csdef
144+
145+
# Windows Store app package directory
146+
AppPackages/
147+
148+
# Others
149+
sql/
150+
*.Cache
151+
ClientBin/
152+
[Ss]tyle[Cc]op.*
153+
~$*
154+
*~
155+
*.dbmdl
156+
*.[Pp]ublish.xml
157+
*.pfx
158+
*.publishsettings
159+
160+
# RIA/Silverlight projects
161+
Generated_Code/
162+
163+
# Backup & report files from converting an old project file to a newer
164+
# Visual Studio version. Backup files are not needed, because we have git ;-)
165+
_UpgradeReport_Files/
166+
Backup*/
167+
UpgradeLog*.XML
168+
UpgradeLog*.htm
169+
170+
# SQL Server files
171+
App_Data/*.mdf
172+
App_Data/*.ldf
173+
174+
#############
175+
## Windows detritus
176+
#############
177+
178+
# Windows image file caches
179+
Thumbs.db
180+
ehthumbs.db
181+
182+
# Folder config file
183+
Desktop.ini
184+
185+
# Recycle Bin used on file shares
186+
$RECYCLE.BIN/
187+
188+
# Mac crap
189+
.DS_Store
190+
191+
192+
#############
193+
## Python
194+
#############
195+
196+
*.py[cod]
197+
198+
# Packages
199+
*.egg
200+
*.egg-info
201+
dist/
202+
build/
203+
eggs/
204+
parts/
205+
var/
206+
sdist/
207+
develop-eggs/
208+
.installed.cfg
209+
210+
# Installer logs
211+
pip-log.txt
212+
213+
# Unit test / coverage reports
214+
.coverage
215+
.tox
216+
217+
#Translations
218+
*.mo
219+
220+
#Mr Developer
221+
.mr.developer.cfg
222+
.idea/deployment.xml
223+
.idea/php.xml
224+
.idea/vagrant.xml

src/ImageResize.php

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,87 @@ class ImageResize
3939
protected $source_h;
4040

4141
/**
42-
* Constructor
42+
* Create instance from a file
43+
*
4344
* @param string $filename
45+
* @return ImageResize
46+
* @throws \Exception
47+
*/
48+
public static function createFromFile($filename){
49+
$s = new self();
50+
$s->load($filename);
51+
return $s;
52+
}
53+
54+
/**
55+
* Create instance from a strng
56+
*
57+
* @param string $imageData
58+
* @return ImageResize
59+
* @throws \exception
60+
*/
61+
public static function createFromString($imageData){
62+
$s = new self();
63+
$s->loadFromString($imageData);
64+
return $s;
65+
}
66+
67+
/**
68+
* Constructor
69+
*
70+
* @param string|null $filename
71+
* @throws \Exception
4472
*/
45-
public function __construct($filename)
73+
public function __construct($filename=null)
4674
{
47-
$this->load($filename);
75+
if(!empty($filename)){
76+
$this->load($filename);
77+
}
78+
}
79+
80+
/**
81+
* Get image size from string
82+
*
83+
* @param string $imagedata
84+
* @return array
85+
*/
86+
protected function getImagesizeFromString($imagedata){
87+
return @getimagesize('data://application/octet-stream;base64,' . base64_encode($imagedata));
4888
}
4989

5090
/**
51-
* Loads image source and its properties to the instanciated object
91+
* Load image from string
92+
*
93+
* @param string $imagedata
94+
* @return ImageResize
95+
* @throws \Exception
96+
*/
97+
public function loadFromString($imagedata)
98+
{
99+
$image_info = $this->getImagesizeFromString($imagedata);
100+
if(!$image_info) {
101+
throw new \Exception('Could not load image from string');
102+
}
103+
104+
list (
105+
$this->original_w,
106+
$this->original_h,
107+
$this->source_type
108+
) = $image_info;
109+
110+
$this->source_image = imagecreatefromstring($imagedata);
111+
112+
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
113+
}
114+
115+
/**
116+
* Load a file
117+
*
52118
* @param string $filename
53-
* @return \static
54-
* @throws Exception
119+
* @return ImageResize
120+
* @throws \Exception
55121
*/
56-
protected function load($filename)
122+
public function load($filename)
57123
{
58124
$image_info = getimagesize($filename);
59125

@@ -161,11 +227,25 @@ public function save($filename, $image_type = null, $quality = null, $permission
161227

162228
return $this;
163229
}
164-
230+
165231
/**
166-
* Outpus image source to browser
167-
* @param string $image_type
168-
* @param integer $quality
232+
* Return image as string
233+
*
234+
* @param int $image_type
235+
* @param int $quality
236+
* @return string
237+
*/
238+
public function toString($image_type = null, $quality = null){
239+
ob_start();
240+
$this->save(null, $image_type, $quality);
241+
return ob_get_clean();
242+
}
243+
244+
/**
245+
* Outputs image source to browser
246+
*
247+
* @param int $image_type
248+
* @param int $quality
169249
*/
170250
public function output($image_type = null, $quality = null)
171251
{

0 commit comments

Comments
 (0)