-
Notifications
You must be signed in to change notification settings - Fork 0
/
ios-resize
executable file
·300 lines (263 loc) · 11.8 KB
/
ios-resize
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/php
<?php
/*******************************************************************************************************
* This software, ios-resize, is built by Tommy Leung on top of work by Jonathan Ellis on his blog:
* http://jona.than.biz/blog/generating-non-retina-images-automatically-for-ios/. Features not found in
* original implementation include:
*
* - cocos2d format support
* - recursively directory search
* - make iOS icons
* - usage instructions in command line
* - comments on how it works (if you care)
*
* ios-resize was developed for the Mac OS X 10.8 operating system since most iOS development happens on a
* Mac and PHP comes preinstalled on every Mac.
*
* USAGE:
*
* In the Terminal, run:
*
* ios-resize *
* This will resize all images in the current folder.
*
* ios-resize file@2x.png file2@2x.png
* This will resize the two images specified.
*
* ios-resize * -d
* This will resize all images in the current directory as well as all children directory recursively.
*
* ios-resize -icon icon_file.png
* This will take the file "icon_file.png" and make the appropriate icon files required by iOS. Make sure the
* source image ("icon_file.png") has square dimensions for best results.
*
* INSTALLATION
*
* 1. Make sure your path to PHP is located at /usr/bin/php otherwise edit the first line to point to PHP.
* 1a. Save the file if you changed the path to PHP.
* 2. In the Terminal, go to where the ios-resize file is and run:
* chmod 777 ios-resize
* 3. Then copy the file to your /usr/bin directory by running:
* cp ./ios-resize /usr/bin/ios-resize
* You may get a permission error so instead do:
* sudo cp ./ios-resize /usr/bin/ios-resize
* And enter your password when prompted. This assumes your user account has proper permissions to write
* to the /usr/bin folder.
*
* ALTERNATE INSTALLATION
*
* 1. Make sure your path to PHP is located at /usr/bin/php otherwise edit the first line to point to PHP.
* 1a. Save the file if you changed the path to PHP.
* 2. And you're done. The only difference is that usage will look like this instead:
* php ios-resize * -d
*
* ios-resize is released under the MIT License.
*
* Copyright (C) 2012 Tommy Leung (http://www.supertommy.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************************/
$args = $_SERVER['argv'];
$numArgs = count($args);
//options
$lookInDirectories = false;
$makeIcons = false;
if($numArgs == 1) //first arg is always the file name so 1 arg is no args
{
echo "** ios-resize is a command line tool for resizing retina resolution PNG images to non-retina resolutions in Apple's naming convention (@2x) as well as the cocos2d framework convention (-hd, -ipadhd).\n\n";
echo "** usage:\n";
echo "ios-resize * [options]\n";
echo "Resize all images in current directory with @2x, -hd, or -ipadhd in file name\n\n";
echo "ios-resize file@2x.png file2@2x.png [options]\n";
echo "Resize images file@2x.png and file2@2x.png with @2x, -hd, or -ipadhd in file name\n\n";
echo "** options:\n";
echo "-d\n";
echo "Recursively search through all children directories and resize those images as well.\n";
echo "Example: \"ios-resize * -d\" will resize all images in current directory and children directories recursively.\n\n";
echo "[src file] -icon\n";
echo "This will take the file [src file] and make the appropriate icon files required by iOS. Make sure the source image has square dimensions for best results.\n";
echo "Example: \"ios-resize icon_file.png -icon\"\n\n";
exit();
}
else
{
//process options--currently only recursive directory search and make icons
for($i = 1; $i < $numArgs; ++$i)
{
if(strcmp($args[$i], "-d") == 0)
{
$lookInDirectories = true;
break;
}
else if(strcmp($args[$i], "-icon") == 0)
{
$makeIcons = true;
if($numArgs != 3)
echo "Error: Incorrect number of arguments for making icons. Please specify one image file.\n";
break;
}
}
}
//wraps is_dir and doesn't allow for . or ..
function isAcceptableDirectory($dir)
{
if(!isAcceptableFilename($dir))
return false;
return is_dir($dir);
}
//checks to make sure a filename is not . or ..
function isAcceptableFilename($filename)
{
$ignoreList = array(".", "..", ".git", ".svn");
for($i = 0; $i < count($ignoreList); ++$i)
{
if(strcmp($ignoreList[$i], $filename) == 0)
return false;
}
return true;
}
function makeCopyOfImageWithNewDimensions($filename, $newFilename, $newWidth=-1, $newHeight=-1)
{
//make a png image resource from the current one
$image = imagecreatefrompng($filename);
//get the size of the image
$width = imagesx($image);
$height = imagesy($image);
//auto calculate the new width and height and assume retina resize if newWidth or newHeight is -1
if($newWidth == -1 || $newHeight == -1)
{
$newWidth = $width/2.0;
$newHeight = $height/2.0;
}
//create a new image with the newly calculated width and height
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($newImage, false); //set imagealphablending to false so that imagesavesalpha will work
if(!imagesavealpha($newImage, true)) //attempt to save full alpha channel information
echo $spacing . "Error: Failed to save full alpha channel information for " . $filename . ". Unexpected results may occur.\n";
//allocate a completely transparent color; params for RGB don't make a difference, A is set to full transparency at 127
$newImageColor = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
//make a transparent color with allocated color above
$color = imagecolortransparent($newImage, $newImageColor);
//fill the new image with color defined above at 0, 0 (top left)
imagefill($newImage, 0, 0, $color);
//copy the source image into the new image at new width and height with smooth interpolation
if(!imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height))
echo $spacing . "Error: Failed to properly resample " . $filename . ". Unexpected results may occur.\n";
//create the new png image and save it in the same directory as the original
imagepng($newImage, $newFilename);
}
//image resize function; $spacing is for output to terminal
function resizeImage($filename, $dir=false, $spacing="")
{
//recursively look into directories if $dir is true
if($dir && isAcceptableDirectory($filename))
{
if($handle = opendir($filename))
{
echo $spacing . "Processing images in " . $filename . " directory . . .\n\n";
$spacing .= " ";
while(($file = readdir($handle)) !== false)
{
//make sure the file is acceptable (not . or .. before it's concatenated with the dir path)
if(isAcceptableFilename($file))
{
resizeImage($filename."/".$file, $dir, $spacing);
}
}
echo $spacing . "Finished processing images in " . $filename . " directory . . .\n\n";
closedir($handle);
}
else
echo $spacing . "Failed to open directory: " . $filename . "\n\n";
}
else if(is_file($filename) && strpos($filename, ".png")) //only resize png files
{
$appleFormat = false;
$cocos2dFormat = false;
$cocos2dFormat_ipad = false;
$appleFormat = strrpos($filename, "@2x", -1);
//check that the @2x is at the end of the file name (7 spaces from the end of the string: xxxx[@2x.png])
if($appleFormat && $appleFormat < strlen($filename) - 7)
echo $spacing . "Warning: @2x suffix was discovered in unexpected location. Unexpected results may occur.\n";
if(!$appleFormat) //@2x not found so let's try -hd
{
$cocos2dFormat = strrpos($filename, "-hd", -1);
if($cocos2dFormat && $cocos2dFormat < strlen($filename) - 7)
echo $spacing . "Warning: -hd suffix was discovered in unexpected location. Unexpected results may occur.\n";
}
if(!$appleFormat && !$cocos2dFormat) //@2x and -hd not found so let's try -ipadhd
{
$cocos2dFormat_ipad = strrpos($filename, "-ipadhd", -1);
if($cocos2dFormat_ipad && $cocos2dFormat_ipad < strlen($filename) - 11)
echo $spacing . "Warning: -ipadhd suffix was discovered in unexpected location. Unexpected results may occur.\n";
}
if ($appleFormat || $cocos2dFormat || $cocos2dFormat_ipad)
{
echo $spacing . "Processing image " . $filename . " . . .\n";
if($appleFormat)
echo $spacing . "Image uses Apple convention: @2x\n";
else if($cocos2dFormat)
echo $spacing . "Image uses cocos2d convention: -hd\n";
else if($cocos2dFormat_ipad)
echo $spacing . "Image uses cocos2d-iPad convention: -ipadhd\n";
//create the appropriate new filename for non-retina image
if($appleFormat)
$newFilename = str_replace("@2x", "", $filename);
else if($cocos2dFormat)
$newFilename = str_replace("-hd", "", $filename);
else if($cocos2dFormat_ipad)
$newFilename = str_replace("-ipadhd", "-ipad", $filename);
makeCopyOfImageWithNewDimensions($filename, $newFilename);
echo $spacing . "Finished processing " . $filename . "\n\n";
}
}
}
//start here
if($makeIcons)
{
$icons = array();
//add icons to 2D array where each index holds an array with index 0, 1, 2 as name, width, height
array_push($icons, array("Icon-144.png", 144, 144));
array_push($icons, array("Icon@2x.png", 114, 114));
array_push($icons, array("Icon-72.png", 72, 72));
array_push($icons, array("Icon-Small@2x.png", 58, 58));
array_push($icons, array("Icon.png", 57, 57));
array_push($icons, array("Icon-Small-50.png", 50, 50));
array_push($icons, array("Icon-Small.png", 29, 29));
for($i = 1; $i < $numArgs; ++$i)
{
$filename = $args[$i];
if(isAcceptableFilename($filename) && strcmp($filename, "-icon") != 0)
{
//make the icons
for($j = 0; $j < count($icons); ++$j)
{
$icon = $icons[$j];
makeCopyOfImageWithNewDimensions($filename, $icon[0], $icon[1], $icon[2]);
}
}
}
}
else
{
for ($i = 1; $i < $numArgs; $i++)
{
$filename = $args[$i];
resizeImage($filename, $lookInDirectories);
}
}
echo "ios-resize finished.\n";
?>