-
Notifications
You must be signed in to change notification settings - Fork 39
/
Tools.vue
800 lines (702 loc) · 24.7 KB
/
Tools.vue
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
<template>
<div class="container col-xxl-8 px-4 py-5">
<div class="row">
<div class="col-md-12">
<img
src="@/assets/logo.png"
style="
float: left;
margin-top: 2em;
margin-right: 2em;
height: 5em;
width: auto;
"
class="img-circle"
/>
<h2>Ape Tools - Image Gorilla</h2>
<p class="lead">
Tired of creating all your icons and splashscreen sizes manually?
You've come to the right place.
</p>
<p>
Upload your Icons and/or Splashscreen and we'll do all the hard work
to create the sizes you need for each platform:
</p>
<p>
Need to design a logo first? Try
<a target="_blank" href="https://logojoy.grsm.io/apetools">Logojoy</a>
</p>
<p>
Want to quickly make some PNGs (or JPGs) tiny? Check out
<a target="_blank" href="https://www.betweenelements.com/tinypng-app"
>tinyPNG.App</a
>
</p>
<p>
Is it just too hard to manage your website certificates? See
<a target="_blank" href="https://certifytheweb.com"
>Certify the web</a
>
</p>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h3>Step 1 - Select Your Icon</h3>
<div class="input-group file-upload">
<input
type="file"
accept="image/*"
id="fileupload-icon"
v-on:change="selectionChanged('icon', $event)"
name="FileUploadIcon"
/>
</div>
<p>
Select an Icon image in PNG format (transparency allowed)
measuring 1024x1024 pixels.
</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h3>Step 2 - Select Your Splashscreen</h3>
<div class="input-group file-upload">
<input
type="file"
accept="image/*"
id="fileupload-splash"
v-on:change="selectionChanged('splash', $event)"
name="FileUploadSplash"
/>
</div>
<p>
Select a Splashscreen image in PNG format (no transparency)
measuring 4096*4096 pixels with the important stuff centred,
we'll then resize from the centre outwards, sampling the top
left corner colour as background.
<span class="sample-swatch" :style="sampleStyle"> </span>
</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12"></div>
</div>
<div v-if="selectedIconFile || selectedSplashFile">
<h4 class="clearfix" style="clear: left">
Step 3 - Where the magic happens
</h4>
<p v-if="!isProcessing && !isArchiveReady" class="alert alert-info">
Ready to Punch It?
<button
type="button"
v-on:click="process"
class="btn btn-primary btn-lg"
>
<i class="fa fa-thumbs-up" aria-hidden="true"></i> Kapow!
</button>
<input type="checkbox" :id="showPreviews" v-model="showPreviews" />
<label for="checkbox"> show previews</label>
</p>
<div v-if="isProcessing">
<i class="fa fa-spinner fa-spin fa-3x fa-fw" aria-hidden="true"></i>
{{ msg }}
<div class="progress">
<div
class="progress-bar"
role="progressbar"
aria-valuenow="70"
aria-valuemin="0"
aria-valuemax="100"
v-bind:style="{ width: processingProgress + '%' }"
>
<span class="sr-only"
>Processing {{ processingProgress }}% Complete</span
>
</div>
</div>
</div>
<div v-if="isArchiveReady">
<button class="btn btn-success btn-lg" v-on:click="downloadZip">
Download Zip
</button>
<a href="https://apetools.webprofusion.com" class="ms-2">Try Again</a>
</div>
<p>
We will generate an asset bundle you can download for the following
platforms:
</p>
<div
v-for="platform of allPlatforms"
:key="platform.id"
style="
width: 300px;
min-height: 100px;
display: inline-block;
border: 1px solid gainsboro;
box-shadow: #c0c0c0 2px 2px;
padding: 1em;
margin: 1em;
"
>
<h3>
<i class="fa fa-lg fa-fw" v-bind:class="platform.icon"></i>
{{ platform.title }}
</h3>
<p class="lead">
{{
platform.bundleSpecs.filter((f) => f.category == "icon")[0]
.imageSet.length
}}
icon sizes,
{{
platform.bundleSpecs.filter((f) => f.category == "splash")[0]
.imageSet.length
}}
splashscreen sizes.
</p>
<input
type="checkbox"
:id="platform.path"
v-model="platform.includeInBundle"
/>
<label for="checkbox"> include in bundle </label>
<div v-if="platform.includeInBundle">
<p>{{ platform.instructions }}</p>
<div v-if="showPreviews">
<div
v-for="bundle of platform.bundleSpecs"
:key="bundle.category"
>
<h2>{{ bundle.category }}</h2>
<input
v-model="bundle.prefix"
placeholder="file name prefix"
/>
<ul
class="list-group"
v-for="(fileSpec, i) of bundle.imageSet"
:key="i"
>
<li class="list-group-item">
{{ fileSpec.path }} {{ fileSpec.prefix }}
{{ fileSpec.idiom }} {{ fileSpec.fileName }}:
{{ fileSpec.width * fileSpec.scale }}x{{
fileSpec.height * fileSpec.scale
}}
<img
v-if="fileSpec.imgData"
:src="fileSpec.imgData"
style="max-width: 200px; max-height: 200px"
class="img-thumbnail"
/>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="well well-sm">
<p>
We'd really appreciate it if you could tell others about this app:
<a
href="https://twitter.com/share"
class="twitter-share-button"
data-url="https://apetools.webprofusion.com"
data-text="Check out Ape Tools, the quick way to generate all the icons and splash screens for your app:"
data-via="webprofusion"
>Share</a
>
</p>
</div>
</div>
</div>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { Logger } from "../util/log";
import { saveAs } from "file-saver";
import JSZip from "jszip";
import pica from "pica";
import { setTimeout } from "timers";
import {
PlatformSpec,
RGBA,
BundleSpec,
ImageSpec,
ImageDataSet,
} from "../models/models";
import { PlatformFileSpecs } from "./../models/platformFileSpecs";
@Options({})
export default class Tools extends Vue {
protected logger: Logger = new Logger();
msg = "";
allPlatforms: Array<PlatformSpec> = [];
zipArchive: JSZip | undefined;
itemsProcessed = 0;
isArchiveReady = false;
isProcessing = false;
isCropSplashFromCentreMode = false;
selectedIconFile: any = null;
selectedSplashFile: any = null;
processingProgress = 0;
totalTasks = 0;
showPreviews = false;
iconSrcData: string | null = null;
iconImgData: ImageData | undefined;
splashSrcData: string | undefined;
splashImgData: ImageData | undefined;
splashColourSample: RGBA = { r: 0, g: 0, b: 0, a: 255 };
pica = new pica({ features: ["js","wasm"] });
get sampleStyle(): string {
return `background-color: rgb(${this.splashColourSample.r}, ${this.splashColourSample.g}, ${this.splashColourSample.b});`;
}
mounted() {
if (!this.logger) this.logger = new Logger();
this.allPlatforms = PlatformFileSpecs.Specs();
// prepare new zip for archive
this.zipArchive = new JSZip();
}
async selectionChanged(imageType: string, e: any) {
// icon/splash changed
if (imageType === "splash") {
this.selectedSplashFile = e.target.files[0];
// get image and sample it
let result = await this.getSourceImageForProcessing(
"splash",
this.selectedSplashFile
);
this.splashImgData = result.imgData;
// sample splash corner colour for canvas fill
this.splashColourSample = this.samplePixelValue(
this.splashImgData,
10,
10
);
}
if (imageType === "icon") {
this.selectedIconFile = e.target.files[0];
}
}
samplePixelValue(
imagedata: ImageData,
sampleX: number,
sampleY: number
): RGBA {
// get pixelArray from imagedata object
let data = imagedata.data;
// image data is array of RGBA values (4 bytes). Pixel sample offset is Y value * width of image, plus X pixels across.
let i = (sampleY * imagedata.width + sampleX) * 4;
// get RGBA values
let result: RGBA = {
r: data[i],
g: data[i + 1],
b: data[i + 2],
a: data[i + 3],
};
return result;
}
simpleImageResize(
// eslint-disable-next-line no-undef
img: CanvasImageSource,
canvas: HTMLCanvasElement
): Promise<ImageData> {
// resize the given image onto the destination canvas
return new Promise<ImageData>((resolve, reject) => {
let context = canvas.getContext("2d");
if (context != null) {
context.drawImage(
img,
0,
0,
img.width as number,
img.height as number,
0,
0,
canvas.width,
canvas.height
);
setTimeout(() => {
if (context != null) {
let imgData = context.getImageData(
0,
0,
canvas.width,
canvas.height
);
resolve(imgData);
}
}, 500);
} else {
reject();
}
});
}
getImageData(imgSrcData: string): Promise<ImageData> {
return new Promise<ImageData>((resolve, reject) => {
let canvas = document.createElement("canvas");
let context = canvas.getContext("2d");
if (canvas != null) {
const img = new Image();
img.onload = () => {
canvas.width = img.width;
canvas.height = img.height;
context?.drawImage(img, 0, 0, img.width, img.height);
setTimeout(() => {
if (context != null) {
let imgData = context.getImageData(
0,
0,
canvas.width,
canvas.height
);
resolve(imgData);
}
}, 500);
return;
};
img.src = imgSrcData;
} else {
this.logger.warn("getImageData: Canvas is null");
reject();
}
});
}
transformAndArchive(
platSpec: PlatformSpec,
bundleSpec: BundleSpec,
fileSpec: ImageSpec,
srcImage: ImageDataSet,
fromCentre: boolean
): Promise<any> {
return new Promise<any>((resolve, reject) => {
// create new canvas for our target image render
let canvas = document.createElement("canvas");
let newWidth = fileSpec.width;
let newHeight = fileSpec.height;
if (fileSpec.scale != null) {
newWidth = fileSpec.width * fileSpec.scale;
newHeight = fileSpec.height * fileSpec.scale;
}
// TODO: for fixed aspect ratio, work out largest width or height
// for resize from centre, sample corner value and fill canvas, then centre resized image in canvas
canvas.width = newWidth;
canvas.height = newHeight;
let isPortrait = true;
if (bundleSpec.maintainAspectRatio) {
let aspectRatio = srcImage.imgData.width / srcImage.imgData.height;
if (fileSpec.width > fileSpec.height) {
// original height / original width * new width = new height
// landscape aspect
let maxDimension = fileSpec.height;
canvas.width = maxDimension;
canvas.height =
(srcImage.imgData.height / srcImage.imgData.width) * maxDimension;
} else {
// portrait aspect
// original width / original height * new height = new width
isPortrait = true;
let maxDimension = fileSpec.width;
canvas.width =
(srcImage.imgData.width / srcImage.imgData.height) * maxDimension;
canvas.height = maxDimension;
}
}
const img = new Image();
img.onload = () => {
// if image has no alpha, fill with background color based on sample
this.logger.info(
`resizing ${img.width} x ${img.height} to ${fileSpec.width} x ${fileSpec.height} canvas: ${canvas.width}x${canvas.height}`
);
// FIXME: no idea what this calculation should be (crop from center)
if (this.isCropSplashFromCentreMode && bundleSpec.maintainAspectRatio) {
// copy source image to a canvas, cropped from centre
let destx = -(img.width / 2) + fileSpec.width / 2;
let desty = -(img.height / 2) + fileSpec.height / 2;
let srcCtx = canvas.getContext("2d");
if (srcCtx != null) {
srcCtx.drawImage(img, destx, desty);
this.logger.info(
`crop from centre: fileSpec ${fileSpec.width}x${fileSpec.height} ': Src img: ${srcImage.imgData.width}x${srcImage.imgData.height} , Src Canvas ${canvas.width} x ${canvas.height} destX:${destx}, destY:${desty}`
);
this.addImageToBundle(canvas, fileSpec, platSpec, bundleSpec);
return resolve(true);
} else {
reject("Invalid context");
}
} else {
// resize to fit
this.pica
.resize(img, canvas, {
unsharpAmount: 160,
unsharpRadius: 0.6,
unsharpThreshold: 1,
// alpha: bundleSpec.useAlpha,
})
.then(
() => {
if (bundleSpec.maintainAspectRatio) {
let destCanvas = document.createElement("canvas");
destCanvas.width = fileSpec.width;
destCanvas.height = fileSpec.height;
let sample: RGBA = this.splashColourSample;
let ctx = destCanvas.getContext("2d");
if (ctx != null) {
ctx.fillStyle =
"rgb(" + sample.r + "," + sample.g + "," + sample.b + ")";
ctx.fillRect(0, 0, destCanvas.width, destCanvas.height);
// centre destination coord to draw image to
let destx = (destCanvas.width - canvas.width) / 2;
let desty = (destCanvas.height - canvas.height) / 2;
if (destx < 0) destx = 0;
if (desty < 0) desty = 0;
this.logger.info(
`fileSpec ${fileSpec.width}x${fileSpec.height} : Src img: ${srcImage.imgData.width}x${srcImage.imgData.height} , Src Canvas ${canvas.width} x ${canvas.height} Dest: ${destCanvas.width} x ${destCanvas.height} destX:${destx}, destY:${desty}`
);
ctx.drawImage(canvas, destx, desty);
canvas = destCanvas;
}
}
this.addImageToBundle(canvas, fileSpec, platSpec, bundleSpec);
return resolve(true);
},
() => {
return reject("Resize failed");
}
);
}
};
// trigger image load for source image, which then triggers processing onload
img.src = srcImage.imgSrcDataBase64;
});
}
addImageToBundle(
canvas: HTMLCanvasElement,
fileSpec: ImageSpec,
platSpec: PlatformSpec,
bundleSpec: BundleSpec
): void {
this.logger.info("resize done, adding to bundle.");
// take generated image and archive it in our bundle
let imgData = canvas.toDataURL("image/png");
fileSpec.imgData = imgData;
if (this.zipArchive != null) {
let imgFolder: JSZip | null | undefined =
this.zipArchive.folder("bundle");
imgFolder = imgFolder?.folder(platSpec.path) ?? null;
if (imgFolder != null) {
if (bundleSpec.path) imgFolder = imgFolder.folder(bundleSpec.path);
if (fileSpec.path) imgFolder = imgFolder?.folder(fileSpec.path);
let imgDataBase64 = imgData.replace(
/^data:image\/(png|jpg);base64,/,
""
);
// add file to zip folder
imgFolder?.file(bundleSpec.prefix + fileSpec.fileName, imgDataBase64, {
base64: true,
});
} else {
this.logger.warn("Bundle image folder is null");
}
} else {
this.logger.warn("Zip archive is null");
}
}
downloadZip() : void {
this.zipArchive?.generateAsync({ type: "blob" }).then((content) => {
// see FileSaver.js
saveAs(content, "ApeToolsGeneratedAssets.zip");
});
}
appendToManifest(
generatedManifest: any,
bundleSpec: BundleSpec,
itemSpec: ImageSpec
) : void {
generatedManifest.images.push({
idiom: itemSpec.idiom,
size: itemSpec.width + "x" + itemSpec.height,
scale: itemSpec.scale + "x",
filename: bundleSpec.prefix + itemSpec.fileName,
});
}
getSourceImageForProcessing(
srcItemCategory: string,
srcItemBlob: any
): Promise<ImageDataSet> {
let promise = new Promise<any>((resolve, reject) => {
if (srcItemCategory === "icon" && this.iconSrcData != null) {
resolve({
imgSrcDataBase64: this.iconSrcData,
imgData: this.iconImgData,
});
return;
}
if (srcItemCategory === "splash" && this.splashSrcData != null) {
resolve({
imgSrcDataBase64: this.splashSrcData,
imgData: this.splashImgData,
});
return;
}
// not got cached values, read image data blob and process
let reader = new FileReader();
reader.addEventListener(
"load",
() => {
let srcData = reader.result as string;
// use setTimeout to allow image time to complete load, otherwise reading image data doesn't always work
setTimeout(() => {
return this.getImageData(srcData).then((imgData) => {
// cache values
if (srcItemCategory === "icon") {
this.iconSrcData = srcData;
this.iconImgData = imgData;
}
if (srcItemCategory === "splash") {
this.splashSrcData = srcData;
this.splashImgData = imgData;
}
resolve({
imgSrcDataBase64: srcData,
imgData: imgData,
});
return;
});
}, 500);
},
false
);
// fire above load event for file
reader.readAsDataURL(srcItemBlob);
});
return promise;
}
async process() : Promise<void> {
this.msg = "Processing..";
this.logger.info("Begin processing..");
this.isProcessing = true;
this.isArchiveReady = false;
let numTasks = 0;
// compute total number of tasks to perform and compile manifests if required
for (let platformSpec of this.allPlatforms) {
if (platformSpec.includeInBundle) {
for (let bundleSpec of platformSpec.bundleSpecs) {
let generatedManifest: any = null;
// optionally generate a manifest file
if (bundleSpec.manifest != null && platformSpec.id === "ios") {
generatedManifest = {
images: [],
info: { version: 1, author: "apetools.webprofusion.com" },
};
}
for (let itemSpec of bundleSpec.imageSet) {
if (
(bundleSpec.category === "icon" && this.selectedIconFile) ||
(bundleSpec.category === "splash" && this.selectedSplashFile)
) {
if (generatedManifest)
this.appendToManifest(generatedManifest, bundleSpec, itemSpec);
numTasks++;
}
}
if (bundleSpec.manifest != null) {
bundleSpec.manifest = generatedManifest;
this.logger.info(JSON.stringify(bundleSpec.manifest));
}
}
}
}
this.totalTasks = numTasks;
this.logger.info(`Generating ${this.totalTasks} assets for bundle..`);
if (this.totalTasks > 0) {
// process the images for each platform
this.processingProgress = 0;
// cache image data
if (this.selectedIconFile)
await this.getSourceImageForProcessing("icon", this.selectedIconFile);
if (this.selectedSplashFile) {
await this.getSourceImageForProcessing(
"splash",
this.selectedSplashFile
);
// sample splash corner colour for canvas fill
// this.splashColourSample = this.samplePixelValue(this.splashImgData, 0, 0);
}
for (let platformSpec of this.allPlatforms) {
if (platformSpec.includeInBundle) {
for (let bundleSpec of platformSpec.bundleSpecs) {
for (let itemSpec of bundleSpec.imageSet) {
let srcItem: any = null;
let fromCentre = false;
if (bundleSpec.category === "icon") {
srcItem = this.selectedIconFile;
}
if (bundleSpec.category === "splash") {
srcItem = this.selectedSplashFile;
}
if (srcItem != null) {
setTimeout(async () => {
await this.getSourceImageForProcessing(
bundleSpec.category,
srcItem
).then((imgBundle: ImageDataSet) => {
this.transformAndArchive(
platformSpec,
bundleSpec,
itemSpec,
imgBundle,
fromCentre
).then(() => {
this.itemsProcessed++;
this.processingProgress = Math.round(
(this.itemsProcessed / this.totalTasks) * 100
);
this.msg = "Processing " + this.processingProgress + "%";
this.logger.info(this.msg);
if (this.processingProgress === 100) {
this.isArchiveReady = true;
this.isProcessing = false;
}
// ask vue to re-render
this.$forceUpdate();
});
});
}, 1000);
}
}
// optionally include generated manifest in archiver
if (bundleSpec.manifest) {
// append manifest to archive
let imgFolder = this.zipArchive?.folder("bundle");
if (imgFolder != null) {
imgFolder = imgFolder.folder(platformSpec.path);
imgFolder = imgFolder?.folder(bundleSpec.path as string);
imgFolder?.file(
"Contents.json",
JSON.stringify(bundleSpec.manifest, null, "\t")
);
}
}
}
}
}
this.logger.info(`Bundle generation completed.`);
} else {
this.logger.info("Nothing to do..");
this.isProcessing = false;
alert(
"The apes are bored, there's nothing to do. \nMaybe set an icon or splashscreen and check a few platforms?"
);
}
}
}
</script>
<style scoped>
</style>