forked from rsky/php-epeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepeg.c
1563 lines (1359 loc) · 43.3 KB
/
epeg.c
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The Epeg PHP extension
*
* Copyright (c) 2006-2010 Ryusuke SEKIYAMA. All rights reserved.
*
* 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.
*
* @package php-epeg
* @author Ryusuke SEKIYAMA <[email protected]>
* @copyright 2006-2010 Ryusuke SEKIYAMA
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
#include "php_epeg.h"
/* {{{ globals */
static int le_epeg;
static zend_class_entry *ce_Epeg = NULL;
static zend_object_handlers _php_epeg_object_handlers;
/* }}} */
/* {{{ module function prototypes */
static PHP_MINIT_FUNCTION(epeg);
static PHP_MINFO_FUNCTION(epeg);
/* }}} */
/* {{{ PHP function prototypes */
static PHP_FUNCTION(epeg_thumbnail_create);
static PHP_FUNCTION(epeg_open);
static PHP_FUNCTION(epeg_file_open);
static PHP_FUNCTION(epeg_memory_open);
static PHP_FUNCTION(epeg_size_get);
static PHP_FUNCTION(epeg_decode_size_set);
#ifdef PHP_EPEG_ENABLE_DECODE_BOUNDS_SET
static PHP_FUNCTION(epeg_decode_bounds_set);
#endif
static PHP_FUNCTION(epeg_decode_colorspace_set);
static PHP_FUNCTION(epeg_comment_get);
static PHP_FUNCTION(epeg_comment_set);
static PHP_FUNCTION(epeg_quality_set);
static PHP_FUNCTION(epeg_thumbnail_comments_get);
static PHP_FUNCTION(epeg_thumbnail_comments_enable);
static PHP_FUNCTION(epeg_encode);
static PHP_FUNCTION(epeg_trim);
static PHP_FUNCTION(epeg_close);
static PHP_METHOD(Epeg, openFile);
static PHP_METHOD(Epeg, openBuffer);
/* }}} */
/* {{{ internal function prototypes */
static inline int
round_to_i(double num)
{
return (int)lround(num);
}
static php_epeg_t *
php_epeg_file_open(char *file TSRMLS_DC);
static php_epeg_t *
php_epeg_memory_open(char *data, int data_len TSRMLS_DC);
static void
php_epeg_open_wrapper(INTERNAL_FUNCTION_PARAMETERS, int mode);
static void
php_epeg_set_retval(unsigned char *buf, int buf_len,
char *file, int file_len, zval *retval TSRMLS_DC);
static void
php_epeg_encode_error(int errcode TSRMLS_DC);
static void
php_epeg_trim_error(int errcode TSRMLS_DC);
static void
php_epeg_free_resource(zend_rsrc_list_entry *rsrc TSRMLS_DC);
static void
php_epeg_free(php_epeg_t *im);
static zend_object_value
php_epeg_object_new(zend_class_entry *ce TSRMLS_DC);
static void
php_epeg_free_object_storage(void *object TSRMLS_DC);
static int
php_epeg_calc_thumb_size(
int src_width, int src_height,
int max_width, int max_height,
int *dst_width, int *dst_height);
static void
php_epeg_reset(php_epeg_t *im);
/* }}} */
/* {{{ function shortcurs */
/* {{{ macro for parsing parameters and fetching resource */
/* fetch (php_epeg_t *) from resource */
#define FETCH_IMAGE_FROM_RESOURCE(im, zim) \
ZEND_FETCH_RESOURCE((im), php_epeg_t *, &(zim), -1, "epeg", le_epeg)
/* free any type of resource */
#define FREE_RESOURCE(resource) zend_list_delete(Z_RESVAL_P(resource))
/* fetch (php_epeg_t *) from object storage */
#define FETCH_IMAGE_FROM_OBJECT(im, obj) { \
php_epeg_object *intern; \
intern = (php_epeg_object *)zend_object_store_get_object((obj) TSRMLS_CC); \
im = intern->ptr; \
}
/* expect a parameter */
#define PHP_EPEG_PARSE_PARAMETER() \
if (obj) { \
if (ZEND_NUM_ARGS() != 0) { \
WRONG_PARAM_COUNT; \
} \
FETCH_IMAGE_FROM_OBJECT(im, obj); \
} else { \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zim) == FAILURE) { \
return; \
} \
FETCH_IMAGE_FROM_RESOURCE(im, zim); \
}
/* expect many parameters */
#define PHP_EPEG_PARSE_PARAMETERS(fmt, ...) \
if (obj) { \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, fmt, __VA_ARGS__) == FAILURE) { \
return; \
} \
FETCH_IMAGE_FROM_OBJECT(im, obj); \
} else { \
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r" fmt, &zim, __VA_ARGS__) == FAILURE) { \
return; \
} \
FETCH_IMAGE_FROM_RESOURCE(im, zim); \
}
/* }}} */
/* {{{ argument informations */
#if PHP_VERSION_ID < 50300
#define ARG_INFO_STATIC static
#else
#define ARG_INFO_STATIC
#endif
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg__epeg, 0)
ZEND_ARG_INFO(0, image)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg__output, 0)
ZEND_ARG_INFO(0, image)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg__output_m, 0)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO_EX(arginfo_epeg_thumbnail_create, 0, 0, 4)
ZEND_ARG_INFO(0, in_file)
ZEND_ARG_INFO(0, out_file)
ZEND_ARG_INFO(0, max_width)
ZEND_ARG_INFO(0, max_height)
ZEND_ARG_INFO(0, quality)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO_EX(arginfo_epeg_open, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, is_data)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_file_open, 0)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_memory_open, 0)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_decode_size_set, 0)
ZEND_ARG_INFO(0, image)
ZEND_ARG_INFO(0, width)
ZEND_ARG_INFO(0, height)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_decode_size_set_m, 0)
ZEND_ARG_INFO(0, width)
ZEND_ARG_INFO(0, height)
ZEND_END_ARG_INFO()
#ifdef PHP_EPEG_ENABLE_DECODE_BOUNDS_SET
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_decode_bounds_set, 0)
ZEND_ARG_INFO(0, image)
ZEND_ARG_INFO(0, x)
ZEND_ARG_INFO(0, y)
ZEND_ARG_INFO(0, width)
ZEND_ARG_INFO(0, height)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_decode_bounds_set_m, 0)
ZEND_ARG_INFO(0, x)
ZEND_ARG_INFO(0, y)
ZEND_ARG_INFO(0, width)
ZEND_ARG_INFO(0, height)
ZEND_END_ARG_INFO()
#endif
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_decode_colorspace_set, 0)
ZEND_ARG_INFO(0, image)
ZEND_ARG_INFO(0, colorspace)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_decode_colorspace_set_m, 0)
ZEND_ARG_INFO(0, colorspace)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_comment_set, 0)
ZEND_ARG_INFO(0, image)
ZEND_ARG_INFO(0, comment)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_comment_set_m, 0)
ZEND_ARG_INFO(0, comment)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_quality_set, 0)
ZEND_ARG_INFO(0, image)
ZEND_ARG_INFO(0, quality)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO(arginfo_epeg_quality_set_m, 0)
ZEND_ARG_INFO(0, quality)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO_EX(arginfo_epeg_thumbnail_comments_enable, 0, 0, 1)
ZEND_ARG_INFO(0, image)
ZEND_ARG_INFO(0, onoff)
ZEND_END_ARG_INFO()
ARG_INFO_STATIC
ZEND_BEGIN_ARG_INFO_EX(arginfo_epeg_thumbnail_comments_enable_m, 0, 0, 0)
ZEND_ARG_INFO(0, onoff)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ Class definitions */
/* {{{ methods */
static zend_function_entry epeg_methods[] = {
PHP_ME(Epeg, openFile, arginfo_epeg_file_open, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(Epeg, openBuffer, arginfo_epeg_memory_open, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME_MAPPING(__construct, epeg_open, arginfo_epeg_open, ZEND_ACC_CTOR | ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(getSize, epeg_size_get, NULL, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(setDecodeSize, epeg_decode_size_set, arginfo_epeg_decode_size_set_m, ZEND_ACC_PUBLIC)
#ifdef PHP_EPEG_ENABLE_DECODE_BOUNDS_SET
PHP_ME_MAPPING(setDecodeBounds, epeg_decode_bounds_set, arginfo_epeg_decode_bounds_set_m, ZEND_ACC_PUBLIC)
#endif
PHP_ME_MAPPING(setDecodeColorSpace, epeg_decode_colorspace_set, arginfo_epeg_decode_colorspace_set_m, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(getComment, epeg_comment_get, NULL, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(setComment, epeg_comment_set, arginfo_epeg_comment_set_m, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(setQuality, epeg_quality_set, arginfo_epeg_quality_set_m, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(getThumbnailComments, epeg_thumbnail_comments_get, NULL, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(enableThumbnailComments, epeg_thumbnail_comments_enable, arginfo_epeg_thumbnail_comments_enable_m, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(encode, epeg_encode, arginfo_epeg__output_m, ZEND_ACC_PUBLIC)
PHP_ME_MAPPING(trim, epeg_trim, arginfo_epeg__output_m, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
};
/* }}} */
/* }}} Class definitions */
/* {{{ epeg_functions[] */
static zend_function_entry epeg_functions[] = {
PHP_FE(epeg_thumbnail_create, arginfo_epeg_thumbnail_create)
PHP_FE(epeg_open, arginfo_epeg_open)
PHP_FE(epeg_file_open, arginfo_epeg_file_open)
PHP_FE(epeg_memory_open, arginfo_epeg_memory_open)
PHP_FE(epeg_size_get, arginfo_epeg__epeg)
PHP_FE(epeg_decode_size_set, arginfo_epeg_decode_size_set)
#ifdef PHP_EPEG_ENABLE_DECODE_BOUNDS_SET
PHP_FE(epeg_decode_bounds_set, arginfo_epeg_decode_bounds_set)
#endif
PHP_FE(epeg_decode_colorspace_set, arginfo_epeg_decode_colorspace_set)
PHP_FE(epeg_comment_get, arginfo_epeg__epeg)
PHP_FE(epeg_comment_set, arginfo_epeg_comment_set)
PHP_FE(epeg_quality_set, arginfo_epeg_quality_set)
PHP_FE(epeg_thumbnail_comments_get, arginfo_epeg__epeg)
PHP_FE(epeg_thumbnail_comments_enable, arginfo_epeg_thumbnail_comments_enable)
PHP_FE(epeg_encode, arginfo_epeg__output)
PHP_FE(epeg_trim, arginfo_epeg__output)
PHP_FE(epeg_close, arginfo_epeg__epeg)
{ NULL, NULL, NULL }
};
/* }}} */
/* {{{ epeg_module_entry */
zend_module_entry epeg_module_entry = {
STANDARD_MODULE_HEADER,
"epeg",
epeg_functions,
PHP_MINIT(epeg),
NULL,
NULL,
NULL,
PHP_MINFO(epeg),
PHP_EPEG_MODULE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_EPEG
ZEND_GET_MODULE(epeg)
#endif
#define PHP_EPEG_REGISTER_CONSTANT(name) \
REGISTER_LONG_CONSTANT(#name, (long)name, CONST_PERSISTENT | CONST_CS)
#define PHP_EPEG_REGISTER_CLASS_CONSTANT(name) \
zend_declare_class_constant_long(ce_Epeg, #name, strlen(#name), (long)EPEG_##name TSRMLS_CC)
/* {{{ PHP_MINIT_FUNCTION */
static PHP_MINIT_FUNCTION(epeg)
{
zend_class_entry ce;
PHP_EPEG_REGISTER_CONSTANT(EPEG_GRAY8);
PHP_EPEG_REGISTER_CONSTANT(EPEG_YUV8);
PHP_EPEG_REGISTER_CONSTANT(EPEG_RGB8);
PHP_EPEG_REGISTER_CONSTANT(EPEG_BGR8);
PHP_EPEG_REGISTER_CONSTANT(EPEG_RGBA8);
PHP_EPEG_REGISTER_CONSTANT(EPEG_BGRA8);
PHP_EPEG_REGISTER_CONSTANT(EPEG_ARGB32);
PHP_EPEG_REGISTER_CONSTANT(EPEG_CMYK);
le_epeg = zend_register_list_destructors_ex(php_epeg_free_resource, NULL, "epeg", module_number);
INIT_CLASS_ENTRY(ce, "Epeg", epeg_methods);
ce_Epeg = zend_register_internal_class(&ce TSRMLS_CC);
if (!ce_Epeg) {
return FAILURE;
}
ce_Epeg->create_object = php_epeg_object_new;
memcpy(&_php_epeg_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
_php_epeg_object_handlers.clone_obj = NULL;
PHP_EPEG_REGISTER_CLASS_CONSTANT(GRAY8);
PHP_EPEG_REGISTER_CLASS_CONSTANT(YUV8);
PHP_EPEG_REGISTER_CLASS_CONSTANT(RGB8);
PHP_EPEG_REGISTER_CLASS_CONSTANT(BGR8);
PHP_EPEG_REGISTER_CLASS_CONSTANT(RGBA8);
PHP_EPEG_REGISTER_CLASS_CONSTANT(BGRA8);
PHP_EPEG_REGISTER_CLASS_CONSTANT(ARGB32);
PHP_EPEG_REGISTER_CLASS_CONSTANT(CMYK);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(epeg)
{
php_info_print_table_start();
php_info_print_table_row(2, "Epeg Support", "enabled");
php_info_print_table_row(2, "Module Version", PHP_EPEG_MODULE_VERSION);
#ifdef PHP_EPEG_VERSION_STRING
php_info_print_table_row(2, "Epeg Library Version", PHP_EPEG_VERSION_STRING);
#else
php_info_print_table_row(2, "Epeg Library Version", "unknown");
#endif
php_info_print_table_end();
}
/* }}} */
/* {{{ php_epeg_file_open */
static php_epeg_t *
php_epeg_file_open(char *file TSRMLS_DC)
{
php_stream *sth = NULL;
char *data = NULL;
int data_len = 0;
php_epeg_t *im = NULL;
/* open stream for reading */
sth = php_stream_open_wrapper(file, "rb",
ENFORCE_SAFE_MODE | IGNORE_PATH | REPORT_ERRORS, NULL);
if (!sth) {
return NULL;
}
/* copy image data to the buffer */
data_len = php_stream_copy_to_mem(sth, &data, PHP_STREAM_COPY_ALL, 0);
/* close the input stream */
php_stream_close(sth);
if (data_len == 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot read image data");
return NULL;
}
/* open the JPEG image stored in the buffer */
im = php_epeg_memory_open(data, data_len TSRMLS_CC);
/* free the buffer */
efree(data);
/* return Epeg image handle (or NULL) */
return im;
}
/* }}} */
/* {{{ php_epeg_memory_open */
static php_epeg_t *
php_epeg_memory_open(char *data, int data_len TSRMLS_DC)
{
php_epeg_t *im = NULL;
/* initialize */
im = (php_epeg_t *)ecalloc(1, sizeof(php_epeg_t));
im->data = (unsigned char *)estrndup(data, data_len);
im->size = data_len;
im->quality = -1;
/* open the JPEG image stored in the buffer */
im->ptr = epeg_memory_open(im->data, im->size);
if (im->ptr == NULL) {
php_epeg_free(im);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a valid JPEG data");
return NULL;
}
/* get image size */
epeg_size_get(im->ptr, &(im->width), &(im->height));
/* return Epeg image handle */
return im;
}
/* }}} */
/* {{{ php_epeg_open_wrapper */
static void
php_epeg_open_wrapper(INTERNAL_FUNCTION_PARAMETERS, int mode)
{
/* declaration of the arguments */
char *str = NULL;
int str_len = 0;
/* declaration of the local variables */
php_epeg_t *im = NULL;
/* parse the arguments and open the JPEG image */
if (mode & EO_FROM_BUFFER) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &str, &str_len) == FAILURE) {
RETURN_FALSE;
}
im = php_epeg_memory_open(str, str_len TSRMLS_CC);
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
RETURN_FALSE;
}
im = php_epeg_file_open(str TSRMLS_CC);
}
if (im == NULL) {
RETURN_FALSE;
}
if (mode & EO_TO_OBJECT) {
php_epeg_object *intern;
Z_TYPE_P(return_value) = IS_OBJECT;
object_init_ex(return_value, ce_Epeg);
intern = (php_epeg_object *)zend_object_store_get_object(return_value TSRMLS_CC);
intern->ptr = im;
} else {
ZEND_REGISTER_RESOURCE(return_value, im, le_epeg);
}
}
/* }}} */
/* {{{ php_epeg_set_retval */
static void
php_epeg_set_retval(unsigned char *buf, int buf_len,
char *file, int file_len, zval *retval TSRMLS_DC)
{
/* if the output is an empty string, the content of the thubmnail is returned */
if (file_len == 0) {
/* set return value to the content of the thumbnail */
ZVAL_STRINGL(retval, (char *)buf, buf_len, 1);
} else {
/* open stream for writing */
php_stream *sth = NULL;
sth = php_stream_open_wrapper(file, "wb",
ENFORCE_SAFE_MODE | IGNORE_PATH | REPORT_ERRORS, NULL);
if (!sth) {
/* set return value to false */
ZVAL_FALSE(retval);
} else {
int expected_written_len = buf_len;
if (expected_written_len != php_stream_write(sth, (char *)buf, buf_len)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write image data to stream");
/* set return value to false */
ZVAL_FALSE(retval);
} else {
/* set return value to true */
ZVAL_TRUE(retval);
}
/* close the output stream */
php_stream_close(sth);
}
}
}
/* }}} */
/* {{{ php_epeg_encode_error */
static void
php_epeg_encode_error(int errcode TSRMLS_DC)
{
switch (errcode) {
case 3:
case 4:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to decode image");
break;
case 1:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to scale image");
break;
case 2:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to encode image");
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error");
}
}
/* }}} */
/* {{{ php_epeg_trim_error */
static void
php_epeg_trim_error(int errcode TSRMLS_DC)
{
switch (errcode) {
case 1:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to trim image");
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error");
}
}
/* }}} */
/* {{{ php_epeg_free_resource */
static void
php_epeg_free_resource(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_epeg_free((php_epeg_t *)(rsrc->ptr));
}
/* }}} */
/* {{{ php_epeg_free */
static void
php_epeg_free(php_epeg_t *im)
{
if (im->ptr != NULL) {
epeg_close(im->ptr);
}
if (im->data != NULL) {
efree(im->data);
}
efree(im);
}
/* }}} */
/* {{{ php_epeg_object_new */
static zend_object_value
php_epeg_object_new(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
php_epeg_object *intern;
intern = (php_epeg_object *)ecalloc(1, sizeof(php_epeg_object));
zend_object_std_init(&intern->std, ce TSRMLS_CC);
#if PHP_API_VERSION < 20100412
zend_hash_copy(intern->std.properties, &ce->default_properties,
(copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval *));
#else
object_properties_init(&intern->std, ce);
#endif
retval.handle = zend_objects_store_put(intern,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
(zend_objects_free_object_storage_t)php_epeg_free_object_storage,
NULL TSRMLS_CC);
retval.handlers = &_php_epeg_object_handlers;
return retval;
}
/* }}} */
/* {{{ php_epeg_free_object_storage */
static void
php_epeg_free_object_storage(void *object TSRMLS_DC)
{
php_epeg_object *intern = (php_epeg_object *)object;
php_epeg_free(intern->ptr);
zend_object_std_dtor(&intern->std TSRMLS_CC);
efree(object);
}
/* }}} */
/* {{{ php_epeg_calc_thumb_size */
static int
php_epeg_calc_thumb_size(
int src_width, int src_height,
int max_width, int max_height,
int *dst_width, int *dst_height)
{
/* check */
if (src_width < 1 || max_width < 0) {
return FAILURE;
} else if (max_width == 0) {
max_width = src_width;
}
if (src_height < 1 || max_height < 0) {
return FAILURE;
} else if (max_height == 0) {
max_height = src_height;
}
if (max_width >= src_width && max_height >= src_height) {
*dst_width = src_width;
*dst_height = src_height;
} else {
double width = (double)src_width;
double height = (double)src_height;
double to_width = (double)max_width;
double to_height = (double)max_height;
double xy_ratio = width / height;
/* calculate */
if (width <= to_width) {
*dst_width = round_to_i(to_height * xy_ratio);
*dst_height = round_to_i(to_height);
} else if (height <= to_height) {
*dst_width = round_to_i(to_width);
*dst_height = round_to_i(to_width / xy_ratio);
} else if (xy_ratio > to_width / to_height) {
*dst_width = round_to_i(to_width);
*dst_height = round_to_i(to_width / xy_ratio);
} else {
*dst_width = round_to_i(to_height * xy_ratio);
*dst_height = round_to_i(to_height);
}
}
return SUCCESS;
}
/* }}} */
/* {{{ php_epeg_reset */
static void
php_epeg_reset(php_epeg_t *im)
{
epeg_close(im->ptr);
im->ptr = epeg_memory_open(im->data, im->size);
if (im->quality != -1) {
epeg_quality_set(im->ptr, im->quality);
}
}
/* }}} */
/* {{{ proto mixed epeg_thumbnail_create(string in_file, string out_file, int max_width, int max_height[, int quality]) */
/**
* bool|string epeg_thumbnail(string in_file, string out_file, int max_width, int max_height[, int quality])
*
* Create thumbnail using the Epeg library.
* This function can be used for only JPEG image.
*
* @param string $in_file The pathname or the URL of the source image.
* @param string $out_file The pathname or the URL of the thumbnail.
* @param int $max_width The maximum width of the thumbnail.
* The value must be greater than 0.
* @param int $max_height The maximum height of the thumbnail.
* The value must be greater than 0.
* @param int $quality The quality of the thumbnail. (optional)
* The value must be greater than or equal to 0
* and must be less than or equal to 100.
* The default is 75.
* @return mixed False is returned if failed to create the thumbnail.
* True is returned if succeeded in creating and writing the thumbnail.
* If $out_file is an empty string and succeeded in creating
* the thumbnail, the content of the thumbnail is returned.
*/
static PHP_FUNCTION(epeg_thumbnail_create)
{
/* declaration of the arguments */
char *in_file = NULL;
int in_file_len = 0;
char *out_file = NULL;
int out_file_len = 0;
long max_width = 0;
long max_height = 0;
long quality = 75;
/* declaration of the local variables */
php_epeg_t *im = NULL;
php_stream *sth = NULL;
char *in_buf;
unsigned char *out_buf;
int in_buf_len, out_buf_len;
zend_bool out_buf_free_stdc = 0; /* use free() */
zend_bool out_buf_free_zend = 0; /* use efree() */
/* parse the arguments */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssll|l",
&in_file, &in_file_len, &out_file, &out_file_len,
&max_width, &max_height, &quality) == FAILURE)
{
RETURN_FALSE;
}
/* check output size */
if (max_width <= 0 || max_height <= 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Invalid image dimensions '%ldx%ld'", max_width, max_height);
RETURN_FALSE;
}
/* check quality */
if (quality < 0 || quality > 100) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid quality '%ld'", quality);
RETURN_FALSE;
}
/* open stream for reading */
sth = php_stream_open_wrapper(in_file, "rb",
ENFORCE_SAFE_MODE | IGNORE_PATH | REPORT_ERRORS, NULL);
if (!sth) {
RETURN_FALSE;
}
/* copy image data to the buffer */
in_buf_len = php_stream_copy_to_mem(sth, &in_buf, PHP_STREAM_COPY_ALL, 0);
/* close the input stream */
php_stream_close(sth);
if (in_buf_len == 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot read image data");
RETURN_FALSE;
}
/* open the JPEG image stored in the buffer */
im = php_epeg_memory_open(in_buf, in_buf_len TSRMLS_CC);
if (im == NULL) {
/* free the input buffer */
efree(in_buf);
RETURN_FALSE;
}
/* get image size and check whether to do resampling */
if (im->width > max_width || im->height > max_height) {
unsigned char *tmp_buf;
int result, tw, th, tmp_buf_len;
/* free the input buffer */
efree(in_buf);
/* calculate size */
(void)php_epeg_calc_thumb_size(im->width, im->height, max_width, max_height, &tw, &th);
/* set the size of thumbnail */
epeg_decode_size_set(im->ptr, tw, th);
/* set output to the buffer */
epeg_memory_output_set(im->ptr, &tmp_buf, &tmp_buf_len);
/* set quality */
epeg_quality_set(im->ptr, (int)quality);
/* encode the image and save to the buffer */
result = epeg_encode(im->ptr);
/* close the Epeg image handle */
php_epeg_free(im);
if (result != 0) {
/* free the temporary buffer */
if (tmp_buf) {
free(tmp_buf);
}
/* raise error by the result */
php_epeg_encode_error(result TSRMLS_CC);
RETURN_FALSE;
}
/* set the result */
out_buf = tmp_buf;
out_buf_len = tmp_buf_len;
out_buf_free_stdc = 1;
} else {
unsigned char *in_ptr, *in_end, *out_ptr;
/* close the Epeg image handle */
php_epeg_free(im);
/* cast the input data to unsigned char */
in_ptr = (unsigned char *)in_buf;
in_end = in_ptr + in_buf_len;
/* check for SOI (Start Of Image Segment) marker */
if (*in_ptr != 0xFF && *(in_ptr + 1) != 0xD8) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a valid JPEG data");
efree(in_buf);
RETURN_FALSE;
}
/* allocate memory for the output buffer */
out_buf = (unsigned char *)emalloc((size_t)in_buf_len);
out_ptr = out_buf;
out_buf_free_zend = 1;
/* write SOI marker */
*out_ptr++ = 0xFF;
*out_ptr++ = 0xD8;
in_ptr += 2;
/* search and skip extra markers */
while (in_ptr < in_end) {
unsigned char marker;
size_t field_len;
if (*in_ptr == 0xFF) {
/* skip padding */
while (*in_ptr == 0xFF && in_ptr < in_end) {
in_ptr++;
}
marker = *in_ptr++;
field_len = (((size_t)*in_ptr) << 8) | (size_t)*(in_ptr + 1);
if (in_ptr + field_len > in_end) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid data structure");
efree(out_buf);
RETURN_FALSE;
}
/* following markers are dropped :
* RES: 0xFF [0x02-0xBF] (reserved)
* APP[1-15]: 0xFF [0xE1-0xEF] (application markers, APP0 (0xE0) is JFIF (kept), APP1 is EXIF)
* JPEG[0-13]: 0xFF [0xF0-0xFD] (reserved for expansion of JPEG)
* COM: 0xFF 0xFE (comment)
*/
if ((marker > 0x01 && marker < 0xC0) || (marker > 0xE0 && marker < 0xFF)) {
in_ptr += field_len;
continue;
} else {
*out_ptr++ = 0xFF;
*out_ptr++ = marker;
(void)memcpy(out_ptr, in_ptr, field_len);
in_ptr += field_len;
out_ptr += field_len;
/* break if SOS (Start Of Scan) marker found */
if (marker == 0xDA) {
break;
}
}
} else {
*out_ptr++ = *in_ptr++;
}
}
/* copy leftovers */
if (in_ptr < in_end) {
size_t rest_len = (size_t)(in_end - in_ptr);
(void)memcpy(out_ptr, in_ptr, rest_len);
out_ptr += rest_len;
}
/* free the input buffer */
efree(in_buf);
/* set the result */
out_buf_len = (int)(out_ptr - out_buf);
}
/* set return value */
php_epeg_set_retval(out_buf, out_buf_len, out_file, out_file_len, return_value TSRMLS_CC);
/* free the output buffer */
if (out_buf_free_stdc) {
free(out_buf);
} else if (out_buf_free_zend) {
efree(out_buf);
}
}
/* }}} epeg_thumbnail_create */
/* {{{ proto resource epeg_open(string filename[, boolean is_data]) */
/**
* resource epeg epeg_open(string filename[, boolean is_data])
* object Epeg Epeg::__construct(string filename[, boolean is_data])
*
* Open a JPEG image for thumbnailing.
*
* @param string $filename The pathname or the URL or the binary data of the source image.
* @param bool $is_data Whether $file is a binary data or not.
* @return resource epeg An Epeg image handle is returned if succeeded in opening the image.
* False is returned if failed to open the image.
*/
static PHP_FUNCTION(epeg_open)
{
/* declaration of the resources */
zval *obj = getThis();
php_epeg_t *im = NULL;
/* declaration of the arguments */
char *file = NULL;
int file_len = 0;
zend_bool is_data = 0;
/* parse arguments */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_data) == FAILURE) {
RETURN_FALSE;
}
if (is_data) {
/* open the JPEG image stored in the buffer */
im = php_epeg_memory_open(file, file_len TSRMLS_CC);
} else {
/* open Epeg image handle */
im = php_epeg_file_open(file TSRMLS_CC);
}
if (im == NULL) {
RETURN_FALSE;
}
if (obj) {
php_epeg_object *intern;
intern = (php_epeg_object *)zend_object_store_get_object(obj TSRMLS_CC);
if (intern->ptr != NULL) {
php_epeg_free(im);
zend_throw_exception(zend_exception_get_default(TSRMLS_C),
"Epeg already initialized", 0 TSRMLS_CC);
return;
}
intern->ptr = im;
} else {
ZEND_REGISTER_RESOURCE(return_value, im, le_epeg);
}
}
/* }}} epeg_open */
/* {{{ proto resource epeg epeg_file_open(string filename) */
/**
* resource epeg epeg_file_open(string filename)
*
* Open a JPEG image for thumbnailing from pathname or URL.