1+ <?php
2+ /**
3+ * View image header
4+ *
5+ * @return mixed
6+ */
7+ function view_image (){
8+ ini_set ('gd.jpeg_ignore_warning ' , true );
9+ $ path = $ _GET ["path " ];
10+
11+ if (@getimagesize ($ path ) === FALSE ){
12+ $ path = getcwd ().'/assets/images/default.jpg ' ;
13+ }
14+
15+ //getting extension type (jpg, png, etc)
16+ $ type = explode (". " , $ path );
17+ $ ext = strtolower ($ type [sizeof ($ type )-1 ]);
18+
19+ $ ext = (!in_array ($ ext , array ("jpeg " ,"png " ,"gif " ))) ? "jpeg " : $ ext ;
20+
21+ //get image size
22+ $ size = getimagesize ($ path );
23+
24+ $ width = $ size [0 ];
25+ $ height = $ size [1 ];
26+
27+ //get source image
28+ $ func = "imagecreatefrom " .$ ext ;
29+ $ source = $ func ($ path );
30+
31+ //setting default values
32+
33+ $ new_width = $ width ;
34+ $ new_height = $ height ;
35+ $ k_w = 1 ;
36+ $ k_h = 1 ;
37+ $ dst_x =0 ;
38+ $ dst_y =0 ;
39+ $ src_x =0 ;
40+ $ src_y =0 ;
41+
42+ //selecting width and height
43+
44+ if ($ _GET ["width " ]==null && $ _GET ["height " ]==null )
45+ {
46+ $ new_height = $ height ;
47+ $ new_width = $ width ;
48+ }
49+ else if ($ _GET ["width " ]==null )
50+ {
51+ $ new_height = $ _GET ["height " ];
52+ $ new_width = ($ _GET ["height " ])/$ height ;
53+ }
54+ else if ($ _GET ["height " ]==null )
55+ {
56+ $ new_height = ($ height *$ _GET ["width " ])/$ width ;
57+ $ new_width = $ _GET ["width " ];
58+ }
59+ else
60+ {
61+ $ new_width = $ _GET ["width " ];
62+ $ new_height = $ _GET ["height " ];
63+ }
64+
65+ //secelcting_offsets
66+
67+ if ($ new_width >$ width )//by width
68+ {
69+ $ dst_x = ($ new_width -$ width )/2 ;
70+ }
71+ if ($ new_height >$ height )//by height
72+ {
73+ $ dst_y = ($ new_height -$ height )/2 ;
74+ }
75+ if ( $ new_width <$ width || $ new_height <$ height )
76+ {
77+ $ k_w = $ new_width /$ width ;
78+ $ k_h = $ new_height /$ height ;
79+
80+ if ($ new_height >$ height )
81+ {
82+ $ src_x = ($ width -$ new_width )/2 ;
83+ }
84+ else if ($ new_width >$ width )
85+ {
86+ $ src_y = ($ height -$ new_height )/2 ;
87+ }
88+ else
89+ {
90+ if ($ k_h >$ k_w )
91+ {
92+ $ src_x = round (($ width -($ new_width /$ k_h ))/2 );
93+ }
94+ else
95+ {
96+ $ src_y = round (($ height -($ new_height /$ k_w ))/2 );
97+ }
98+ }
99+ }
100+ $ output = imagecreatetruecolor ( $ new_width , $ new_height );
101+
102+ //to preserve PNG transparency
103+ if ($ ext == "png " )
104+ {
105+ //saving all full alpha channel information
106+ imagesavealpha ($ output , true );
107+ //setting completely transparent color
108+ $ transparent = imagecolorallocatealpha ($ output , 0 , 0 , 0 , 127 );
109+ //filling created image with transparent color
110+ imagefill ($ output , 0 , 0 , $ transparent );
111+ }
112+
113+ imagecopyresampled ( $ output , $ source , $ dst_x , $ dst_y , $ src_x , $ src_y ,
114+ $ new_width -2 *$ dst_x , $ new_height -2 *$ dst_y ,
115+ $ width -2 *$ src_x , $ height -2 *$ src_y );
116+ //free resources
117+ ImageDestroy ($ source );
118+
119+ //output image
120+ header ('Content-Disposition: inline ' );
121+ header ('Content-Type: image/ ' .$ ext );
122+ $ func = "image " .$ ext ;
123+ $ func ($ output );
124+
125+ //free resources
126+ ImageDestroy ($ output );
127+ exit ();
128+ }
129+
130+
131+ view_image ();
0 commit comments