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