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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
|
<?php // +------------------------------------------------------------------------+ // | class.upload.php | // +------------------------------------------------------------------------+ // | Copyright (c) Colin Verot 2003-2006. All rights reserved. | // | Version 0.23 | // | Last modified 23/12/2006 | // | Email colin@verot.net | // | Web http://www.verot.net | // +------------------------------------------------------------------------+ // | This program is free software; you can redistribute it and/or modify | // | it under the terms of the GNU General Public License version 2 as | // | published by the Free Software Foundation. | // | | // | This program is distributed in the hope that it will be useful, | // | but WITHOUT ANY WARRANTY; without even the implied warranty of | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | // | GNU General Public License for more details. | // | | // | You should have received a copy of the GNU General Public License | // | along with this program; if not, write to the | // | Free Software Foundation, Inc., 59 Temple Place, Suite 330, | // | Boston, MA 02111-1307 USA | // | | // | Please give credit on sites that use class.upload and submit changes | // | of the script so other people can use them as well. | // | This script is free to use, don't abuse. | // +------------------------------------------------------------------------+ //
/** * Class upload * * @version 0.23 * @author Colin Verot <colin@verot.net> * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @copyright Colin Verot * @package cmf * @subpackage external */
/** * Class upload * * <b>What does it do?</b> * * It manages file uploads for you. In short, it manages the uploaded file, * and allows you to do whatever you want with the file, especially if it * is an image, and as many times as you want. * * It is the ideal class to quickly integrate file upload in your site. * If the file is an image, you can convert, resize, crop it in many ways. * You can also apply filters, add borders, text, watermarks, etc... * That's all you need for a gallery script for instance. * * You can also use the class to work on local files, which is especially * useful to use the image manipulation features * * <b>How does it work?</b> * * You instanciate the class with the $_FILES['my_field'] array * where my_field is the field name from your upload form. * The class will check if the original file has been uploaded * to its temporary location (alternatively, you can instanciate * the class with a local filename). * * You can then set a number of processing variables to act on the file. * For instance, you can rename the file, and if it is an image, * convert and resize it in many ways. * You can also set what will the class do if the file already exists. * * Then you call the function {@link process} to actually perform the actions * according to the processing parameters you set above. * It will create new instances of the original file, * so the original file remains the same between each process. * The file will be manipulated, and copied to the given location. * The processing variables will be reseted once it is done. * * You can repeat setting up a new set of processing variables, * and calling {@link process} again as many times as you want. * When you have finished, you can call {@link clean} to delete * the original uploaded file. * * If you don't set any processing parameters and call {@link process} * just after instanciating the class. The uploaded file will be simply * copied to the given location without any alteration or checks. * * Don't forget to add <i>enctype="multipart/form-data"</i> in your form * tag <form> if you want your form to upload the file. * * <b>How to use it?</b><br> * Create a simple HTML file, with a form such as: * <pre> * <form enctype="multipart/form-data" method="post" action="upload.php"> * <input type="file" size="32" name="image_field" value=""> * <input type="submit" name="Submit" value="upload"> * </form> * </pre> * Create a file called upload.php: * <pre> * $handle = new upload($_FILES['image_field']); * if ($handle->uploaded) { * $handle->file_new_name_body = 'image_resized'; * $handle->image_resize = true; * $handle->image_x = 100; * $handle->image_ratio_y = true; * $handle->process('/home/user/files/'); * if ($handle->processed) { * echo 'image resized'; * $handle->clean(); * } else { * echo 'error : ' . $handle->error; * } * } * </pre> * * <b>Processing parameters</b> (reseted after each process) * <ul> * <li><b>file_new_name_body</b> replaces the name body (default: '')<br> * <pre>$handle->file_new_name_body = 'new name';</pre></li> * <li><b>file_name_body_add</b> appends to the name body (default: '')<br> * <pre>$handle->file_name_body_add = '_uploaded';</pre></li> * <li><b>file_new_name_ext</b> replaces the file extension (default: '')<br> * <pre>$handle->file_new_name_ext = 'txt';</pre></li> * <li><b>file_safe_name</b> formats the filename (spaces changed to _) (default: true)<br> * <pre>$handle->file_safe_name = true;</pre></li> * <li><b>file_overwrite</b> sets behaviour if file already exists (default: false)<br> * <pre>$handle->file_overwrite = true;</pre></li> * <li><b>file_auto_rename</b> automatically renames file if it already exists (default: true)<br> * <pre>$handle->file_auto_rename = true;</pre></li> * <li><b>auto_create_dir</b> automatically creates destination directory if missing (default: true)<br> * <pre>$handle->auto_create_dir = true;</pre></li> * <li><b>dir_auto_chmod</b> automatically attempts to chmod the destination directory if not writeable (default: true)<br> * <pre>$handle->dir_auto_chmod = true;</pre></li> * <li><b>dir_chmod</b> chmod used when creating directory or if directory not writeable (default: 0777)<br> * <pre>$handle->dir_chmod = 0777;</pre></li> * <li><b>file_max_size</b> sets maximum upload size (default: upload_max_filesize from php.ini)<br> * <pre>$handle->file_max_size = '1024'; // 1KB</pre></li> * <li><b>mime_check</b> sets if the class check the MIME against the {@link allowed} list (default: true)<br> * <pre>$handle->mime_check = false;</pre></li> * <li><b>mime_magic_check</b> sets if the class uses mime_magic (default: false)<br> * <pre>$handle->mime_magic_check = true;</pre></li> * <li><b>no_script</b> sets if the class turns scripts into test files (default: true)<br> * <pre>$handle->no_script = false;</pre></li> * <li><b>allowed</b> array of allowed mime-types (default: check {@link Init})<br> * <pre>$handle->allowed = array('application/pdf','application/msword');</pre></li> * </ul> * <ul> * <li><b>image_convert</b> if set, image will be converted (possible values : ''|'png'|'jpeg'|'gif'; default: '')<br> * <pre>$handle->image_convert = 'jpg';</pre></li> * <li><b>jpeg_quality</b> sets the compression quality for JPEG images (default: 75)<br> * <pre>$handle->jpeg_quality = 50;</pre></li> * <li><b>jpeg_size</b> if set to a size in bytes, will approximate {@link jpeg_quality} so the output image fits within the size (default: NULL)<br> * <pre>$handle->jpeg_size = 3072;</pre></li> * </ul> * <ul> * <li><b>image_resize</b> determines is an image will be resized (default: false)<br> * <pre>$handle->image_resize = true;</pre></li> * </ul> * The following variables are used only if {@link image_resize} == true * <ul> * <li><b>image_x</b> destination image width (default: 150)<br> * <pre>$handle->image_x = 100;</pre></li> * <li><b>image_y</b> destination image height (default: 150)<br> * <pre>$handle->image_y = 200;</pre></li> * </ul> * Use either one of the following * <ul> * <li><b>image_ratio</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes if true (default: false)<br> * <pre>$handle->image_ratio = true;</pre></li> * <li><b>image_ratio_crop</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, and cropping excedent to fill the space. setting can also be a string, with one or more from 'TBLR', indicating which side of the image will be kept while cropping (default: false)<br> * <pre>$handle->image_ratio = true;</pre></li> * <li><b>image_ratio_no_zoom_in</b> same as {@link image_ratio}, but won't resize if the source image is smaller than {@link image_x} x {@link image_y} (default: false)<br> * <pre>$handle->image_ratio_no_zoom_in = true;</pre></li> * <li><b>image_ratio_no_zoom_out</b> same as {@link image_ratio}, but won't resize if the source image is bigger than {@link image_x} x {@link image_y} (default: false)<br> * <pre>$handle->image_ratio_no_zoom_out = true;</pre></li> * <li><b>image_ratio_x</b> if true, resize image, calculating {@link image_x} from {@link image_y} and conserving the original sizes ratio (default: false)<br> * <pre>$handle->image_ratio_x = true;</pre></li> * <li><b>image_ratio_y</b> if true, resize image, calculating {@link image_y} from {@link image_x} and conserving the original sizes ratio (default: false)<br> * <pre>$handle->image_ratio_y = true;</pre></li> * </ul> * The following image manipulations require GD2+ * <ul> * <li><b>image_brightness</b> if set, corrects the brightness. value between -127 and 127 (default: NULL)<br> * <pre>$handle->image_brightness = 40;</pre></li> * <li><b>image_contrast</b> if set, corrects the contrast. value between -127 and 127 (default: NULL)<br> * <pre>$handle->image_contrast = 50;</pre></li> * <li><b>image_tint_color</b> if set, will tint the image with a color, value as hexadecimal #FFFFFF (default: NULL)<br> * <pre>$handle->image_tint_color = '#FF0000';</pre></li> * <li><b>image_overlay_color</b> if set, will add a colored overlay, value as hexadecimal #FFFFFF (default: NULL)<br> * <pre>$handle->image_overlay_color = '#FF0000';</pre></li> * <li><b>image_overlay_percent</b> used when {@link image_overlay_color} is set, determines the opacity (default: 50)<br> * <pre>$handle->image_overlay_percent = 20;</pre></li> * <li><b>image_negative</b> inverts the colors in the image (default: false)<br> * <pre>$handle->image_negative = true;</pre></li> * <li><b>image_greyscale</b> transforms an image into greyscale (default: false)<br> * <pre>$handle->image_greyscale = true;</pre></li> * <li><b>image_threshold</b> applies a threshold filter. value between -127 and 127 (default: NULL)<br> * <pre>$handle->image_threshold = 20;</pre></li> * </ul> * <ul> * <li><b>image_text</b> creates a text label on the image, value is a string (default: NULL)<br> * <pre>$handle->image_text = 'test';</pre></li> * <li><b>image_text_direction</b> text label direction, either 'h' horizontal or 'v' vertical (default: 'h')<br> * <pre>$handle->image_text_direction = 'v';</pre></li> * <li><b>image_text_color</b> text color for the text label, in hexadecimal (default: #FFFFFF)<br> * <pre>$handle->image_text_color = '#FF0000';</pre></li> * <li><b>image_text_percent</b> text opacity on the text label, integer between 0 and 100 (default: 100)<br> * <pre>$handle->image_text_percent = 50;</pre></li> * <li><b>image_text_background</b> text label background color, in hexadecimal (default: NULL)<br> * <pre>$handle->image_text_background = '#FFFFFF';</pre></li> * <li><b>image_text_background_percent</b> text label background opacity, integer between 0 and 100 (default: 100)<br> * <pre>$handle->image_text_background_percent = 50;</pre></li> * <li><b>image_text_font</b> built-in font for the text label, from 1 to 5. 1 is the smallest (default: 5)<br> * <pre>$handle->image_text_font = 4;</pre></li> * <li><b>image_text_x</b> absolute text label position, in pixels from the left border. can be negative (default: NULL)<br> * <pre>$handle->image_text_x = 5;</pre></li> * <li><b>image_text_y</b> absolute text label position, in pixels from the top border. can be negative (default: NULL)<br> * <pre>$handle->image_text_y = 5;</pre></li> * <li><b>image_text_position</b> text label position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: NULL)<br> * <pre>$handle->image_text_position = 'LR';</pre></li> * <li><b>image_text_padding</b> text label padding, in pixels. can be overridden by {@link image_text_padding_x} and {@link image_text_padding_y} (default: 0)<br> * <pre>$handle->image_text_padding = 5;</pre></li> * <li><b>image_text_padding_x</b> text label horizontal padding (default: NULL)<br> * <pre>$handle->image_text_padding_x = 2;</pre></li> * <li><b>image_text_padding_y</b> text label vertical padding (default: NULL)<br> * <pre>$handle->image_text_padding_y = 10;</pre></li> * <li><b>image_text_alignment</b> text alignment when text has multiple lines, either 'L', 'C' or 'R' (default: 'C')<br> * <pre>$handle->image_text_alignment = 'R';</pre></li> * <li><b>image_text_line_spacing</b> space between lines in pixels, when text has multiple lines (default: 0)<br> * <pre>$handle->image_text_line_spacing = 3;</pre></li> * </ul> * <ul> * <li><b>image_flip</b> flips image, wither 'h' horizontal or 'v' vertical (default: NULL)<br> * <pre>$handle->image_flip = 'h';</pre></li> * <li><b>image_rotate</b> rotates image. possible values are 90, 180 and 270 (default: NULL)<br> * <pre>$handle->image_rotate = 90;</pre></li> * <li><b>image_crop</b> crops image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: NULL)<br> * <pre>$handle->image_crop = array(50,40,30,20); OR '-20 20%'...</pre></li> * </ul> * <ul> * <li><b>image_bevel</b> adds a bevel border to the image. value is thickness in pixels (default: NULL)<br> * <pre>$handle->image_bevel = 20;</pre></li> * <li><b>image_bevel_color1</b> top and left bevel color, in hexadecimal (default: #FFFFFF)<br> * <pre>$handle->image_bevel_color1 = '#FFFFFF';</pre></li> * <li><b>image_bevel_color2</b> bottom and right bevel color, in hexadecimal (default: #000000)<br> * <pre>$handle->image_bevel_color2 = '#000000';</pre></li> * <li><b>image_border</b> adds a unicolor border to the image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: NULL)<br> * <pre>$handle->image_border = '3px'; OR '-20 20%' OR array(3,2)...</pre></li> * <li><b>image_border_color</b> border color, in hexadecimal (default: #FFFFFF)<br> * <pre>$handle->image_border_color = '#FFFFFF';</pre></li> * <li><b>image_frame</b> type of frame: 1=flat 2=crossed (default: NULL)<br> * <pre>$handle->image_frame = 2;</pre></li> * <li><b>image_frame_colors</b> list of hex colors, in an array or a space separated string (default: '#FFFFFF #999999 #666666 #000000')<br> * <pre>$handle->image_frame_colors = array('#999999', '#FF0000', '#666666', '#333333', '#000000');</pre></li> * </ul> * <ul> * <li><b>image_watermark</b> adds a watermark on the image, value is a local filename. accepted files are GIF, JPG, PNG and PNG alpha (default: NULL)<br> * <pre>$handle->image_watermark = 'watermark.png';</pre></li> * <li><b>image_watermark_x</b> absolute watermark position, in pixels from the left border. can be negative (default: NULL)<br> * <pre>$handle->image_watermark_x = 5;</pre></li> * <li><b>image_watermark_y</b> absolute watermark position, in pixels from the top border. can be negative (default: NULL)<br> * <pre>$handle->image_watermark_y = 5;</pre></li> * <li><b>image_watermark_position</b> watermark position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: NULL)<br> * <pre>$handle->image_watermark_position = 'LR';</pre></li> * </ul> * <ul> * <li><b>image_reflection_height</b> if set, a reflection will be added. Format is either in pixels or percentage, such as 40, '40', '40px' or '40%' (default: NULL)<br> * <pre>$handle->image_reflection_height = '25%';</pre></li> * <li><b>image_reflection_space</b> space in pixels between the source image and the reflection, can be negative (default: NULL)<br> * <pre>$handle->image_reflection_space = 3;</pre></li> * <li><b>image_reflection_color</b> reflection background color, in hexadecimal (default: #FFFFFF)<br> * <pre>$handle->image_reflection_color = '#000000';</pre></li> * <li><b>image_reflection_opacity</b> opacity level at which the reflection starts, integer between 0 and 100 (default: 60)<br> * <pre>$handle->image_reflection_opacity = 60;</pre></li> * </ul> * * <b>Requirements</b> * * Most of the image operations require GD. GD2 is greatly recommended * * The class is compatible with PHP 4.3+, and compatible with PHP5 * * <b>Changelog</b> * <ul> * <li><b>v 0.23</b> 23/12/2006<br> * - fixed a bug when processing more than once the same uploaded file. If there is an open_basedir restriction, the class now creates a temporary file for the first call to process(). This file will be used for subsequent processes, and will be deleted upon calling clean()</li> * <li><b>v 0.22</b> 16/12/2006<br> * - added automatic creation of a temporary file if the upload directory is not within open_basedir<br> * - fixed a bug which was preventing to work on a local file by overwriting it with its processed copy<br> * - added MIME types video/x-ms-wmv and image/x-png and fixed PNG support for IE weird MIME types<br> * - modified {@link image_ratio_crop} so it can accept one or more from string 'TBLR', determining which side of the image is kept while cropping<br> * - added support for multiple lines in the text, using "\n" as a line break<br> * - added {@link image_text_line_spacing} which allow to set the space between several lines of text<br> * - added {@link image_text_alignment} which allow to set the alignment when text has several lines<br> * - {@link image_text_font} can now be set to the path of a GDF font to load external fonts<br> * - added {@link image_reflection_height} to create a reflection of the source image, which height is in pixels or percentage<br> * - added {@link image_reflection_space} to set the space in pixels between the source image and the reflection<br> * - added {@link image_reflection_color} to set the reflection background color<br> * - added {@link image_reflection_opacity} to set the initial level of opacity of the reflection</li> * <li><b>v 0.21</b> 30/09/2006<br> * - added {@link image_ratio_crop} which resizes within {@link image_x} and {@link image_y}, keeping ratio, but filling the space by cropping excedent of image<br> * - added {@link mime_check}, which default is true, to set checks against {@link allowed} MIME list<br> * - if MIME is empty, the class now triggers an error<br> * - color #000000 is OK for {@link image_text_color}, and related text transparency bug fixed<br> * - {@link gd_version}() now uses gd_info(), or else phpinfo()<br> * - fixed path issue when the destination path has no trailing slash on Windows systems <br> * - removed inline functions to be fully PHP5 compatible </li> * <li><b>v 0.20</b> 11/08/2006<br> * - added some more error checking and messages (GD presence, permissions...)<br> * - fix when uploading files without extension<br> * - changed values for {@link image_brightness} and {@link image_contrast} to be between -127 and 127<br> * - added {@link dir_auto_create} to automatically and recursively create destination directory if missing.<br> * - added {@link dir_auto_chmod} to automatically chmod the destination directory if not writeable.<br> * - added {@link dir_chmod} to set the default chmod to use.<br> * - added {@link image_crop} to crop images<br> * - added {@link image_negative} to invert the colors on the image<br> * - added {@link image_greyscale} to turn the image into greyscale<br> * - added {@link image_threshold} to apply a threshold filter on the image<br> * - added {@link image_bevel}, {@link image_bevel_color1} and {@link image_bevel_color2} to add a bevel border<br> * - added {@link image_border} and {@link image_border_color} to add a single color border<br> * - added {@link image_frame} and {@link image_frame_colors} to add a multicolored frame</li> * <li><b>v 0.19</b> 29/03/2006<br> * - class is now compatible i18n (thanks Sylwester).<br> * - the class can mow manipulate local files, not only uploaded files (instanciate the class with a local filename).<br> * - {@link file_safe_name} has been improved a bit.<br> * - added {@link image_brightness}, {@link image_contrast}, {@link image_tint_color}, {@link image_overlay_color} and {@link image_overlay_percent} to do color manipulation on the images.<br> * - added {@link image_text} and all derivated settings to add a text label on the image.<br> * - added {@link image_watermark} and all derivated settings to add a watermark image on the image.<br> * - added {@link image_flip} and {@link image_rotate} for more image manipulations<br> * - added {@link jpeg_size} to calculate the JPG compression quality in order to fit within one filesize.</li> * <li><b>v 0.18</b> 02/02/2006<br> * - added {@link no_script} to turn dangerous scripts into text files.<br> * - added {@link mime_magic_check} to set the class to use mime_magic.<br> * - added {@link preserve_transparency} *experimental*. Thanks Gregor.<br> * - fixed size and mime checking, wasn't working :/ Thanks Willem.<br> * - fixed memory leak when resizing images.<br> * - when resizing, it is not necessary anymore to set {@link image_convert}.<br> * - il is now possible to simply convert an image, with no resizing.<br> * - sets the default {@link file_max_size} to upload_max_filesize from php.ini. Thanks Edward</li> * <li><b>v 0.17</b> 28/05/2005<br> * - the class can be used with any version of GD.<br> * - added security check on the file with a list of mime-types.<br> * - changed the license to GPL v2 only</li> * <li><b>v 0.16</b> 19/05/2005<br> * - added {@link file_auto_rename} automatic file renaming if the same filename already exists.<br> * - added {@link file_safe_name} safe formatting of the filename (spaces to _underscores so far).<br> * - added some more error reporting to avoid crash if GD is not present</li> * <li><b>v 0.15</b> 16/04/2005<br> * - added JPEG compression quality setting. Thanks Vad</li> * <li><b>v 0.14</b> 14/03/2005<br> * - reworked the class file to allow parsing with phpDocumentor</li> * <li><b>v 0.13</b> 07/03/2005<br> * - fixed a bug with {@link image_ratio}. Thanks Justin.<br> * - added {@link image_ratio_no_zoom_in} and {@link image_ratio_no_zoom_out}</li> * <li><b>v 0.12</b> 21/01/2005<br> * - added {@link image_ratio} to resize within max values, keeping image ratio</li> * <li><b>v 0.11</b> 22/08/2003<br> * - update for GD2 (changed imageresized() into imagecopyresampled() and imagecreate() into imagecreatetruecolor())</li> * </ul> * * @package cmf * @subpackage external */ class upload {
/** * Uploaded file name * * @access public * @var string */ var $file_src_name;
/** * Uploaded file name body (i.e. without extension) * * @access public * @var string */ var $file_src_name_body;
/** * Uploaded file name extension * * @access public * @var string */ var $file_src_name_ext;
/** * Uploaded file MIME type * * @access public * @var string */ var $file_src_mime;
/** * Uploaded file size, in bytes * * @access public * @var double */ var $file_src_size;
/** * Holds eventual PHP error code from $_FILES * * @access public * @var string */ var $file_src_error;
/** * Uloaded file name, including server path * * @access private * @var string */ var $file_src_pathname;
/** * Uloaded file name temporary copy * * @access private * @var string */ var $file_src_temp;
/** * Destination file name * * @access private * @var string */ var $file_dst_path;
/** * Destination file name * * @access public * @var string */ var $file_dst_name;
/** * Destination file name body (i.e. without extension) * * @access public * @var string */ var $file_dst_name_body;
/** * Destination file extension * * @access public * @var string */ var $file_dst_name_ext;
/** * Destination file name, including path * * @access private * @var string */ var $file_dst_pathname;
/** * Source image width * * @access private * @var integer */ var $image_src_x;
/** * Source image height * * @access private * @var integer */ var $image_src_y;
/** * Destination image width * * @access private * @var integer */ var $image_dst_x;
/** * Destination image height * * @access private * @var integer */ var $image_dst_y;
/** * Flag set after instanciating the class * * Indicates if the file has been uploaded properly * * @access public * @var bool */ var $uploaded;
/** * Flag stopping PHP upload checks * * Indicates whether we instanciated the class with a filename, in which case * we will not check on the validity of the PHP *upload* * * This flag is automatically set to true when working on a local file * * Warning: for uploads, this flag MUST be set to false for security reason * * @access public * @var bool */ var $no_upload_check;
/** * Flag set after calling a process * * Indicates if the processing, and copy of the resulting file went OK * * @access public * @var bool */ var $processed;
/** * Holds eventual error message in plain english * * @access public * @var string */ var $error;
/** * Holds an HTML formatted log * * @access public * @var string */ var $log;
// overiddable processing variables /** * Set this variable to replace the name body (i.e. without extension) * * @access public * @var string */ var $file_new_name_body;
/** * Set this variable to add a string to the faile name body * * @access public * @var string */ var $file_name_body_add;
/** * Set this variable to change the file extension * * @access public * @var string */ var $file_new_name_ext;
/** * Set this variable to format the filename (spaces changed to _) * * @access public * @var boolean */ var $file_safe_name;
/** * Set this variable to false if you don't want to check the MIME against the allowed list * * This variable is set to true by default for security reason * * @access public * @var boolean */ var $mime_check;
/** * Set this variable to true if you want to check the MIME type against a mime_magic file * * This variable is set to false by default as many systems don't have mime_magic installed or properly set * * @access public * @var boolean */ var $mime_magic_check;
/** * Set this variable to false if you don't want to turn dangerous scripts into simple text files * * @access public * @var boolean */ var $no_script;
/** * Set this variable to true to allow automatic renaming of the file * if the file already exists * * Default value is true * * For instance, on uploading foo.ext,<br> * if foo.ext already exists, upload will be renamed foo_1.ext<br> * and if foo_1.ext already exists, upload will be renamed foo_2.ext<br> * * @access public * @var bool */ var $file_auto_rename;
/** * Set this variable to true to allow automatic creation of the destination * directory if it is missing (works recursively) * * Default value is true * * @access public * @var bool */ var $dir_auto_create;
/** * Set this variable to true to allow automatic chmod of the destination * directory if it is not writeable * * Default value is true * * @access public * @var bool */ var $dir_auto_chmod;
/** * Set this variable to the default chmod you want the class to use * when creating directories, or attempting to write in a directory * * Default value is 0777 (without quotes) * * @access public * @var bool */ var $dir_chmod;
/** * Set this variable tu true to allow overwriting of an existing file * * Default value is false, so no files will be overwritten * * @access public * @var bool */ var $file_overwrite;
/** * Set this variable to change the maximum size in bytes for an uploaded file * * Default value is the value <i>upload_max_filesize</i> from php.ini * * @access public * @var double */ var $file_max_size;
/** * Set this variable to true to resize the file if it is an image * * You will probably want to set {@link image_x} and {@link image_y}, and maybe one of the ratio variables * * Default value is false (no resizing) * * @access public * @var bool */ var $image_resize;
/** * Set this variable to convert the file if it is an image * * Possibles values are : ''; 'png'; 'jpeg'; 'gif' * * Default value is '' (no conversion)<br> * If {@link resize} is true, {@link convert} will be set to the source file extension * * @access public * @var string */ var $image_convert;
/** * Set this variable to the wanted (or maximum/minimum) width for the processed image, in pixels * * Default value is 150 * * @access public * @var integer */ var $image_x;
/** * Set this variable to the wanted (or maximum/minimum) height for the processed image, in pixels * * Default value is 150 * * @access public * @var integer */ var $image_y;
/** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y} * * Default value is false * * @access public * @var bool */ var $image_ratio;
/** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y} * * The image will be resized as to fill the whole space, and excedent will be cropped * * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right) * If set as a string, it determines which side of the image is kept while cropping. * By default, the part of the image kept is in the center, i.e. it crops equally on both sides * * Default value is false * * @access public * @var mixed */ var $image_ratio_crop;
/** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}, * but only if original image is bigger * * Default value is false * * @access public * @var bool */ var $image_ratio_no_zoom_in;
/** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}, * but only if original image is smaller * * Default value is false * * @access public * @var bool */ var $image_ratio_no_zoom_out;
/** * Set this variable to calculate {@link image_x} automatically , using {@link image_y} and conserving ratio * * Default value is false * * @access public * @var bool */ var $image_ratio_x;
/** * Set this variable to calculate {@link image_y} automatically , using {@link image_x} and conserving ratio * * Default value is false * * @access public * @var bool */ var $image_ratio_y;
/** * Quality of JPEG created/converted destination image * * Default value is 75 * * @access public * @var integer; */ var $jpeg_quality;
/** * Determines the quality of the JPG image to fit a desired file size * * Value is in bytes. The JPG quality will be set between 1 and 100% * The calculations are approximations. * * Default value is NULL (no calculations) * * @access public * @var integer; */ var $jpeg_size;
/** * Preserve transparency when resizing or converting an image (experimental) * * Default value is false * * Currently works only when resizing GIFs or converting transparent GIF to PNG<br> * It has problems with transparent PNG * * @access public * @var integer; */ var $preserve_transparency; /** * Corrects the image brightness * * Value can range between -127 and 127 * * Default value is NULL * * @access public * @var integer; */ var $image_brightness;
/** * Corrects the image contrast * * Value can range between -127 and 127 * * Default value is NULL * * @access public * @var integer; */ var $image_contrast; /** * Applies threshold filter * * Value can range between -127 and 127 * * Default value is NULL * * @access public * @var integer; */ var $image_threshold;
/** * Applies a tint on the image * * Value is an hexadecimal color, such as #FFFFFF * * Default value is NULL * * @access public * @var string; */ var $image_tint_color;
/** * Applies a colored overlay on the image * * Value is an hexadecimal color, such as #FFFFFF * * To use with {@link image_overlay_percent} * * Default value is NULL * * @access public * @var string; */ var $image_overlay_color;
/** * Sets the percentage for the colored overlay * * Value is a percentage, as an integer between 0 and 100 * * Unless used with {@link image_overlay_color}, this setting has no effect * * Default value is 50 * * @access public * @var integer; */ var $image_overlay_percent;
/** * Inverts the color of an image * * Default value is FALSE * * @access public * @var boolean; */ var $image_negative; /** * Turns the image into greyscale * * Default value is FALSE * * @access public * @var boolean; */ var $image_greyscale;
/** * Adds a text label on the image * * Value is a string, any text. Text will not word-wrap, although you can use breaklines in your text "\n" * * If set, this setting allow the use of all other settings starting with image_text_ * * Default value is NULL * * @access public * @var string; */ var $image_text;
/** * Sets the text direction for the text label * * Value is either 'h' or 'v', as in horizontal and vertical * * Default value is h (horizontal) * * @access public * @var string; */ var $image_text_direction;
/** * Sets the text color for the text label * * Value is an hexadecimal color, such as #FFFFFF * * Default value is #FFFFFF (white) * * @access public * @var string; */ var $image_text_color;
/** * Sets the text visibility in the text label * * Value is a percentage, as an integer between 0 and 100 * * Default value is 100 * * @access public * @var integer; */ var $image_text_percent;
/** * Sets the text background color for the text label * * Value is an hexadecimal color, such as #FFFFFF * * Default value is NULL (no background) * * @access public * @var string; */ var $image_text_background;
/** * Sets the text background visibility in the text label * * Value is a percentage, as an integer between 0 and 100 * * Default value is 100 * * @access public * @var integer; */ var $image_text_background_percent;
/** * Sets the text font in the text label * * Value is a an integer between 1 and 5 for GD built-in fonts. 1 is the smallest font, 5 the biggest * Value can also be a string, which represents the path to a GDF font. The font will be loaded into GD, and used as a built-in font. * * Default value is 5 * * @access public * @var mixed; */ var $image_text_font;
/** * Sets the text label position within the image * * Value is one or two out of 'TBLR' (top, bottom, left, right) * * The positions are as following: * <pre> * TL T TR * L R * BL B BR * </pre> * * Default value is NULL (centered, horizontal and vertical) * * Note that is {@link image_text_x} and {@link image_text_y} are used, this setting has no effect * * @access public * @var string; */ var $image_text_position;
/** * Sets the text label absolute X position within the image * * Value is in pixels, representing the distance between the left of the image and the label * If a negative value is used, it will represent the distance between the right of the image and the label * * Default value is NULL (so {@link image_text_position} is used) * * @access public * @var integer; */ var $image_text_x;
/** * Sets the text label absolute Y position within the image * * Value is in pixels, representing the distance between the top of the image and the label * If a negative value is used, it will represent the distance between the bottom of the image and the label * * Default value is NULL (so {@link image_text_position} is used) * * @access public * @var integer; */ var $image_text_y;
/** * Sets the text label padding * * Value is in pixels, representing the distance between the text and the label background border * * Default value is 0 * * This setting can be overriden by {@link image_text_padding_x} and {@link image_text_padding_y} * * @access public * @var integer; */ var $image_text_padding;
/** * Sets the text label horizontal padding * * Value is in pixels, representing the distance between the text and the left and right label background borders * * Default value is NULL * * If set, this setting overrides the horizontal part of {@link image_text_padding} * * @access public * @var integer; */ var $image_text_padding_x;
/** * Sets the text label vertical padding * * Value is in pixels, representing the distance between the text and the top and bottom label background borders * * Default value is NULL * * If set, his setting overrides the vertical part of {@link image_text_padding} * * @access public * @var integer; */ var $image_text_padding_y;
/** * Sets the text alignment * * Value is a string, which can be either 'L', 'C' or 'R' * * Default value is 'C' * * This setting is relevant only if the text has several lines. * * @access public * @var string; */ var $image_text_alignment;
/** * Sets the text line spacing * * Value is an integer, in pixels * * Default value is 0 * * This setting is relevant only if the text has several lines. * * @access public * @var integer; */ var $image_text_line_spacing;
/** * Sets the height of the reflection * * Value is an integer in pixels, or a string which format can be in pixels or percentage. * For instance, values can be : 40, '40', '40px' or '40%' * * Default value is NULL, no reflection * * @access public * @var mixed; */ var $image_reflection_height;
/** * Sets the space between the source image and its relection * * Value is an integer in pixels, which can be negative * * Default value is 2 * * This setting is relevant only if {@link image_reflection_height} is set * * @access public * @var integer; */ var $image_reflection_space;
/** * Sets the color of the reflection background. * * Value is an hexadecimal color, such as #FFFFFF * * Default value is #FFFFFF * * This setting is relevant only if {@link image_reflection_height} is set * * @access public * @var string; */ var $image_reflection_color;
/** * Sets the initial opacity of the reflection * * Value is an integer between 0 (no opacity) and 100 (full opacity). * The reflection will start from {@link image_reflection_opacity} and end up at 0 * * Default value is 60 * * This setting is relevant only if {@link image_reflection_height} is set * * @access public * @var integer; */ var $image_reflection_opacity;
/** * Flips the image vertically or horizontally * * Value is either 'h' or 'v', as in horizontal and vertical * * Default value is NULL (no flip) * * @access public * @var string; */ var $image_flip;
/** * Rotates the image by increments of 45 degrees * * Value is either 90, 180 or 270 * * Default value is NULL (no rotation) * * @access public * @var string; */ var $image_rotate;
/** * Crops an image * * Values are four dimensions, or two, or one (CSS style) * They represent the amount cropped top, right, bottom and left. * These values can either be in an array, or a space separated string. * Each value can be in pixels (with or without 'px'), or percentage (of the source image) * * For instance, are valid: * <pre> * $foo->image_crop = 20 OR array(20); * $foo->image_crop = '20px' OR array('20px'); * $foo->image_crop = '20 40' OR array('20', 40); * $foo->image_crop = '-20 25%' OR array(-20, '25%'); * $foo->image_crop = '20px 25%' OR array('20px', '25%'); * $foo->image_crop = '20% 25%' OR array('20%', '25%'); * $foo->image_crop = '20% 25% 10% 30%' OR array('20%', '25%', '10%', '30%'); * $foo->image_crop = '20px 25px 2px 2px' OR array('20px', '25%px', '2px', '2px'); * $foo->image_crop = '20 25% 40px 10%' OR array(20, '25%', '40px', '10%'); * </pre> * * If a value is negative, the image will be expanded, and the extra parts will be filled with black * * Default value is NULL (no cropping) * * @access public * @var string OR array; */ var $image_crop;
/** * Adds a bevel border on the image * * Value is a positive integer, representing the thickness of the bevel * * If the bevel colors are the same as the background, it makes a fade out effect * * Default value is NULL (no bevel) * * @access public * @var integer; */ var $image_bevel;
/** * Top and left bevel color * * Value is a color, in hexadecimal format * This setting is used only if {@link image_bevel} is set * * Default value is #FFFFFF * * @access public * @var string; */ var $image_bevel_color1;
/** * Right and bottom bevel color * * Value is a color, in hexadecimal format * This setting is used only if {@link image_bevel} is set * * Default value is #000000 * * @access public * @var string; */ var $image_bevel_color2;
/** * Adds a single-color border on the outer of the image * * Values are four dimensions, or two, or one (CSS style) * They represent the border thickness top, right, bottom and left. * These values can either be in an array, or a space separated string. * Each value can be in pixels (with or without 'px'), or percentage (of the source image) * * See {@link image_crop} for valid formats * * If a value is negative, the image will be cropped. * Note that the dimensions of the picture will be increased by the borders' thickness * * Default value is NULL (no border) * * @access public * @var integer; */ var $image_border;
/** * Border color * * Value is a color, in hexadecimal format. * This setting is used only if {@link image_border} is set * * Default value is #FFFFFF * * @access public * @var string; */ var $image_border_color;
/** * Adds a multi-color frame on the outer of the image * * Value is an integer. Two values are possible for now: * 1 for flat border, meaning that the frame is mirrored horizontally and vertically * 2 for crossed border, meaning that the frame will be inversed, as in a bevel effect * * The frame will be composed of colored lines set in {@link image_frame_colors} * * Note that the dimensions of the picture will be increased by the borders' thickness * * Default value is NULL (no frame) * * @access public * @var integer; */ var $image_frame;
/** * Sets the colors used to draw a frame * * Values is a list of n colors in hexadecimal format. * These values can either be in an array, or a space separated string. * * The colors are listed in the following order: from the outset of the image to its center * * For instance, are valid: * <pre> * $foo->image_frame_colors = '#FFFFFF #999999 #666666 #000000'; * $foo->image_frame_colors = array('#FFFFFF', '#999999', '#666666', '#000000'); * </pre> * * This setting is used only if {@link image_frame} is set * * Default value is '#FFFFFF #999999 #666666 #000000' * * @access public * @var string OR array; */ var $image_frame_colors;
/** * Adds a watermark on the image * * Value is a local image filename, relative or absolute. GIF, JPG and PNG are supported, as well as PNG alpha. * * If set, this setting allow the use of all other settings starting with image_watermark_ * * Default value is NULL * * @access public * @var string; */ var $image_watermark;
/** * Sets the watermarkposition within the image * * Value is one or two out of 'TBLR' (top, bottom, left, right) * * The positions are as following: TL T TR * L R * BL B BR * * Default value is NULL (centered, horizontal and vertical) * * Note that is {@link image_watermark_x} and {@link image_watermark_y} are used, this setting has no effect * * @access public * @var string; */ var $image_watermark_position;
/** * Sets the watermark absolute X position within the image * * Value is in pixels, representing the distance between the top of the image and the watermark * If a negative value is used, it will represent the distance between the bottom of the image and the watermark * * Default value is NULL (so {@link image_watermark_position} is used) * * @access public * @var integer; */ var $image_watermark_x;
/** * Sets the twatermark absolute Y position within the image * * Value is in pixels, representing the distance between the left of the image and the watermark * If a negative value is used, it will represent the distance between the right of the image and the watermark * * Default value is NULL (so {@link image_watermark_position} is used) * * @access public * @var integer; */ var $image_watermark_y;
/** * Allowed MIME types * * Default is a selection of safe mime-types, but you might want to change it * * @access public * @var integer */ var $allowed;
/** * Init or re-init all the processing variables to their default values * * This function is called in the constructor, and after each call of {@link process} * * @access private */ function init() {
// overiddable variables $this->file_new_name_body = ''; // replace the name body $this->file_name_body_add = ''; // append to the name body $this->file_new_name_ext = ''; // replace the file extension $this->file_safe_name = true; // format safely the filename $this->file_overwrite = false; // allows overwritting if the file already exists $this->file_auto_rename = true; // auto-rename if the file already exists $this->dir_auto_create = true; // auto-creates directory if missing $this->dir_auto_chmod = true; // auto-chmod directory if not writeable $this->dir_chmod = 0777; // default chmod to use $this->mime_check = true; // don't check the mime type against the allowed list $this->mime_magic_check = false; // don't double check the MIME type with mime_magic $this->no_script = true; // turns scripts into test files
$val = trim(ini_get('upload_max_filesize')); $last = strtolower($val{strlen($val)-1}); switch($last) { case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } $this->file_max_size = $val; $this->image_resize = false; // resize the image $this->image_convert = ''; // convert. values :''; 'png'; 'jpeg'; 'gif'
$this->image_x = 150; $this->image_y = 150; $this->image_ratio = false; // keeps aspect ration with x and y dimensions $this->image_ratio_crop = false; // keeps aspect ration with x and y dimensions, filling the space $this->image_ratio_no_zoom_in = false; $this->image_ratio_no_zoom_out = false; $this->image_ratio_x = false; // calculate the $image_x if true $this->image_ratio_y = false; // calculate the $image_y if true $this->jpeg_quality = 75; $this->jpeg_size = NULL; $this->preserve_transparency = false; $this->image_brightness = NULL; $this->image_contrast = NULL; $this->image_threshold = NULL; $this->image_tint_color = NULL; $this->image_overlay_color = NULL; $this->image_overlay_percent = NULL; $this->image_negative = false; $this->image_greyscale = false;
$this->image_text = NULL; $this->image_text_direction = NULL; $this->image_text_color = '#FFFFFF'; $this->image_text_percent = 100; $this->image_text_background = NULL; $this->image_text_background_percent = 100; $this->image_text_font = 5; $this->image_text_x = NULL; $this->image_text_y = NULL; $this->image_text_position = NULL; $this->image_text_padding = 0; $this->image_text_padding_x = NULL; $this->image_text_padding_y = NULL; $this->image_text_alignment = 'C'; $this->image_text_line_spacing = 0;
$this->image_reflection_height = NULL; $this->image_reflection_space = 2; $this->image_reflection_color = '#ffffff'; $this->image_reflection_opacity = 60; $this->image_watermark = NULL; $this->image_watermark_x = NULL; $this->image_watermark_y = NULL; $this->image_watermark_position = NULL;
$this->image_flip = NULL; $this->image_rotate = NULL; $this->image_crop = NULL;
$this->image_bevel = NULL; $this->image_bevel_color1 = '#FFFFFF'; $this->image_bevel_color2 = '#000000'; $this->image_border = NULL; $this->image_border_color = '#FFFFFF'; $this->image_frame = NULL; $this->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
$this->allowed = array("application/arj", "application/excel", "application/gnutar", "application/msword", "application/mspowerpoint", "application/octet-stream", "application/pdf", "application/powerpoint", "application/postscript", "application/plain", "application/rtf", "application/vnd.ms-excel", "application/vocaltec-media-file", "application/wordperfect", "application/x-bzip", "application/x-bzip2", "application/x-compressed", "application/x-excel", "application/x-gzip", "application/x-latex", "application/x-midi", "application/x-msexcel", "application/x-rtf", "application/x-sit", "application/x-stuffit", "application/x-shockwave-flash", "application/x-troff-msvideo", "application/x-zip-compressed", "application/xml", "application/zip", "audio/aiff", "audio/basic", "audio/midi", "audio/mod", "audio/mpeg", "audio/mpeg3", "audio/wav", "audio/x-aiff", "audio/x-au", "audio/x-mid", "audio/x-midi", "audio/x-mod", "audio/x-mpeg-3", "audio/x-wav", "audio/xm", "image/bmp", "image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/tiff", "image/x-tiff", "image/x-windows-bmp", "multipart/x-zip", "multipart/x-gzip", "music/crescendo", "text/richtext", "text/plain", "text/xml", "video/avi", "video/mpeg", "video/msvideo", "video/quicktime", "video/quicktime", "video/x-mpeg", "video/x-ms-wmv", "video/x-ms-asf", "video/x-ms-asf-plugin", "video/x-msvideo", "x-music/x-midi"); }
/** * Constructor. Checks if the file has been uploaded * * The constructor takes $_FILES['form_field'] array as argument * where form_field is the form field name * * The constructor will check if the file has been uploaded in its temporary location, and * accordingly will set {@link uploaded} (and {@link error} is an error occurred) * * If the file has been uploaded, the constructor will populate all the variables holding the upload * information (none of the processing class variables are used here). * You can have access to information about the file (name, size, MIME type...). * * * Alternatively, you can set the first argument to be a local filename (string) * and the second argument to be a MIME type (string) (second argument optional if mime_magic is installed) * This allows processing of a local file, as if the file was uploaded * * @access private * @param array $file $_FILES['form_field'] * or string $file Local filename */ function upload($file) {
$this->file_src_name = ''; $this->file_src_name_body = ''; $this->file_src_name_ext = ''; $this->file_src_mime = ''; $this->file_src_size = ''; $this->file_src_error = ''; $this->file_src_pathname = ''; $this->file_src_temp = '';
$this->file_dst_path = ''; $this->file_dst_name = ''; $this->file_dst_name_body = ''; $this->file_dst_name_ext = ''; $this->file_dst_pathname = '';
$this->image_src_x = 0; $this->image_src_y = 0; $this->image_dst_type = ''; $this->image_dst_x = 0; $this->image_dst_y = 0;
$this->uploaded = true; $this->no_upload_check = false; $this->processed = true; $this->error = ''; $this->log = ''; $this->allowed = array(); $this->init();
if (!$file) { $this->uploaded = false; $this->error = _("File error. Please try again"); }
// check if we sent a local filename rather than a $_FILE element if (!is_array($file)) { if (empty($file)) { $this->uploaded = false; $this->error = _("File error. Please try again"); } else { $this->no_upload_check = TRUE; // this is a local filename, i.e.not uploaded $this->log .= '<b>' . _("source is a local file") . ' ' . $file . '</b><br />';
if ($this->uploaded && !file_exists($file)) { $this->uploaded = false; $this->error = _("Local file doesn't exist"); } if ($this->uploaded && !is_readable($file)) { $this->uploaded = false; $this->error = _("Local file is not readable"); }
if ($this->uploaded) { $this->file_src_pathname = $file; $this->file_src_name = basename($file); $this->log .= '- ' . _("local file name OK") . '<br />'; ereg('\.([^\.]*$)', $this->file_src_name, $extension); if (is_array($extension)) { $this->file_src_name_ext = strtolower($extension[1]); $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1); } else { $this->file_src_name_ext = ''; $this->file_src_name_body = $this->file_src_name; } $this->file_src_size = (file_exists($file) ? filesize($file) : 0); // we try to retrieve the MIME type $info = getimagesize($this->file_src_pathname); $this->file_src_mime = (array_key_exists('mime', $info) ? $info['mime'] : NULL); // if we don't have a MIME type, we attempt to retrieve it the old way if (empty($this->file_src_mime)) { $mime = (array_key_exists(2, $info) ? $info[2] : NULL); // 1 = GIF, 2 = JPG, 3 = PNG $this->file_src_mime = ($mime==1 ? 'image/gif' : ($mime==2 ? 'image/jpeg' : ($mime==3 ? 'image/png' : NULL))); } // if we still don't have a MIME type, we attempt to retrieve it otherwise if (empty($this->file_src_mime) && function_exists('mime_content_type')) { $this->file_src_mime = mime_content_type($this->file_src_pathname); } $this->file_src_error = 0; } } } else { // this is an element from $_FILE, i.e. an uploaded file $this->log .= '<b>' . _("source is an uploaded file") . '</b><br />'; if ($this->uploaded) { $this->file_src_error = $file['error']; switch($this->file_src_error) { case 0: // all is OK $this->log .= '- ' . _("upload OK") . '<br />'; break; case 1: $this->uploaded = false; $this->error = _("File upload error (the uploaded file exceeds the upload_max_filesize directive in php.ini)"); break; case 2: $this->uploaded = false; $this->error = _("File upload error (the uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form)"); break; case 3: $this->uploaded = false; $this->error = _("File upload error (the uploaded file was only partially uploaded)"); break; case 4: $this->uploaded = false; $this->error = _("File upload error (no file was uploaded)"); break; default: $this->uploaded = false; $this->error = _("File upload error (unknown error code)"); } } if ($this->uploaded) { $this->file_src_pathname = $file['tmp_name']; $this->file_src_name = $file['name']; if ($this->file_src_name == '') { $this->uploaded = false; $this->error = _("File upload error. Please try again"); } }
if ($this->uploaded) { $this->log .= '- ' . _("file name OK") . '<br />'; ereg('\.([^\.]*$)', $this->file_src_name, $extension); if (is_array($extension)) { $this->file_src_name_ext = strtolower($extension[1]); $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1); } else { $this->file_src_name_ext = ''; $this->file_src_name_body = $this->file_src_name; } $this->file_src_size = $file['size']; $this->file_src_mime = $file['type']; } }
$this->log .= '- ' . _("source variables") . '<br />'; $this->log .= ' file_src_name : ' . $this->file_src_name . '<br />'; $this->log .= ' file_src_name_body : ' . $this->file_src_name_body . '<br />'; $this->log .= ' file_src_name_ext : ' . $this->file_src_name_ext . '<br />'; $this->log .= ' file_src_pathname : ' . $this->file_src_pathname . '<br />'; $this->log .= ' file_src_mime : ' . $this->file_src_mime . '<br />'; $this->log .= ' file_src_size : ' . $this->file_src_size . ' (max= ' . $this->file_max_size . ')<br />'; $this->log .= ' file_src_error : ' . $this->file_src_error . '<br />'; }
/** * Returns the version of GD * * @access public * @return float GD version */ function gd_version() { static $gd_version = null; if ($gd_version === null) { if (function_exists('gd_info')) { $gd = gd_info(); $gd = $gd["GD Version"]; $regex = "/([\d\.]+)/i"; } else { ob_start(); phpinfo(8); $gd = ob_get_contents(); ob_end_clean(); $regex = "/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i"; } if (preg_match($regex, $gd, $m)) { $gd_version = (float) $m[1]; } else { $gd_version = 0; } } return $gd_version; }
/** * Creates directories recursively * * @access private * @param string $path Path to create * @param integer $mode Optional permissions * @return boolean Success */ function r_mkdir($path, $mode = 0777) { return is_dir($path) || ( $this->r_mkdir(dirname($path), $mode) && @mkdir($path, $mode) ); }
/** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Path location of the uploaded file, with an ending slash */ function process($server_path) {
$this->error = ''; $this->processed = true;
if(strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') $server_path = $server_path . '\\'; } else { if (substr($server_path, -1, 1) != '/') $server_path = $server_path . '/'; } $this->log .= '<b>' . _("process file to") . ' ' . $server_path . '</b><br />';
// checks file size and mine type if ($this->uploaded) {
if ($this->file_src_size > $this->file_max_size ) { $this->processed = false; $this->error = _("File too big"); } else { $this->log .= '- ' . _("file size OK") . '<br />'; }
// turn dangerous scripts into text files if ($this->no_script) { if (((substr($this->file_src_mime, 0, 5) == 'text/' || strpos($this->file_src_mime, 'javascript') !== false) && (substr($this->file_src_name, -4) != '.txt')) || preg_match('/\.(php|pl|py|cgi|asp)$/i', $this->file_src_name) || empty($this->file_src_name_ext)) { $this->file_src_mime = 'text/plain'; $this->log .= '- ' . _("script") . ' ' . $this->file_src_name . ' ' . _("renamed as") . ' ' . $this->file_src_name . '.txt!<br />'; $this->file_src_name_ext .= (empty($this->file_src_name_ext) ? 'txt' : '.txt'); } }
// checks MIME type with mime_magic if ($this->mime_magic_check && function_exists('mime_content_type')) { $detected_mime = mime_content_type($this->file_src_pathname); if ($this->file_src_mime != $detected_mime) { $this->log .= '- ' . _("MIME type detected as") . ' ' . $detected_mime . ' ' . _("but given as") . ' ' . $this->file_src_mime . '!<br />'; $this->file_src_mime = $detected_mime; } }
if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = _("MIME type can't be detected!"); } else if ($this->mime_check && !empty($this->file_src_mime) && !array_key_exists($this->file_src_mime, array_flip($this->allowed))) { $this->processed = false; $this->error = _("Incorrect type of file"); } else { $this->log .= '- ' . _("file mime OK") . ' : ' . $this->file_src_mime . '<br />'; } } else { $this->error = _("File not uploaded. Can't carry on a process"); $this->processed = false; }
if ($this->processed) { $this->file_dst_path = $server_path;
// repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext;
if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- ' . _("new file name body") . ' : ' . $this->file_new_name_body . '<br />'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- ' . _("new file name ext") . ' : ' . $this->file_new_name_ext . '<br />'; } if ($this->file_name_body_add != '') { // append a bit to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- ' . _("file name body add") . ' : ' . $this->file_name_body_add . '<br />'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_','_'), $this->file_dst_name_body) ; $this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body) ; $this->log .= '- ' . _("file name safe format") . '<br />'; }
$this->log .= '- ' . _("destination variables") . '<br />'; $this->log .= ' file_dst_path : ' . $this->file_dst_path . '<br />'; $this->log .= ' file_dst_name_body : ' . $this->file_dst_name_body . '<br />'; $this->log .= ' file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />';
// do we do some image manipulation? $image_manipulation = ($this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height);
if ($image_manipulation) { if ($this->image_convert=='') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- ' . _("image operation, keep extension") . '<br />'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- ' . _("image operation, change extension for conversion type") . '<br />'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- ' . _("no image operation, keep extension") . '<br />'; } if (!$this->file_auto_rename) { $this->log .= '- ' . _("no auto_rename if same filename exists") . '<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- ' . _("checking for auto_rename") . '<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt>1) $this->log .= ' ' . _("auto_rename to") . ' ' . $this->file_dst_name . '<br />'; }
$this->log .= '- ' . _("destination file details") . '<br />'; $this->log .= ' file_dst_name : ' . $this->file_dst_name . '<br />'; $this->log .= ' file_dst_pathname : ' . $this->file_dst_pathname . '<br />';
if ($this->file_overwrite) { $this->log .= '- ' . _("no overwrite checking") . '<br />'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->file_dst_name . ' ' . _("already exists. Please change the file name"); } else { $this->log .= '- ' . $this->file_dst_name . ' ' . _("doesn't exist already") . '<br />'; } } } else { $this->processed = false; }
// if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists if (!empty($this->file_src_temp)) { $this->log .= '- ' . _("use the temp file instead of the original file since it is a second process") . '<br />'; $this->file_src_pathname = $this->file_src_temp; if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = _("No correct temp source file. Can't carry on a process"); } // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file() } else if (!$this->no_upload_check) { if (!is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = _("No correct uploaded source file. Can't carry on a process"); } // otherwise, if we don;t check on uploaded files (local file for instance), we use file_exists() } else { if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = _("No correct source file. Can't carry on a process"); } } // checks if the destination directory exists, and attempt to create it if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' ' . _("doesn't exist. Attempting creation:"); if (!$this->r_mkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Destination directory can't be created. Can't carry on a process"); } else { $this->log .= ' ' . _("success") . '<br />'; } } else { $this->error = _("Destination directory doesn't exist. Can't carry on a process"); } }
if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = _("Destination path is not a directory. Can't carry on a process"); }
// checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' ' . _("is not writeable. Attempting chmod:"); if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Destination directory can't be made writeable. Can't carry on a process"); } else { $this->log .= ' ' . _("success") . '<br />'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = _("Destination directory is still not writeable. Can't carry on a process"); } else { @fclose($f); } } } else { $this->processed = false; $this->error = _("Destination path is not a writeable. Can't carry on a process"); } } else { @fclose($f); @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); }
// if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction) // then we create a temp file that will be used as the source file in subsequent processes // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists) if (!$this->no_upload_check && empty($this->file_src_temp) && !file_exists($this->file_src_pathname)) { $this->log .= '- ' . _("attempting creating a temp file:"); $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->file_src_temp = $this->file_src_pathname; $this->log .= ' ' . _("file created") . '<br />'; $this->log .= ' ' . _("temp file is:") . ' ' . $this->file_src_temp . '<br />'; } else { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Can't create the temporary file. Can't carry on a process"); } }
if ($this->processed) {
if ($image_manipulation) {
// checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = _("Source file is not readable. Can't carry on a process"); } else { @fclose($f); }
// we now do all the image manipulations $this->log .= '- ' . _("image resizing or conversion wanted") . '<br />'; if ($this->gd_version()) { switch($this->file_src_mime) { case 'image/pjpeg': case 'image/jpeg': case 'image/jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = _("No create from JPEG support"); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("Error in creating JPEG image from source"); } else { $this->log .= '- ' . _("source image is JPEG") . '<br />'; } } break; case 'image/x-png': case 'image/png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = _("No create from PNG support"); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("Error in creating PNG image from source"); } else { $this->log .= '- ' . _("source image is PNG") . '<br />'; } } break; case 'image/gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = _("Error in creating GIF image from source"); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("No GIF read support"); } else { $this->log .= '- ' . _("source image is GIF") . '<br />'; } } break; default: $this->processed = false; $this->error = _("Can't read image source. not an image?"); } } else { $this->processed = false; $this->error = _("GD doesn't seem to be present"); }
if ($this->processed && $image_src) {
$this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; $gd_version = $this->gd_version(); $ratio_crop = null;
if ($this->image_resize) { $this->log .= '- ' . _("resizing...") . '<br />'; if ($this->image_ratio_x) { $this->log .= ' ' . _("calculate x size") . '<br />'; $this->image_dst_x = round(($this->image_src_x * $this->image_y) / $this->image_src_y); $this->image_dst_y = $this->image_y; } else if ($this->image_ratio_y) { $this->log .= ' ' . _("calculate y size") . '<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = round(($this->image_src_y * $this->image_x) / $this->image_src_x); } else if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) { $this->log .= ' ' . _("check x/y sizes") . '<br />'; if ((!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out) || ($this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y)) || ($this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y)) { $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; if ($this->image_ratio_crop) { if (!is_string($this->image_ratio_crop)) $this->image_ratio_crop = ''; $this->image_ratio_crop = strtolower($this->image_ratio_crop); if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_crop, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else if (strpos($this->image_ratio_crop, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x']/2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } $this->log .= ' ' . _("ratio_crop_x") . ' : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0); } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_crop, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else if (strpos($this->image_ratio_crop, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y']/2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } $this->log .= ' ' . _("ratio_crop_y") . ' : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0); } } else { if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x)); } else { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y)); } } } else { $this->log .= ' ' . _("doesn't calculate x/y sizes") . '<br />'; $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } } else { $this->log .= ' ' . _("use plain sizes") . '<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; }
if ($this->preserve_transparency && $this->file_src_mime != 'image/gif' && $this->file_src_mime != 'image/png') $this->preserve_transparency = false;
if ($gd_version >= 2 && !$this->preserve_transparency) { $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); } else { $image_dst = imagecreate($this->image_dst_x, $this->image_dst_y); } if ($this->preserve_transparency) { $this->log .= '- ' . _("preserve transparency") . '<br />'; $transparent_color = imagecolortransparent($image_src); imagepalettecopy($image_dst, $image_src); imagefill($image_dst, 0, 0, $transparent_color); imagecolortransparent($image_dst, $transparent_color); } if ($gd_version >= 2 && !$this->preserve_transparency) { $res = imagecopyresampled($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } else { $res = imagecopyresized($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); }
$this->log .= ' ' . _("resized image object created") . '<br />'; $this->log .= ' image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />'; $this->log .= ' image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />';
} else { // we only convert, so we link the dst image to the src image $image_dst = & $image_src; }
// we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= ' ' . _("setting destination file type to") . ' ' . $this->file_src_name_ext . '<br />'; $this->image_convert = $this->file_src_name_ext; }
// crop imag (and also crops if image_ratio_crop is used) if ($gd_version >= 2 && (!empty($this->image_crop) || !is_null($ratio_crop))) { if (is_array($this->image_crop)) { $vars = $this->image_crop; } else { $vars = explode(' ', $this->image_crop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } if (strpos($ct, '%')>0) $ct = $this->image_dst_y * (str_replace('%','',$ct) / 100); if (strpos($cr, '%')>0) $cr = $this->image_dst_x * (str_replace('%','',$cr) / 100); if (strpos($cb, '%')>0) $cb = $this->image_dst_y * (str_replace('%','',$cb) / 100); if (strpos($cl, '%')>0) $cl = $this->image_dst_x * (str_replace('%','',$cl) / 100); if (strpos($ct, 'px')>0) $ct = str_replace('px','',$ct); if (strpos($cr, 'px')>0) $cr = str_replace('px','',$cr); if (strpos($cb, 'px')>0) $cb = str_replace('px','',$cb); if (strpos($cl, 'px')>0) $cl = str_replace('px','',$cl); $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; // we adjust the cropping if we use image_ratio_crop if (!is_null($ratio_crop)) { if (array_key_exists('t', $ratio_crop)) $ct += $ratio_crop['t']; if (array_key_exists('r', $ratio_crop)) $cr += $ratio_crop['r']; if (array_key_exists('b', $ratio_crop)) $cb += $ratio_crop['b']; if (array_key_exists('l', $ratio_crop)) $cl += $ratio_crop['l']; } $this->log .= '- ' . _("crop image") . ' : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_dst_x = $this->image_dst_x - $cl - $cr; $this->image_dst_y = $this->image_dst_y - $ct - $cb; if ($this->image_dst_x < 1) $this->image_dst_x = 1; if ($this->image_dst_y < 1) $this->image_dst_y = 1;
$tmp=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y);
// we transfert tmp into image_dst imagedestroy($image_dst); $image_dst=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst,$tmp,0,0,0,0,$this->image_dst_x,$this->image_dst_y); imagedestroy($tmp); } // flip image if ($gd_version >= 2 && !empty($this->image_flip)) { $this->image_flip = strtolower($this->image_flip); $this->log .= '- ' . _("flip image") . ' : ' . $this->image_flip . '<br />'; $tmp=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++){ if (strpos($this->image_flip, 'v') !== false) { imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1); } } }
// we transfert tmp into image_dst imagedestroy($image_dst); $image_dst=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst,$tmp,0,0,0,0,$this->image_dst_x,$this->image_dst_y); imagedestroy($tmp); }
// rotate image if ($gd_version >= 2 && is_numeric($this->image_rotate)) { if (!in_array($this->image_rotate, array(0, 90, 180, 270))) $this->image_rotate = 0; if ($this->image_rotate != 0) { if ($this->image_rotate == 90 || $this->image_rotate == 270) { $tmp=imagecreatetruecolor($this->image_dst_y, $this->image_dst_x); } else { $tmp=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); } $this->log .= '- ' . _("rotate image") . ' : ' . $this->image_rotate . '<br />'; for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++){ if ($this->image_rotate == 90) { imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1); } else if ($this->image_rotate == 180) { imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1); } else if ($this->image_rotate == 270) { imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1); } } } if ($this->image_rotate == 90 || $this->image_rotate == 270) { $t = $this->image_dst_y; $this->image_dst_y = $this->image_dst_x; $this->image_dst_x = $t; } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst,$tmp,0,0,0,0,$this->image_dst_x,$this->image_dst_y); imagedestroy($tmp); } }
// add color overlay if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && !empty($this->image_overlay_color))) { $this->log .= '- ' . _("apply color overlay") . '<br />'; sscanf($this->image_overlay_color, "#%2x%2x%2x", $red, $green, $blue); $filter=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $color=imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color); imagecopymerge($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent); imagedestroy($filter); }
// add brightness, contrast and tint, turns to greyscale and inverts colors if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold)|| is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) { $this->log .= '- ' . _("apply tint, light, contrast correction, negative, greyscale and threshold") . '<br />'; if (!empty($this->image_tint_color)) sscanf($this->image_tint_color, "#%2x%2x%2x", $red, $green, $blue); $background = imagecolorallocatealpha($image_dst, 255, 255, 255, 0); imagefill($image_dst, 0, 0, $background); imagealphablending($image_dst, TRUE); for($y=0; $y < $this->image_dst_y; $y++) { for($x=0; $x < $this->image_dst_x; $x++) { if ($this->image_greyscale) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = $g = $b = round((0.2125 * $pixel['red']) + (0.7154 * $pixel['green']) + (0.0721 * $pixel['blue'])); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_threshold)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $c = (round($pixel['red']+$pixel['green']+$pixel['blue'])/3) - 127; $r = $g = $b = ($c > $this->image_threshold ? 255 : 0); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_brightness)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = max(min(round($pixel['red']+(($this->image_brightness*2))),255),0); $g = max(min(round($pixel['green']+(($this->image_brightness*2))),255),0); $b = max(min(round($pixel['blue']+(($this->image_brightness*2))),255),0); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_contrast)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = max(min(round(($this->image_contrast+128)*$pixel['red']/128),255),0); $g = max(min(round(($this->image_contrast+128)*$pixel['green']/128),255),0); $b = max(min(round(($this->image_contrast+128)*$pixel['blue']/128),255),0); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (!empty($this->image_tint_color)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = min(round($red*$pixel['red']/169),255); $g = min(round($green*$pixel['green']/169),255); $b = min(round($blue*$pixel['blue']/169),255); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (!empty($this->image_negative)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = round(255-$pixel['red']); $g = round(255-$pixel['green']); $b = round(255-$pixel['blue']); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } } } }
// adds a border if ($gd_version >= 2 && !empty($this->image_border)) { if (is_array($this->image_border)) { $vars = $this->image_border; $this->log .= '- ' . _("add border") . ' : ' . implode(' ', $this->image_border) . '<br />'; } else { $this->log .= '- ' . _("add border") . ' : ' . $this->image_border . '<br />'; $vars = explode(' ', $this->image_border); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } if (strpos($ct, '%')>0) $ct = $this->image_dst_y * (str_replace('%','',$ct) / 100); if (strpos($cr, '%')>0) $cr = $this->image_dst_x * (str_replace('%','',$cr) / 100); if (strpos($cb, '%')>0) $cb = $this->image_dst_y * (str_replace('%','',$cb) / 100); if (strpos($cl, '%')>0) $cl = $this->image_dst_x * (str_replace('%','',$cl) / 100); if (strpos($ct, 'px')>0) $ct = str_replace('px','',$ct); if (strpos($cr, 'px')>0) $cr = str_replace('px','',$cr); if (strpos($cb, 'px')>0) $cb = str_replace('px','',$cb); if (strpos($cl, 'px')>0) $cl = str_replace('px','',$cl); $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->image_dst_x = $this->image_dst_x + $cl + $cr; $this->image_dst_y = $this->image_dst_y + $ct + $cb; if (!empty($this->image_border_color)) sscanf($this->image_border_color, "#%2x%2x%2x", $red, $green, $blue);
$tmp=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0); imagefill($tmp, 0, 0, $background); imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct); // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst,$tmp,0,0,0,0,$this->image_dst_x,$this->image_dst_y); imagedestroy($tmp); } // add frame border if (is_numeric($this->image_frame)) { if (is_array($this->image_frame_colors)) { $vars = $this->image_frame_colors; $this->log .= '- ' . _("add frame") . ' : ' . implode(' ', $this->image_frame_colors) . '<br />'; } else { $this->log .= '- ' . _("add frame") . ' : ' . $this->image_frame_colors . '<br />'; $vars = explode(' ', $this->image_frame_colors); }
$nb = sizeof($vars); $this->image_dst_x = $this->image_dst_x + ($nb * 2); $this->image_dst_y = $this->image_dst_y + ($nb * 2); $tmp=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - ($nb * 2), $this->image_dst_y - ($nb * 2)); for ($i=0; $i<$nb; $i++) { sscanf($vars[$i], "#%2x%2x%2x", $red, $green, $blue); $c = imagecolorallocate($tmp, $red, $green, $blue); if ($this->image_frame == 1) { imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c); imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $this->image_dst_x - $i -1, $i, $c); imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c); } else { imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c); } } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst,$tmp,0,0,0,0,$this->image_dst_x,$this->image_dst_y); imagedestroy($tmp); }
// add bevel border if ($this->image_bevel > 0) { if (empty($this->image_bevel_color1)) $this->image_bevel_color1 = '#FFFFFF'; if (empty($this->image_bevel_color2)) $this->image_bevel_color2 = '#000000'; sscanf($this->image_bevel_color1, "#%2x%2x%2x", $red1, $green1, $blue1); sscanf($this->image_bevel_color2, "#%2x%2x%2x", $red2, $green2, $blue2); imagealphablending($image_dst, true); for ($i=0; $i<$this->image_bevel; $i++) { $alpha = round(($i / $this->image_bevel) * 127); $c1 = imagecolorallocatealpha($image_dst, $red1, $green1, $blue1, $alpha); $c2 = imagecolorallocatealpha($image_dst, $red2, $green2, $blue2, $alpha); imageline($image_dst, $i, $i, $this->image_dst_x - $i -1, $i, $c1); imageline($image_dst, $this->image_dst_x - $i -1, $this->image_dst_y - $i, $this->image_dst_x - $i -1, $i, $c2); imageline($image_dst, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c2); imageline($image_dst, $i, $i, $i, $this->image_dst_y - $i -1, $c1); } }
// add watermark image if ($this->image_watermark!='' && file_exists($this->image_watermark)) { $this->log .= '- ' . _("add watermark") . '<br />'; $this->image_watermark_position = strtolower($this->image_watermark_position); $watermark_info = getimagesize($this->image_watermark); $watermark_type = (array_key_exists(2, $watermark_info) ? $watermark_info[2] : NULL); // 1 = GIF, 2 = JPG, 3 = PNG $watermark_checked = false;
if ($watermark_type == 1) { if (!function_exists('imagecreatefromgif')) { $this->error = _("No create from GIF support, can't read watermark"); } else { $filter = @imagecreatefromgif($this->image_watermark); if (!$filter) { $this->error = _("No GIF read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is GIF") . '<br />'; $watermark_checked = true; } } } else if ($watermark_type == 2) { if (!function_exists('imagecreatefromjpeg')) { $this->error = _("No create from JPG support, can't read watermark"); } else { $filter = @imagecreatefromjpeg($this->image_watermark); if (!$filter) { $this->error = _("No JPG read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is JPG") . '<br />'; $watermark_checked = true; } } } else if ($watermark_type == 3) { if (!function_exists('imagecreatefrompng')) { $this->error = _("No create from PNG support, can't read watermark"); } else { $filter = @imagecreatefrompng($this->image_watermark); if (!$filter) { $this->error = _("No PNG read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is PNG") . '<br />'; $watermark_checked = true; } } } if ($watermark_checked) { $watermark_width = imagesx($filter); $watermark_height = imagesy($filter); $watermark_x = 0; $watermark_y = 0; if (is_numeric($this->image_watermark_x)) { if ($this->image_watermark_x < 0) { $watermark_x = $this->image_dst_x - $watermark_width + $this->image_watermark_x; } else { $watermark_x = $this->image_watermark_x; } } else { if (strpos($this->image_watermark_position, 'r') !== false) { $watermark_x = $this->image_dst_x - $watermark_width; } else if (strpos($this->image_watermark_position, 'l') !== false) { $watermark_x = 0; } else { $watermark_x = ($this->image_dst_x - $watermark_width) / 2; } } if (is_numeric($this->image_watermark_y)) { if ($this->image_watermark_y < 0) { $watermark_y = $this->image_dst_y - $watermark_height + $this->image_watermark_y; } else { $watermark_y = $this->image_watermark_y; } } else { if (strpos($this->image_watermark_position, 'b') !== false) { $watermark_y = $this->image_dst_y - $watermark_height; } else if (strpos($this->image_watermark_position, 't') !== false) { $watermark_y = 0; } else { $watermark_y = ($this->image_dst_y - $watermark_height) / 2; } } imagecopyresampled ($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height); } else { $this->error = _("Watermark image is of unknown type"); } }
// add text if (!empty($this->image_text)) { $this->log .= '- ' . _("add text") . '<br />'; if (!is_numeric($this->image_text_padding)) $this->image_text_padding = 0; if (!is_numeric($this->image_text_line_spacing)) $this->image_text_line_spacing = 0; if (!is_numeric($this->image_text_padding_x)) $this->image_text_padding_x = $this->image_text_padding; if (!is_numeric($this->image_text_padding_y)) $this->image_text_padding_y = $this->image_text_padding; $this->image_text_position = strtolower($this->image_text_position); $this->image_text_direction = strtolower($this->image_text_direction); $this->image_text_alignment = strtolower($this->image_text_alignment); // if the font is a string, we assume that we might want to load a font if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') { $this->log .= ' ' . _("try to load font") . ' ' . $this->image_text_font . '... '; if ($this->image_text_font = @imageloadfont($this->image_text_font)) { $this->log .= _("success") . '<br />'; } else { $this->log .= _("error") . '<br />'; $this->image_text_font = 5; } } $text = explode("\n", $this->image_text); $char_width = ImageFontWidth($this->image_text_font); $char_height = ImageFontHeight($this->image_text_font); $text_height = 0; $text_width = 0; $line_height = 0; $line_width = 0; foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { $h = ($char_width * strlen($v)); if ($h > $text_height) $text_height = $h; $line_width = $char_height; $text_width += $line_width + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0); } else { $w = ($char_width * strlen($v)); if ($w > $text_width) $text_width = $w; $line_height = $char_height; $text_height += $line_height + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0); } } $text_width += (2 * $this->image_text_padding_x); $text_height += (2 * $this->image_text_padding_y); $text_x = 0; $text_y = 0; if (is_numeric($this->image_text_x)) { if ($this->image_text_x < 0) { $text_x = $this->image_dst_x - $text_width + $this->image_text_x; } else { $text_x = $this->image_text_x; } } else { if (strpos($this->image_text_position, 'r') !== false) { $text_x = $this->image_dst_x - $text_width; } else if (strpos($this->image_text_position, 'l') !== false) { $text_x = 0; } else { $text_x = ($this->image_dst_x - $text_width) / 2; } } if (is_numeric($this->image_text_y)) { if ($this->image_text_y < 0) { $text_y = $this->image_dst_y - $text_height + $this->image_text_y; } else { $text_y = $this->image_text_y; } } else { if (strpos($this->image_text_position, 'b') !== false) { $text_y = $this->image_dst_y - $text_height; } else if (strpos($this->image_text_position, 't') !== false) { $text_y = 0; } else { $text_y = ($this->image_dst_y - $text_height) / 2; } } // add a background, maybe transparent if (!empty($this->image_text_background)) { sscanf($this->image_text_background, "#%2x%2x%2x", $red, $green, $blue); if ($gd_version >= 2 && (is_numeric($this->image_text_background_percent)) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) { $filter=imagecreatetruecolor($text_width, $text_height); $background_color=imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color); imagecopymerge($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent); imagedestroy($filter); } else { $background_color = imageColorAllocate($image_dst ,$red, $green, $blue); imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color); } }
$text_x += $this->image_text_padding_x; $text_y += $this->image_text_padding_y; $t_width = $text_width - (2 * $this->image_text_padding_x); $t_height = $text_height - (2 * $this->image_text_padding_y); sscanf($this->image_text_color, "#%2x%2x%2x", $red, $green, $blue);
// add the text, maybe transparent if ($gd_version >= 2 && (is_numeric($this->image_text_percent)) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) { if ($t_width < 0) $t_width = 0; if ($t_height < 0) $t_height = 0; $filter=imagecreatetruecolor($t_width, $t_height); $color = imagecolorallocate($filter, 0, 0, ($blue == 0 ? 255 : 0)); imagefill($filter, 0, 0, $color); $text_color = imagecolorallocate($filter ,$red, $green, $blue); imagecolortransparent($filter, $color); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)), $text_height - (2 * $this->image_text_padding_y) - ($this->image_text_alignment == 'l' ? 0 : (($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))) , $v, $text_color); } else { imagestring($filter, $this->image_text_font, ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))), $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } imagecopymerge($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent); imagedestroy($filter); } else { $text_color = imageColorAllocate($image_dst ,$red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - (2 * $this->image_text_padding_y) - ($this->image_text_alignment == 'l' ? 0 : (($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))), $v, $text_color); } else { imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))), $text_y + $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } }
}
// add a reflection if ($this->image_reflection_height) { $this->log .= '- ' . _("add reflection") . ' : ' . $this->image_reflection_height . '<br />'; // we decode image_reflection_height, which can be a integer, a string in pixels or percentage $image_reflection_height = $this->image_reflection_height; if (strpos($image_reflection_height, '%')>0) $image_reflection_height = $this->image_dst_y * (str_replace('%','',$image_reflection_height / 100)); if (strpos($image_reflection_height, 'px')>0) $image_reflection_height = str_replace('px','',$image_reflection_height); $image_reflection_height = (int) $image_reflection_height; if ($image_reflection_height > $this->image_dst_y) $image_reflection_height = $this->image_dst_y; if (empty($this->image_reflection_color)) $this->image_reflection_color = '#FFFFFF'; if (empty($this->image_reflection_opacity)) $this->image_reflection_opacity = 60; // create the new destination image $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space); sscanf($this->image_reflection_color, "#%2x%2x%2x", $red, $green, $blue); $color = imagecolorallocate($tmp, $red, $green, $blue); imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, $color); $transparency = $this->image_reflection_opacity; // copy the original image imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)); // copy the reflection for ($y = 0; $y < $image_reflection_height; $y++) { imagecopymerge($tmp, $image_dst, 0, $y + $this->image_dst_y + $this->image_reflection_space, 0, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0), $this->image_dst_x, 1, (int) $transparency); if ($transparency > 0) $transparency = $transparency - ($this->image_reflection_opacity / $image_reflection_height); } // copy the resulting image into the destination image $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space; imagedestroy($image_dst); $image_dst=imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst,$tmp,0,0,0,0,$this->image_dst_x,$this->image_dst_y); imagedestroy($tmp); }
if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) { // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net $this->log .= '- ' . _("JPEG desired file size") . ' : ' . $this->jpeg_size . '<br />'; //calculate size of each image. 75%, 50%, and 25% quality ob_start(); imagejpeg($image_dst,'',75); $buffer = ob_get_contents(); ob_end_clean(); $size75 = strlen($buffer); ob_start(); imagejpeg($image_dst,'',50); $buffer = ob_get_contents(); ob_end_clean(); $size50 = strlen($buffer); ob_start(); imagejpeg($image_dst,'',25); $buffer = ob_get_contents(); ob_end_clean(); $size25 = strlen($buffer); //calculate gradient of size reduction by quality $mgrad1 = 25/($size50-$size25); $mgrad2 = 25/($size75-$size50); $mgrad3 = 50/($size75-$size25); $mgrad = ($mgrad1+$mgrad2+$mgrad3)/3; //result of approx. quality factor for expected size $q_factor=round($mgrad*($this->jpeg_size-$size50)+50); if ($q_factor<1) { $this->jpeg_quality=1; } elseif ($q_factor>100) { $this->jpeg_quality=100; } else { $this->jpeg_quality=$q_factor; } $this->log .= ' ' . _("JPEG quality factor set to") . ' ' . $this->jpeg_quality . '<br />'; }
// outputs image $this->log .= '- ' . _("converting..") . '<br />'; switch($this->image_convert) { case 'jpeg': case 'jpg': $result = @imagejpeg ($image_dst, $this->file_dst_pathname, $this->jpeg_quality); if (!$result) { $this->processed = false; $this->error = _("No JPEG create support"); } else { $this->log .= ' ' . _("JPEG image created") . '<br />'; } break; case 'png': $result = @imagepng ($image_dst, $this->file_dst_pathname); if (!$result) { $this->processed = false; $this->error = _("No PNG create support"); } else { $this->log .= ' ' . _("PNG image created") . '<br />'; } break; case 'gif': $result = @imagegif ($image_dst, $this->file_dst_pathname); if (!$result) { $this->processed = false; $this->error = _("No GIF create support"); } else { $this->log .= ' ' . _("GIF image created") . '<br />'; } break; default: $this->processed = false; $this->error = _("No convertion type defined"); } if ($this->processed) { if (is_resource($image_src)) imagedestroy($image_src); if (is_resource($image_dst)) imagedestroy($image_dst); $this->log .= ' ' . _("image objects destroyed") . '<br />'; } }
} else { $this->log .= '- ' . _("no image processing wanted") . '<br />';
// copy the file to its final destination. we don;t use move_uploaded_file here // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file if (!copy($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = _("Error copying file on the server. copy() failed"); } } }
if ($this->processed) { $this->log .= '- <b>' . _("process OK") . '</b><br />';
} // we reinit all the var $this->init();
}
/** * Deletes the uploaded file from its temporary location * * When PHP uploads a file, it stores it in a temporary location. * When you {@link process} the file, you actually copy the resulting file to the given location, it doesn't alter the original file. * Once you have processed the file as many times as you wanted, you can delete the uploaded file. * If there is open_basedir restrictions, the uploaded file is in fact a temporary file * * You might want not to use this function if you work on local files, as it will delete the source file * * @access public */ function clean() { $this->log .= '<b>' . _("cleanup") . '</b><br />'; $this->log .= '- ' . _("delete temp file") . ' ' . $this->file_src_pathname . '<br />'; @unlink($this->file_src_pathname); }
}
// i18n gettext compatibility if (!function_exists("_")) { function _($str) { return $str; } }
?>
|