Sponsorenverwaltung - Team StarCraft e.V.
 All Data Structures Files Functions Variables
img.php
Go to the documentation of this file.
1 <?php
2  /**
3  * @file img.php
4  *
5  * @brief Handles scaling and image loading
6  *
7  * @details
8  * To specify the image to load use http get var 'file'.
9  * To specify the maximum height and maximum width use http get vars 'max_height'
10  * and 'max_width'. Scaled photos are stored in a subfolder '_scaled' for further use.
11  * Output format is png.
12  *
13  * @copyright (c) 2013, Team StarCraft e.V.
14  * @version 1.0.0
15  * @author Daniel Seichter
16  * @date 02.07.2013
17  */
18 
19  // get absolute path to the script root
20  //! used to get absolute path to the script root
21  $absPath = str_replace('\\','/',realpath(dirname(__FILE__))).'/';
22  $absPath = str_replace('//','/',str_replace('inc','',$absPath));
23  $absPath = (is_dir($absPath)) ? $absPath : '../';
24 
25  //! absolute path to the script root
26  define('ABS_PATH', $absPath);
27 
28  //! image to load on error
29  define('NO_PREVIEW', 'inc/img/no_preview.png');
30 
31  /**
32  * Loads image depending on the type
33  * @param string $path path to the image
34  * @return mixed
35  * - img if the image was loaded
36  * - FALSE if an error occured
37  * @author Daniel Seichter
38  */
39  function loadImage($path) {
40  if (file_exists($path)) {
41  // read image infos (size, type)
42  list($imgWidth, $imgHeight, $imgType) = getImageSize($path);
43  switch($imgType) {
44  case IMAGETYPE_GIF:
45  $img = ImageCreateFromGIF($path);
46  break;
47  case IMAGETYPE_JPEG:
48  $img = ImageCreateFromJPEG($path);
49  break;
50  case IMAGETYPE_PNG:
51  $img = ImageCreateFromPNG($path);
52  break;
53  }
54  return isset($img) ? $img : FALSE;
55  }
56  else {
57  return FALSE;
58  }
59  }
60 
61  /**
62  * Resize image depending on the type (and handle transparancy)
63  * @param string $path path to the image
64  * @param integer $width maximum width for scaled image
65  * @param integer $height maximum height for scaled image
66  * @return mixed
67  * - img if the image was loaded
68  * - FALSE if an error occured
69  * @author Daniel Seichter
70  */
71  function resizeImage($path, $width, $height) {
72  $img = loadImage($path);
73  if ($img) {
74  // read image infos (size, type)
75  list($imgWidth, $imgHeight, $imgType) = getImageSize($path);
76  $imgRatio = $imgWidth/$imgHeight;
77 
78  // calc new image size
79  if (($width < $imgWidth) || ($height < $imgHeight)) {
80  if ($width/$height > $imgRatio) {
81  $width = $height * $imgRatio;
82  }
83  else {
84  $height = $width / $imgRatio;
85  }
86  }
87  else {
88  $width = $imgWidth;
89  $height = $imgHeight;
90  }
91  // create resized image
92  $newImg = ImageCreateTRUEColor($width, $height);
93  // handle transparancy
94  if (($imgType == IMAGETYPE_GIF) || ($imgType == IMAGETYPE_PNG)) {
95  $transparentIndex = imageColorTransparent($img);
96  if ($transparentIndex >= 0) {
97  $transparentColor = imageColorsForIndex($img, $transparentIndex);
98  $transparentIndex = imageColorAllocate($newImg, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
99  imageFill($newImg, 0, 0, $transparentIndex);
100  imageColorTransparent($newImg, $transparentIndex);
101  }
102  else {
103  if ($imgType == IMAGETYPE_PNG) {
104  imageAlphaBlending($newImg, FALSE);
105  $color = imageColorAllocateAlpha($newImg, 0, 0, 0, 127);
106  imageFill($newImg, 0, 0, $color);
107  imageSaveAlpha($newImg, TRUE);
108  }
109  }
110  }
111 
112  // copy image
113  ImageCopyResampled($newImg, $img, 0, 0, 0 , 0, $width, $height, $imgWidth, $imgHeight);
114  imageDestroy($img);
115  return $newImg;
116  }
117  else {
118  return FALSE;
119  }
120  }
121 
122  /**
123  * Save image depending on the type
124  * @param mixed $img image ressource
125  * @param string $path path for saving (including filename and extension)
126  * @param integer $extension
127  * - IMAGETYPE_JPEG
128  * - IMAGETYPE_GIF
129  * - IMAGETYPE_PNG
130  * @param integer $quality [optional]
131  * - quality (only used if $extension is IMAGETYPE_JPEG)
132  * - Default: 100
133  * @return boolean on success TRUE else FALSE if an error occured
134  * @author Daniel Seichter
135  */
136  function saveImage($img, $path, $extension, $quality=100) {
137  switch ($extension) {
138  case IMAGETYPE_JPEG:
139  return imageJpeg($img,$path,$quality);
140  break;
141  case IMAGETYPE_GIF:
142  return imageGif($img,$path);
143  break;
144  case IMAGETYPE_PNG:
145  return imagePng($img,$path);
146  break;
147  }
148  }
149 
150 
151  // get params
152  //! holds maximum height
153  $maxHeight = (isset($_GET['max_height']) && ctype_digit($_GET['max_height'])) ? $_GET['max_height'] : 60;
154  //! holds maximum width
155  $maxWidth = (isset($_GET['max_width']) && ctype_digit($_GET['max_width'])) ? $_GET['max_width'] : 120;
156  //! holds file to load
157  $filename = $_GET['file'];
158 
159  /// @cond MAINPART
160  // set header information
161  header("Content-Type: image/png");
162 
163  if (!empty($filename)) {
164  $pathParts = pathInfo($filename);
165  $scaledImgFilename = $pathParts['dirname'].'/_scaled/'.$pathParts['filename'].'_'.$maxWidth.'x'.$maxHeight.'.png';
166 
167  // check if scaled image already
168  if (file_exists(ABS_PATH.$scaledImgFilename)) {
169  // load and display scaled image
170  $img = loadImage(ABS_PATH.$scaledImgFilename);
171  imageAlphaBlending($img, FALSE);
172  imageSaveAlpha($img, TRUE);
173  imagePng($img);
174  }
175  else {
176  if (file_exists(ABS_PATH.$filename)) {
177  // load, scale and display image
178  $img = resizeImage(ABS_PATH.$filename, $maxWidth, $maxHeight);
179  saveImage($img, ABS_PATH.$scaledImgFilename, IMAGETYPE_PNG);
180  imagePng($img);
181  imageDestroy($img);
182  }
183  else {
184  // load and display error image
185  $img = loadImage(ABS_PATH.NO_PREVIEW);
186  imageAlphaBlending($img, FALSE);
187  imageSaveAlpha($img, TRUE);
188  imagePng($img);
189  }
190  }
191  }
192  else {
193  // load and display error image
194  $img = loadImage(ABS_PATH.NO_PREVIEW);
195  imageAlphaBlending($img, FALSE);
196  imageSaveAlpha($img, TRUE);
197  imagePng($img);
198  }
199  /// @endcond
200 ?>