PageRenderTime 84ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/modules/fileManager/extensions/upload/Upload.php

https://bitbucket.org/graaaf/erso
PHP | 5139 lines | 2601 code | 321 blank | 2217 comment | 859 complexity | a717db24ca507be071cde3fd3fe22a25 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. // +------------------------------------------------------------------------+
  3. // | class.upload.php |
  4. // +------------------------------------------------------------------------+
  5. // | Copyright (c) Colin Verot 2003-2010. All rights reserved. |
  6. // | Version 0.31 |
  7. // | Last modified 11/04/2011 |
  8. // | Email colin@verot.net |
  9. // | Web http://www.verot.net |
  10. // +------------------------------------------------------------------------+
  11. // | This program is free software; you can redistribute it and/or modify |
  12. // | it under the terms of the GNU General Public License version 2 as |
  13. // | published by the Free Software Foundation. |
  14. // | |
  15. // | This program is distributed in the hope that it will be useful, |
  16. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. // | GNU General Public License for more details. |
  19. // | |
  20. // | You should have received a copy of the GNU General Public License |
  21. // | along with this program; if not, write to the |
  22. // | Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
  23. // | Boston, MA 02111-1307 USA |
  24. // | |
  25. // | Please give credit on sites that use class.upload and submit changes |
  26. // | of the script so other people can use them as well. |
  27. // | This script is free to use, don't abuse. |
  28. // +------------------------------------------------------------------------+
  29. //
  30. /**
  31. * Class upload
  32. *
  33. * @version 0.31
  34. * @author Colin Verot <colin@verot.net>
  35. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  36. * @copyright Colin Verot
  37. * @package cmf
  38. * @subpackage external
  39. */
  40. /**
  41. * Class upload
  42. *
  43. * <b>What does it do?</b>
  44. *
  45. * It manages file uploads for you. In short, it manages the uploaded file,
  46. * and allows you to do whatever you want with the file, especially if it
  47. * is an image, and as many times as you want.
  48. *
  49. * It is the ideal class to quickly integrate file upload in your site.
  50. * If the file is an image, you can convert, resize, crop it in many ways.
  51. * You can also apply filters, add borders, text, watermarks, etc...
  52. * That's all you need for a gallery script for instance. Supported formats
  53. * are PNG, JPG, GIF and BMP.
  54. *
  55. * You can also use the class to work on local files, which is especially
  56. * useful to use the image manipulation features. The class also supports
  57. * Flash uploaders.
  58. *
  59. * The class works with PHP 4 and 5, and its error messages can
  60. * be localized at will.
  61. *
  62. * <b>How does it work?</b>
  63. *
  64. * You instanciate the class with the $_FILES['my_field'] array
  65. * where my_field is the field name from your upload form.
  66. * The class will check if the original file has been uploaded
  67. * to its temporary location (alternatively, you can instanciate
  68. * the class with a local filename).
  69. *
  70. * You can then set a number of processing variables to act on the file.
  71. * For instance, you can rename the file, and if it is an image,
  72. * convert and resize it in many ways.
  73. * You can also set what will the class do if the file already exists.
  74. *
  75. * Then you call the function {@link process} to actually perform the actions
  76. * according to the processing parameters you set above.
  77. * It will create new instances of the original file,
  78. * so the original file remains the same between each process.
  79. * The file will be manipulated, and copied to the given location.
  80. * The processing variables will be reset once it is done.
  81. *
  82. * You can repeat setting up a new set of processing variables,
  83. * and calling {@link process} again as many times as you want.
  84. * When you have finished, you can call {@link clean} to delete
  85. * the original uploaded file.
  86. *
  87. * If you don't set any processing parameters and call {@link process}
  88. * just after instanciating the class. The uploaded file will be simply
  89. * copied to the given location without any alteration or checks.
  90. *
  91. * Don't forget to add <i>enctype="multipart/form-data"</i> in your form
  92. * tag <form> if you want your form to upload the file.
  93. *
  94. * <b>How to use it?</b><br>
  95. * Create a simple HTML file, with a form such as:
  96. * <pre>
  97. * <form enctype="multipart/form-data" method="post" action="upload.php">
  98. * <input type="file" size="32" name="image_field" value="">
  99. * <input type="submit" name="Submit" value="upload">
  100. * </form>
  101. * </pre>
  102. * Create a file called upload.php:
  103. * <pre>
  104. * $handle = new Upload($_FILES['image_field']);
  105. * if ($handle->uploaded) {
  106. * $handle->file_new_name_body = 'image_resized';
  107. * $handle->image_resize = true;
  108. * $handle->image_x = 100;
  109. * $handle->image_ratio_y = true;
  110. * $handle->process('/home/user/files/');
  111. * if ($handle->processed) {
  112. * echo 'image resized';
  113. * $handle->clean();
  114. * } else {
  115. * echo 'error : ' . $handle->error;
  116. * }
  117. * }
  118. * </pre>
  119. *
  120. * <b>How to process local files?</b><br>
  121. * Use the class as following, the rest being the same as above:
  122. * <pre>
  123. * $handle = new Upload('/home/user/myfile.jpg');
  124. * </pre>
  125. *
  126. * <b>How to set the language?</b><br>
  127. * Instantiate the class with a second argument being the language code:
  128. * <pre>
  129. * $handle = new Upload($_FILES['image_field'], 'fr_FR');
  130. * $handle = new Upload('/home/user/myfile.jpg', 'fr_FR');
  131. * </pre>
  132. *
  133. * <b>How to output the resulting file or picture directly to the browser?</b><br>
  134. * Simply call {@link process}() without an argument (or with null as first argument):
  135. * <pre>
  136. * $handle = new Upload($_FILES['image_field']);
  137. * header('Content-type: ' . $handle->file_src_mime);
  138. * echo $handle->Process();
  139. * die();
  140. * </pre>
  141. * Or if you want to force the download of the file:
  142. * <pre>
  143. * $handle = new Upload($_FILES['image_field']);
  144. * header('Content-type: ' . $handle->file_src_mime);
  145. * header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
  146. * echo $handle->Process();
  147. * die();
  148. * </pre>
  149. *
  150. * <b>Processing parameters</b> (reset after each process)
  151. * <ul>
  152. * <li><b>{@link file_new_name_body}</b> replaces the name body (default: null)<br>
  153. * <pre>$handle->file_new_name_body = 'new name';</pre></li>
  154. * <li><b>{@link file_name_body_add}</b> appends to the name body (default: null)<br>
  155. * <pre>$handle->file_name_body_add = '_uploaded';</pre></li>
  156. * <li><b>{@link file_name_body_pre}</b> prepends to the name body (default: null)<br>
  157. * <pre>$handle->file_name_body_pre = 'thumb_';</pre></li>
  158. * <li><b>{@link file_new_name_ext}</b> replaces the file extension (default: null)<br>
  159. * <pre>$handle->file_new_name_ext = 'txt';</pre></li>
  160. * <li><b>{@link file_safe_name}</b> formats the filename (spaces changed to _) (default: true)<br>
  161. * <pre>$handle->file_safe_name = true;</pre></li>
  162. * <li><b>{@link file_force_extension}</b> forces an extension if there is't any (default: true)<br>
  163. * <pre>$handle->file_force_extension = true;</pre></li>
  164. * <li><b>{@link file_overwrite}</b> sets behaviour if file already exists (default: false)<br>
  165. * <pre>$handle->file_overwrite = true;</pre></li>
  166. * <li><b>{@link file_auto_rename}</b> automatically renames file if it already exists (default: true)<br>
  167. * <pre>$handle->file_auto_rename = true;</pre></li>
  168. * <li><b>{@link dir_auto_create}</b> automatically creates destination directory if missing (default: true)<br>
  169. * <pre>$handle->auto_create_dir = true;</pre></li>
  170. * <li><b>{@link dir_auto_chmod}</b> automatically attempts to chmod the destination directory if not writeable (default: true)<br>
  171. * <pre>$handle->dir_auto_chmod = true;</pre></li>
  172. * <li><b>{@link dir_chmod}</b> chmod used when creating directory or if directory not writeable (default: 0777)<br>
  173. * <pre>$handle->dir_chmod = 0777;</pre></li>
  174. * <li><b>{@link file_max_size}</b> sets maximum upload size (default: upload_max_filesize from php.ini)<br>
  175. * <pre>$handle->file_max_size = '1024'; // 1KB</pre></li>
  176. * <li><b>{@link mime_check}</b> sets if the class check the MIME against the {@link allowed} list (default: true)<br>
  177. * <pre>$handle->mime_check = true;</pre></li>
  178. * <li><b>{@link no_script}</b> sets if the class turns scripts into text files (default: true)<br>
  179. * <pre>$handle->no_script = false;</pre></li>
  180. * <li><b>{@link allowed}</b> array of allowed mime-types (or one string). wildcard accepted, as in image/* (default: check {@link Init})<br>
  181. * <pre>$handle->allowed = array('application/pdf','application/msword', 'image/*');</pre></li>
  182. * <li><b>{@link forbidden}</b> array of forbidden mime-types (or one string). wildcard accepted, as in image/* (default: check {@link Init})<br>
  183. * <pre>$handle->forbidden = array('application/*');</pre></li>
  184. * </ul>
  185. * <ul>
  186. * <li><b>{@link image_convert}</b> if set, image will be converted (possible values : ''|'png'|'jpeg'|'gif'|'bmp'; default: '')<br>
  187. * <pre>$handle->image_convert = 'jpg';</pre></li>
  188. * <li><b>{@link image_background_color}</b> if set, will forcibly fill transparent areas with the color, in hexadecimal (default: null)<br>
  189. * <pre>$handle->image_background_color = '#FF00FF';</pre></li>
  190. * <li><b>{@link image_default_color}</b> fallback color background color for non alpha-transparent output formats, such as JPEG or BMP, in hexadecimal (default: #FFFFFF)<br>
  191. * <pre>$handle->image_default_color = '#FF00FF';</pre></li>
  192. * <li><b>{@link jpeg_quality}</b> sets the compression quality for JPEG images (default: 85)<br>
  193. * <pre>$handle->jpeg_quality = 50;</pre></li>
  194. * <li><b>{@link 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>
  195. * <pre>$handle->jpeg_size = 3072;</pre></li>
  196. * </ul>
  197. * The following eight settings can be used to invalidate an upload if the file is an image (note that <i>open_basedir</i> restrictions prevent the use of these settings)
  198. * <ul>
  199. * <li><b>{@link image_max_width}</b> if set to a dimension in pixels, the upload will be invalid if the image width is greater (default: null)<br>
  200. * <pre>$handle->image_max_width = 200;</pre></li>
  201. * <li><b>{@link image_max_height}</b> if set to a dimension in pixels, the upload will be invalid if the image height is greater (default: null)<br>
  202. * <pre>$handle->image_max_height = 100;</pre></li>
  203. * <li><b>{@link image_max_pixels}</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is greater (default: null)<br>
  204. * <pre>$handle->image_max_pixels = 50000;</pre></li>
  205. * <li><b>{@link image_max_ratio}</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is greater (default: null)<br>
  206. * <pre>$handle->image_max_ratio = 1.5;</pre></li>
  207. * <li><b>{@link image_min_width}</b> if set to a dimension in pixels, the upload will be invalid if the image width is lower (default: null)<br>
  208. * <pre>$handle->image_min_width = 100;</pre></li>
  209. * <li><b>{@link image_min_height}</b> if set to a dimension in pixels, the upload will be invalid if the image height is lower (default: null)<br>
  210. * <pre>$handle->image_min_height = 500;</pre></li>
  211. * <li><b>{@link image_min_pixels}</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is lower (default: null)<br>
  212. * <pre>$handle->image_min_pixels = 20000;</pre></li>
  213. * <li><b>{@link image_min_ratio}</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is lower (default: null)<br>
  214. * <pre>$handle->image_min_ratio = 0.5;</pre></li>
  215. * </ul>
  216. * <ul>
  217. * <li><b>{@link image_resize}</b> determines is an image will be resized (default: false)<br>
  218. * <pre>$handle->image_resize = true;</pre></li>
  219. * </ul>
  220. * The following variables are used only if {@link image_resize} == true
  221. * <ul>
  222. * <li><b>{@link image_x}</b> destination image width (default: 150)<br>
  223. * <pre>$handle->image_x = 100;</pre></li>
  224. * <li><b>{@link image_y}</b> destination image height (default: 150)<br>
  225. * <pre>$handle->image_y = 200;</pre></li>
  226. * </ul>
  227. * Use either one of the following
  228. * <ul>
  229. * <li><b>{@link 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>
  230. * <pre>$handle->image_ratio = true;</pre></li>
  231. * <li><b>{@link 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>
  232. * <pre>$handle->image_ratio_crop = true;</pre></li>
  233. * <li><b>{@link image_ratio_fill}</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, fitting the image in the space and coloring the remaining space. setting can also be a string, with one or more from 'TBLR', indicating which side of the space the image will be in (default: false)<br>
  234. * <pre>$handle->image_ratio_fill = true;</pre></li>
  235. * <li><b>{@link 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>
  236. * <pre>$handle->image_ratio_no_zoom_in = true;</pre></li>
  237. * <li><b>{@link 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>
  238. * <pre>$handle->image_ratio_no_zoom_out = true;</pre></li>
  239. * <li><b>{@link 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>
  240. * <pre>$handle->image_ratio_x = true;</pre></li>
  241. * <li><b>{@link 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>
  242. * <pre>$handle->image_ratio_y = true;</pre></li>
  243. * <li><b>{@link image_ratio_pixels}</b> if set to a long integer, resize image, calculating {@link image_y} and {@link image_x} to match a the number of pixels (default: false)<br>
  244. * <pre>$handle->image_ratio_pixels = 25000;</pre></li>
  245. * </ul>
  246. * The following image manipulations require GD2+
  247. * <ul>
  248. * <li><b>{@link image_brightness}</b> if set, corrects the brightness. value between -127 and 127 (default: null)<br>
  249. * <pre>$handle->image_brightness = 40;</pre></li>
  250. * <li><b>{@link image_contrast}</b> if set, corrects the contrast. value between -127 and 127 (default: null)<br>
  251. * <pre>$handle->image_contrast = 50;</pre></li>
  252. * <li><b>{@link image_opacity}</b> if set, changes the image opacity. value between 0 and 100 (default: null)<br>
  253. * <pre>$handle->image_opacity = 50;</pre></li>
  254. * <li><b>{@link image_tint_color}</b> if set, will tint the image with a color, value as hexadecimal #FFFFFF (default: null)<br>
  255. * <pre>$handle->image_tint_color = '#FF0000';</pre></li>
  256. * <li><b>{@link image_overlay_color}</b> if set, will add a colored overlay, value as hexadecimal #FFFFFF (default: null)<br>
  257. * <pre>$handle->image_overlay_color = '#FF0000';</pre></li>
  258. * <li><b>{@link image_overlay_opacity}</b> used when {@link image_overlay_color} is set, determines the opacity (default: 50)<br>
  259. * <pre>$handle->image_overlay_opacity = 20;</pre></li>
  260. * <li><b>{@link image_negative}</b> inverts the colors in the image (default: false)<br>
  261. * <pre>$handle->image_negative = true;</pre></li>
  262. * <li><b>{@link image_greyscale}</b> transforms an image into greyscale (default: false)<br>
  263. * <pre>$handle->image_greyscale = true;</pre></li>
  264. * <li><b>{@link image_threshold}</b> applies a threshold filter. value between -127 and 127 (default: null)<br>
  265. * <pre>$handle->image_threshold = 20;</pre></li>
  266. * <li><b>{@link image_unsharp}</b> applies an unsharp mask, with alpha transparency support (default: false)<br>
  267. * <pre>$handle->image_unsharp = true;</pre></li>
  268. * <li><b>{@link image_unsharp_amount}</b> unsharp mask amount, typically 50 - 200 (default: 80)<br>
  269. * <pre>$handle->image_unsharp_amount = 120;</pre></li>
  270. * <li><b>{@link image_unsharp_radius}</b> unsharp mask radius, typically 0.5 - 1 (default: 0.5)<br>
  271. * <pre>$handle->image_unsharp_radius = 0.8;</pre></li>
  272. * <li><b>{@link image_unsharp_threshold}</b> unsharp mask threshold, typically 0 - 5 (default: 1)<br>
  273. * <pre>$handle->image_unsharp_threshold = 0;</pre></li>
  274. * </ul>
  275. * <ul>
  276. * <li><b>{@link image_text}</b> creates a text label on the image, value is a string, with eventual replacement tokens (default: null)<br>
  277. * <pre>$handle->image_text = 'test';</pre></li>
  278. * <li><b>{@link image_text_direction}</b> text label direction, either 'h' horizontal or 'v' vertical (default: 'h')<br>
  279. * <pre>$handle->image_text_direction = 'v';</pre></li>
  280. * <li><b>{@link image_text_color}</b> text color for the text label, in hexadecimal (default: #FFFFFF)<br>
  281. * <pre>$handle->image_text_color = '#FF0000';</pre></li>
  282. * <li><b>{@link image_text_opacity}</b> text opacity on the text label, integer between 0 and 100 (default: 100)<br>
  283. * <pre>$handle->image_text_opacity = 50;</pre></li>
  284. * <li><b>{@link image_text_background}</b> text label background color, in hexadecimal (default: null)<br>
  285. * <pre>$handle->image_text_background = '#FFFFFF';</pre></li>
  286. * <li><b>{@link image_text_background_opacity}</b> text label background opacity, integer between 0 and 100 (default: 100)<br>
  287. * <pre>$handle->image_text_background_opacity = 50;</pre></li>
  288. * <li><b>{@link image_text_font}</b> built-in font for the text label, from 1 to 5. 1 is the smallest (default: 5)<br>
  289. * <pre>$handle->image_text_font = 4;</pre></li>
  290. * <li><b>{@link image_text_x}</b> absolute text label position, in pixels from the left border. can be negative (default: null)<br>
  291. * <pre>$handle->image_text_x = 5;</pre></li>
  292. * <li><b>{@link image_text_y}</b> absolute text label position, in pixels from the top border. can be negative (default: null)<br>
  293. * <pre>$handle->image_text_y = 5;</pre></li>
  294. * <li><b>{@link 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>
  295. * <pre>$handle->image_text_position = 'LR';</pre></li>
  296. * <li><b>{@link 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>
  297. * <pre>$handle->image_text_padding = 5;</pre></li>
  298. * <li><b>{@link image_text_padding_x}</b> text label horizontal padding (default: null)<br>
  299. * <pre>$handle->image_text_padding_x = 2;</pre></li>
  300. * <li><b>{@link image_text_padding_y}</b> text label vertical padding (default: null)<br>
  301. * <pre>$handle->image_text_padding_y = 10;</pre></li>
  302. * <li><b>{@link image_text_alignment}</b> text alignment when text has multiple lines, either 'L', 'C' or 'R' (default: 'C')<br>
  303. * <pre>$handle->image_text_alignment = 'R';</pre></li>
  304. * <li><b>{@link image_text_line_spacing}</b> space between lines in pixels, when text has multiple lines (default: 0)<br>
  305. * <pre>$handle->image_text_line_spacing = 3;</pre></li>
  306. * </ul>
  307. * <ul>
  308. * <li><b>{@link image_flip}</b> flips image, wither 'h' horizontal or 'v' vertical (default: null)<br>
  309. * <pre>$handle->image_flip = 'h';</pre></li>
  310. * <li><b>{@link image_rotate}</b> rotates image. possible values are 90, 180 and 270 (default: null)<br>
  311. * <pre>$handle->image_rotate = 90;</pre></li>
  312. * <li><b>{@link 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>
  313. * <pre>$handle->image_crop = array(50,40,30,20); OR '-20 20%'...</pre></li>
  314. * <li><b>{@link image_precrop}</b> crops image, before an eventual resizing. 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>
  315. * <pre>$handle->image_precrop = array(50,40,30,20); OR '-20 20%'...</pre></li>
  316. * </ul>
  317. * <ul>
  318. * <li><b>{@link image_bevel}</b> adds a bevel border to the image. value is thickness in pixels (default: null)<br>
  319. * <pre>$handle->image_bevel = 20;</pre></li>
  320. * <li><b>{@link image_bevel_color1}</b> top and left bevel color, in hexadecimal (default: #FFFFFF)<br>
  321. * <pre>$handle->image_bevel_color1 = '#FFFFFF';</pre></li>
  322. * <li><b>{@link image_bevel_color2}</b> bottom and right bevel color, in hexadecimal (default: #000000)<br>
  323. * <pre>$handle->image_bevel_color2 = '#000000';</pre></li>
  324. * <li><b>{@link 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>
  325. * <pre>$handle->image_border = '3px'; OR '-20 20%' OR array(3,2)...</pre></li>
  326. * <li><b>{@link image_border_color}</b> border color, in hexadecimal (default: #FFFFFF)<br>
  327. * <pre>$handle->image_border_color = '#FFFFFF';</pre></li>
  328. * <li><b>{@link image_border_opacity}</b> border opacity, integer between 0 and 100 (default: 100)<br>
  329. * <pre>$handle->image_border_opacity = 50;</pre></li>
  330. * <li><b>{@link image_border_transparent}</b> adds a fading-to-transparent 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>
  331. * <pre>$handle->image_border_transparent = '3px'; OR '-20 20%' OR array(3,2)...</pre></li>
  332. * <li><b>{@link image_frame}</b> type of frame: 1=flat 2=crossed (default: null)<br>
  333. * <pre>$handle->image_frame = 2;</pre></li>
  334. * <li><b>{@link image_frame_colors}</b> list of hex colors, in an array or a space separated string (default: '#FFFFFF #999999 #666666 #000000')<br>
  335. * <pre>$handle->image_frame_colors = array('#999999', '#FF0000', '#666666', '#333333', '#000000');</pre></li>
  336. * <li><b>{@link image_frame_opacity}</b> frame opacity, integer between 0 and 100 (default: 100)<br>
  337. * <pre>$handle->image_frame_opacity = 50;</pre></li>
  338. * </ul>
  339. * <ul>
  340. * <li><b>{@link image_watermark}</b> adds a watermark on the image, value is a local filename. accepted files are GIF, JPG, BMP, PNG and PNG alpha (default: null)<br>
  341. * <pre>$handle->image_watermark = 'watermark.png';</pre></li>
  342. * <li><b>{@link image_watermark_x}</b> absolute watermark position, in pixels from the left border. can be negative (default: null)<br>
  343. * <pre>$handle->image_watermark_x = 5;</pre></li>
  344. * <li><b>{@link image_watermark_y}</b> absolute watermark position, in pixels from the top border. can be negative (default: null)<br>
  345. * <pre>$handle->image_watermark_y = 5;</pre></li>
  346. * <li><b>{@link image_watermark_position}</b> watermark position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: null)<br>
  347. * <pre>$handle->image_watermark_position = 'LR';</pre></li>
  348. * <li><b>{@link image_watermark_no_zoom_in}</b> prevents the watermark to be resized up if it is smaller than the image (default: true)<br>
  349. * <pre>$handle->image_watermark_no_zoom_in = false;</pre></li>
  350. * <li><b>{@link image_watermark_no_zoom_out}</b> prevents the watermark to be resized down if it is bigger than the image (default: false)<br>
  351. * <pre>$handle->image_watermark_no_zoom_out = true;</pre></li>
  352. * </ul>
  353. * <ul>
  354. * <li><b>{@link 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>
  355. * <pre>$handle->image_reflection_height = '25%';</pre></li>
  356. * <li><b>{@link image_reflection_space}</b> space in pixels between the source image and the reflection, can be negative (default: null)<br>
  357. * <pre>$handle->image_reflection_space = 3;</pre></li>
  358. * <li><b>{@link image_reflection_color}</b> reflection background color, in hexadecimal. Now deprecated in favor of {@link image_default_color} (default: #FFFFFF)<br>
  359. * <pre>$handle->image_default_color = '#000000';</pre></li>
  360. * <li><b>{@link image_reflection_opacity}</b> opacity level at which the reflection starts, integer between 0 and 100 (default: 60)<br>
  361. * <pre>$handle->image_reflection_opacity = 60;</pre></li>
  362. * </ul>
  363. *
  364. * <b>Values that can be read before calling {@link process}()</b>
  365. * <ul>
  366. * <li><b>{@link file_src_name}</b> Source file name</li>
  367. * <li><b>{@link file_src_name_body}</b> Source file name body</li>
  368. * <li><b>{@link file_src_name_ext}</b> Source file extension</li>
  369. * <li><b>{@link file_src_pathname}</b> Source file complete path and name</li>
  370. * <li><b>{@link file_src_mime}</b> Source file mime type</li>
  371. * <li><b>{@link file_src_size}</b> Source file size in bytes</li>
  372. * <li><b>{@link file_src_error}</b> Upload error code</li>
  373. * <li><b>{@link file_is_image}</b> Boolean flag, true if the file is a supported image type</li>
  374. * </ul>
  375. * If the file is a supported image type (and <i>open_basedir</i> restrictions allow it)
  376. * <ul>
  377. * <li><b>{@link image_src_x}</b> Source file width in pixels</li>
  378. * <li><b>{@link image_src_y}</b> Source file height in pixels</li>
  379. * <li><b>{@link image_src_pixels}</b> Source file number of pixels</li>
  380. * <li><b>{@link image_src_type}</b> Source file type (png, jpg, gif or bmp)</li>
  381. * <li><b>{@link image_src_bits}</b> Source file color depth</li>
  382. * </ul>
  383. *
  384. * <b>Values that can be read after calling {@link process}()</b>
  385. * <ul>
  386. * <li><b>{@link file_dst_path}</b> Destination file path</li>
  387. * <li><b>{@link file_dst_name_body}</b> Destination file name body</li>
  388. * <li><b>{@link file_dst_name_ext}</b> Destination file extension</li>
  389. * <li><b>{@link file_dst_name}</b> Destination file name</li>
  390. * <li><b>{@link file_dst_pathname}</b> Destination file complete path and name</li>
  391. * </ul>
  392. * If the file is a supported image type
  393. * <ul>
  394. * <li><b>{@link image_dst_x}</b> Destination file width</li>
  395. * <li><b>{@link image_dst_y}</b> Destination file height</li>
  396. * <li><b>{@link image_convert}</b> Destination file format</li>
  397. * </ul>
  398. *
  399. * <b>Requirements</b>
  400. *
  401. * Most of the image operations require GD. GD2 is greatly recommended
  402. *
  403. * The class is compatible with PHP 4.3+, and compatible with PHP5
  404. *
  405. * <b>Changelog</b>
  406. * <ul>
  407. * <li><b>v 0.31</b> 11/04/2011<br>
  408. * - added application/x-rar MIME type<br>
  409. * - make sure exec() and ini_get_all()function are not disabled if we want to use them<br>
  410. * - make sure that we don't divide by zero when calculating JPEG size<br>
  411. * - {@link allowed} and {@link forbidden} can now accept strings<br>
  412. * - try to guess the file extension from the MIME type if there is no file extension<br>
  413. * - better class properties when changing the file extension<br>
  414. * - added {@link file_force_extension} to allow extension-less files if needed<br>
  415. * - better file safe conversion of the filename<br>
  416. * - allow shorthand byte values, such as 1K, 2M, 3G for {@link file_max_size} and {@link jpeg_size}<br>
  417. * - added {@link image_opacity} to change picture opacity<br>
  418. * - added {@link image_border_opacity} to allow semi-transparent borders<br>
  419. * - added {@link image_frame_opacity} to allow semi-transparent frames<br>
  420. * - added {@link image_border_transparent} to allow borders fading to transparent<br>
  421. * - duplicated {@link image_overlay_percent} into {@link image_overlay_opacity}<br>
  422. * - duplicated {@link image_text_percent} into {@link image_text_opacity}<br>
  423. * - duplicated {@link image_text_background_percent} into {@link image_text_background_opacity}</li>
  424. * <li><b>v 0.30</b> 05/09/2010<br>
  425. * - implemented an unsharp mask, with alpha transparency support, activated if {@link image_unsharp} is true. added {@link image_unsharp_amount}, {@link image_unsharp_radius}, and {@link image_unsharp_threshold}<br>
  426. * - added text/rtf MIME type, and no_script exception<br>
  427. * - corrected bug when {@link no_script} is activated and several process() are called<br>
  428. * - better error handling for finfo<br>
  429. * - display upload_max_filesize information from php.ini in the log<br>
  430. * - automatic extension for extension-less images<br>
  431. * - fixed {@link image_ratio_fill} top and left filling<br>
  432. * - fixed alphablending issue when applying a transparent PNG watermark on a transparent PNG<br>
  433. * - added {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to allow the watermark to be resized down (or up) to fit in the image. By default, the watermark may be resized down, but not up.</li>
  434. * <li><b>v 0.29</b> 03/02/2010<br>
  435. * - added protection against malicious images<br>
  436. * - added zip and torrent MIME type<br>
  437. * - replaced split() with explode()<br>
  438. * - initialise image_dst_x/y with image_src_x/y<br>
  439. * - removed {@link mime_fileinfo}, {@link mime_file}, {@link mime_magic} and {@link mime_getimagesize} from the docs since they are used before {@link process}<br>
  440. * - added more extensions and MIME types<br>
  441. * - improved MIME type validation<br>
  442. * - improved logging</li>
  443. * <li><b>v 0.28</b> 10/08/2009<br>
  444. * - replaced ereg functions to be compatible with PHP 5.3<br>
  445. * - added flv MIME type<br>
  446. * - improved MIME type detection<br>
  447. * - added {@link file_name_body_pre} to prepend a string to the file name<br>
  448. * - added {@link mime_fileinfo}, {@link mime_file}, {@link mime_magic} and {@link mime_getimagesize} so that it is possible to deactivate some MIME type checking method<br>
  449. * - use exec() rather than shell_exec(), to play better with safe mode <br>
  450. * - added some error messages<br>
  451. * - fix bug when checking on conditions, {@link processed} wasn't propagated properly</li>
  452. * <li><b>v 0.27</b> 14/05/2009<br>
  453. * - look for the language files directory from __FILE__<br>
  454. * - deactivate {@link file_auto_rename} if {@link file_overwrite} is set<br>
  455. * - improved transparency replacement for true color images<br>
  456. * - fixed calls to newer version of UNIX file utility<br>
  457. * - fixed error when using PECL Fileinfo extension in SAFE MODE, and when using the finfo class<br>
  458. * - added {@link image_precrop} to crop the image before an eventual resizing</li>
  459. * <li><b>v 0.26</b> 13/11/2008<br>
  460. * - rewrote conversion from palette to true color to handle transparency better<br>
  461. * - fixed imagecopymergealpha() when the overlayed image is of wrong dimensions<br>
  462. * - fixed imagecreatenew() when the image to create have less than 1 pixels width or height<br>
  463. * - rewrote MIME type detection to be more secure and not rely on browser information; now using Fileinfo PECL extension, UNIX file() command, MIME magic, and getimagesize(), in that order<br>
  464. * - added support for Flash uploaders<br>
  465. * - some bug fixing and error handling</li>
  466. * <li><b>v 0.25</b> 17/11/2007<br>
  467. * - added translation files and mechanism to instantiate the class with a language different from English<br>
  468. * - added {@link forbidden} to set an array of forbidden MIME types<br>
  469. * - implemented support for simple wildcards in {@link allowed} and {@link forbidden}, such as image/*<br>
  470. * - preset the file extension to the desired conversion format when converting an image<br>
  471. * - added read and write support for BMP images<br>
  472. * - added a flag {@link file_is_image} to determine if the file is a supported image type<br>
  473. * - the class now provides some information about the image, before calling {@link process}(). Available are {@link image_src_x}, {@link image_src_y} and the newly introduced {@link image_src_bits}, {@link image_src_pixels} and {@link image_src_type}. Note that this will not work if <i>open_basedir</i> restrictions are in place<br>
  474. * - improved logging; now provides useful system information<br>
  475. * - added some more pre-processing checks for files that are images: {@link image_max_width}, {@link image_max_height}, {@link image_max_pixels}, {@link image_max_ratio}, {@link image_min_width}, {@link image_min_height}, {@link image_min_pixels} and {@link image_min_ratio}<br>
  476. * - added {@link image_ratio_pixels} to resize an image to a number of pixels, keeping aspect ratio<br>
  477. * - added {@link image_is_palette} and {@link image_is_transparent} and {@link image_transparent_color} for GIF images<br>
  478. * - added {@link image_default_color} to define a fallback color for non alpha-transparent output formats, such as JPEG or BMP<br>
  479. * - changed {@link image_background_color}, which now forces transparent areas to be painted<br>
  480. * - improved reflections and color overlays so that it works with alpha transparent images<br>
  481. * - {@link image_reflection_color} is now deprecated in favour of {@link image_default_color}<br />
  482. * - transparent PNGs are now processed in true color, and fully preserving the alpha channel when doing merges<br>
  483. * - transparent GIFs are now automatically detected. {@link preserve_transparency} is deprecated<br>
  484. * - transparent true color images can be saved as GIF while retaining transparency, semi transparent areas being merged with {@link image_default_color}<br>
  485. * - transparent true color images can be saved as JPG/BMP with the semi transparent areas being merged with {@link image_default_color}<br>
  486. * - fixed conversion of images to true color<br>
  487. * - the class can now output the uploaded files content as the return value of process() if the function is called with an empty or null argumenti, or no argument</li>
  488. * <li><b>v 0.24</b> 25/05/2007<br>
  489. * - added {@link image_background_color}, to set the default background color of an image<br>
  490. * - added possibility of using replacement tokens in text labels<br>
  491. * - changed default JPEG quality to 85<br>
  492. * - fixed a small bug when using greyscale filter and associated filters<br>
  493. * - added {@link image_ratio_fill} in order to fit an image within some dimensions and color the remaining space. Very similar to {@link image_ratio_crop}<br>
  494. * - improved the recursive creation of directories<br>
  495. * - the class now converts palette based images to true colors before doing graphic manipulations</li>
  496. * <li><b>v 0.23</b> 23/12/2006<br>
  497. * - 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>
  498. * <li><b>v 0.22</b> 16/12/2006<br>
  499. * - added automatic creation of a temporary file if the upload directory is not within open_basedir<br>
  500. * - fixed a bug which was preventing to work on a local file by overwriting it with its processed copy<br>
  501. * - added MIME types video/x-ms-wmv and image/x-png and fixed PNG support for IE weird MIME types<br>
  502. * - 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>
  503. * - added support for multiple lines in the text, using "\n" as a line break<br>
  504. * - added {@link image_text_line_spacing} which allow to set the space between several lines of text<br>
  505. * - added {@link image_text_alignment} which allow to set the alignment when text has several lines<br>
  506. * - {@link image_text_font} can now be set to the path of a GDF font to load external fonts<br>
  507. * - added {@link image_reflection_height} to create a reflection of the source image, which height is in pixels or percentage<br>
  508. * - added {@link image_reflection_space} to set the space in pixels between the source image and the reflection<br>
  509. * - added {@link image_reflection_color} to set the reflection background color<br>
  510. * - added {@link image_reflection_opacity} to set the initial level of opacity of the reflection</li>
  511. * <li><b>v 0.21</b> 30/09/2006<br>
  512. * - 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>
  513. * - added {@link mime_check}, which default is true, to set checks against {@link allowed} MIME list<br>
  514. * - if MIME is empty, the class now triggers an error<br>
  515. * - color #000000 is OK for {@link image_text_color}, and related text transparency bug fixed<br>
  516. * - {@link gd_version}() now uses gd_info(), or else phpinfo()<br>
  517. * - fixed path issue when the destination path has no trailing slash on Windows systems <br>
  518. * - removed inline functions to be fully PHP5 compatible </li>
  519. * <li><b>v 0.20</b> 11/08/2006<br>
  520. * - added some more error checking and messages (GD presence, permissions...)<br>
  521. * - fix when uploading files without extension<br>
  522. * - changed values for {@link image_brightness} and {@link image_contrast} to be between -127 and 127<br>
  523. * - added {@link dir_auto_create} to automatically and recursively create destination directory if missing.<br>
  524. * - added {@link dir_auto_chmod} to automatically chmod the destination directory if not writeable.<br>
  525. * - added {@link dir_chmod} to set the default chmod to use.<br>
  526. * - added {@link image_crop} to crop images<br>
  527. * - added {@link image_negative} to invert the colors on the image<br>
  528. * - added {@link image_greyscale} to turn the image into greyscale<br>
  529. * - added {@link image_threshold} to apply a threshold filter on the image<br>
  530. * - added {@link image_bevel}, {@link image_bevel_color1} and {@link image_bevel_color2} to add a bevel border<br>
  531. * - added {@link image_border} and {@link image_border_color} to add a single color border<br>
  532. * - added {@link image_frame} and {@link image_frame_colors} to add a multicolored frame</li>
  533. * <li><b>v 0.19</b> 29/03/2006<br>
  534. * - class is now compatible i18n (thanks Sylwester).<br>
  535. * - the class can mow manipulate local files, not only uploaded files (instanciate the class with a local filename).<br>
  536. * - {@link file_safe_name} has been improved a bit.<br>
  537. * - 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>
  538. * - added {@link image_text} and all derivated settings to add a text label on the image.<br>
  539. * - added {@link image_watermark} and all derivated settings to add a watermark image on the image.<br>
  540. * - added {@link image_flip} and {@link image_rotate} for more image manipulations<br>
  541. * - added {@link jpeg_size} to calculate the JPG compression quality in order to fit within one filesize.</li>
  542. * <li><b>v 0.18</b> 02/02/2006<br>
  543. * - added {@link no_script} to turn dangerous scripts into text files.<br>
  544. * - added {@link mime_magic_check} to set the class to use mime_magic.<br>
  545. * - added {@link preserve_transparency} *experimental*. Thanks Gregor.<br>
  546. * - fixed size and mime checking, wasn't working :/ Thanks Willem.<br>
  547. * - fixed memory leak when resizing images.<br>
  548. * - when resizing, it is not necessary anymore to set {@link image_convert}.<br>
  549. * - il is now possible to simply convert an image, with no resizing.<br>
  550. * - sets the default {@link file_max_size} to upload_max_filesize from php.ini. Thanks Edward</li>
  551. * <li><b>v 0.17</b> 28/05/2005<br>
  552. * - the class can be used with any version of GD.<br>
  553. * - added security check on the file with a list of mime-types.<br>
  554. * - changed the license to GPL v2 only</li>
  555. * <li><b>v 0.16</b> 19/05/2005<br>
  556. * - added {@link file_auto_rename} automatic file renaming if the same filename already exists.<br>
  557. * - added {@link file_safe_name} safe formatting of the filename (spaces to _underscores so far).<br>
  558. * - added some more error reporting to avoid crash if GD is not present</li>
  559. * <li><b>v 0.15</b> 16/04/2005<br>
  560. * - added JPEG compression quality setting. Thanks Vad</li>
  561. * <li><b>v 0.14</b> 14/03/2005<br>
  562. * - reworked the class file to allow parsing with phpDocumentor</li>
  563. * <li><b>v 0.13</b> 07/03/2005<br>
  564. * - fixed a bug with {@link image_ratio}. Thanks Justin.<br>
  565. * - added {@link image_ratio_no_zoom_in} and {@link image_ratio_no_zoom_out} </li>
  566. * <li><b>v 0.12</b> 21/01/2005<br>
  567. * - added {@link image_ratio} to resize within max values, keeping image ratio</li>
  568. * <li><b>v 0.11</b> 22/08/2003<br>
  569. * - update for GD2 (changed imageresized() into imagecopyresampled() and imagecreate() into imagecreatetruecolor())</li>
  570. * </ul>
  571. *
  572. * @package cmf
  573. * @subpackage external
  574. */
  575. class Upload {
  576. /**
  577. * Class version
  578. *
  579. * @access public
  580. * @var string
  581. */
  582. var $version;
  583. /**
  584. * Uploaded file name
  585. *
  586. * @access public
  587. * @var string
  588. */
  589. var $file_src_name;
  590. /**
  591. * Uploaded file name body (i.e. without extension)
  592. *
  593. * @access public
  594. * @var string
  595. */
  596. var $file_src_name_body;
  597. /**
  598. * Uploaded file name extension
  599. *
  600. * @access public
  601. * @var string
  602. */
  603. var $file_src_name_ext;
  604. /**
  605. * Uploaded file MIME type
  606. *
  607. * @access public
  608. * @var string
  609. */
  610. var $file_src_mime;
  611. /**
  612. * Uploaded file size, in bytes
  613. *
  614. * @access public
  615. * @var double
  616. */
  617. var $file_src_size;
  618. /**
  619. * Holds eventual PHP error code from $_FILES
  620. *
  621. * @access public
  622. * @var string
  623. */
  624. var $file_src_error;
  625. /**
  626. * Uloaded file name, including server path
  627. *
  628. * @access public
  629. * @var string
  630. */
  631. var $file_src_pathname;
  632. /**
  633. * Uloaded file name temporary copy
  634. *
  635. * @access private
  636. * @var string
  637. */
  638. var $file_src_temp;
  639. /**
  640. * Destination file name
  641. *
  642. * @access public
  643. * @var string
  644. */
  645. var $file_dst_path;
  646. /**
  647. * Destination file name
  648. *
  649. * @access public
  650. * @var string
  651. */
  652. var $file_dst_name;
  653. /**
  654. * Destination file name body (i.e. without extension)
  655. *
  656. * @access public
  657. * @var string
  658. */
  659. var $file_dst_name_body;
  660. /**
  661. * Destination file extension
  662. *
  663. * @access public
  664. * @var string
  665. */
  666. var $file_dst_name_ext;
  667. /**
  668. * Destination file name, including path
  669. *
  670. * @access public
  671. * @var string
  672. */
  673. var $file_dst_pathname;
  674. /**
  675. * Source image width
  676. *
  677. * @access public
  678. * @var integer
  679. */
  680. var $image_src_x;
  681. /**
  682. * Source image height
  683. *
  684. * @access public
  685. * @var integer
  686. */
  687. var $image_src_y;
  688. /**
  689. * Source image color depth
  690. *
  691. * @access public
  692. * @var integer
  693. */
  694. var $image_src_bits;
  695. /**
  696. * Number of pixels
  697. *
  698. * @access public
  699. * @var long
  700. */
  701. var $image_src_pixels;
  702. /**
  703. * Type of image (png, gif, jpg or bmp)
  704. *
  705. * @access public
  706. * @var string
  707. */
  708. var $image_src_type;
  709. /**
  710. * Destination image width
  711. *
  712. * @access public
  713. * @var integer
  714. */
  715. var $image_dst_x;
  716. /**
  717. * Destination image height
  718. *
  719. * @access public
  720. * @var integer
  721. */
  722. var $image_dst_y;
  723. /**
  724. * Supported image formats
  725. *
  726. * @access private
  727. * @var array
  728. */
  729. var $image_supported;
  730. /**
  731. * Flag to determine if the source file is an image
  732. *
  733. * @access public
  734. * @var boolean
  735. */
  736. var $file_is_image;
  737. /**
  738. * Flag set after instanciating the class
  739. *
  740. * Indicates if the file has been uploaded properly
  741. *
  742. * @access public
  743. * @var bool
  744. */
  745. var $uploaded;
  746. /**
  747. * Flag stopping PHP upload checks
  748. *
  749. * Indicates whether we instanciated the class with a filename, in which case
  750. * we will not check on the validity of the PHP *upload*
  751. *
  752. * This flag is automatically set to true when working on a local file
  753. *
  754. * Warning: for uploads, this flag MUST be set to false for security reason
  755. *
  756. * @access public
  757. * @var bool
  758. */
  759. var $no_upload_check;
  760. /**
  761. * Flag set after calling a process
  762. *
  763. * Indicates if the processing, and copy of the resulting file went OK
  764. *
  765. * @access public
  766. * @var bool
  767. */
  768. var $processed;
  769. /**
  770. * Holds eventual error message in plain english
  771. *
  772. * @access public
  773. * @var string
  774. */
  775. var $error;
  776. /**
  777. * Holds an HTML formatted log
  778. *
  779. * @access public
  780. * @var string
  781. */
  782. var $log;
  783. // overiddable processing variables
  784. /**
  785. * Set this variable to replace the name body (i.e. without extension)
  786. *
  787. * @access public
  788. * @var string
  789. */
  790. var $file_new_name_body;
  791. /**
  792. * Set this variable to append a string to the file name body
  793. *
  794. * @access public
  795. * @var string
  796. */
  797. var $file_name_body_add;
  798. /**
  799. * Set this variable to prepend a string to the file name body
  800. *
  801. * @access public
  802. * @var string
  803. */
  804. var $file_name_body_pre;
  805. /**
  806. * Set this variable to change the file extension
  807. *
  808. * @access public
  809. * @var string
  810. */
  811. var $file_new_name_ext;
  812. /**
  813. * Set this variable to format the filename (spaces changed to _)
  814. *
  815. * @access public
  816. * @var boolean
  817. */
  818. var $file_safe_name;
  819. /**
  820. * Forces an extension if the source file doesn't have one
  821. *
  822. * If the file is an image, then the correct extension will be added
  823. * Otherwise, a .txt extension will be chosen
  824. *
  825. * @access public
  826. * @var boolean
  827. */
  828. var $file_force_extension;
  829. /**
  830. * Set this variable to false if you don't want to check the MIME against the allowed list
  831. *
  832. * This variable is set to true by default for security reason
  833. *
  834. * @access public
  835. * @var boolean
  836. */
  837. var $mime_check;
  838. /**
  839. * Set this variable to false in the init() function if you don't want to check the MIME
  840. * with Fileinfo PECL extension. On some systems, Fileinfo is known to be buggy, and you
  841. * may want to deactivate it in the class code directly.
  842. *
  843. * You can also set it with the path of the magic database file.
  844. * If set to true, the class will try to read the MAGIC environment variable
  845. * and if it is empty, will default to '/usr/share/file/magic'
  846. * If set to an empty string, it will call finfo_open without the path argument
  847. *
  848. * This variable is set to true by default for security reason
  849. *
  850. * @access public
  851. * @var boolean
  852. */
  853. var $mime_fileinfo;
  854. /**
  855. * Set this variable to false in the init() function if you don't want to check the MIME
  856. * with UNIX file() command
  857. *
  858. * This variable is set to true by default for security reason
  859. *
  860. * @access public
  861. * @var boolean
  862. */
  863. var $mime_file;
  864. /**
  865. * Set this variable to false in the init() function if you don't want to check the MIME
  866. * with the magic.mime file
  867. *
  868. * The function mime_content_type() will be deprecated,
  869. * and this variable will be set to false in a future release
  870. *
  871. * This variable is set to true by default for security reason
  872. *
  873. * @access public
  874. * @var boolean
  875. */
  876. var $mime_magic;
  877. /**
  878. * Set this variable to false in the init() function if you don't want to check the MIME
  879. * with getimagesize()
  880. *
  881. * The class tries to get a MIME type from getimagesize()
  882. * If no MIME is returned, it tries to guess the MIME type from the file type
  883. *
  884. * This variable is set to true by default for security reason
  885. *
  886. * @access public
  887. * @var boolean
  888. */
  889. var $mime_getimagesize;
  890. /**
  891. * Set this variable to false if you don't want to turn dangerous scripts into simple text files
  892. *
  893. * @access public
  894. * @var boolean
  895. */
  896. var $no_script;
  897. /**
  898. * Set this variable to true to allow automatic renaming of the file
  899. * if the file already exists
  900. *
  901. * Default value is true
  902. *
  903. * For instance, on uploading foo.ext,<br>
  904. * if foo.ext already exists, upload will be renamed foo_1.ext<br>
  905. * and if foo_1.ext already exists, upload will be renamed foo_2.ext<br>
  906. *
  907. * Note that this option doesn't have any effect if {@link file_overwrite} is true
  908. *
  909. * @access public
  910. * @var bool
  911. */
  912. var $file_auto_rename;
  913. /**
  914. * Set this variable to true to allow automatic creation of the destination
  915. * directory if it is missing (works recursively)
  916. *
  917. * Default value is true
  918. *
  919. * @access public
  920. * @var bool
  921. */
  922. var $dir_auto_create;
  923. /**
  924. * Set this variable to true to allow automatic chmod of the destination
  925. * directory if it is not writeable
  926. *
  927. * Default value is true
  928. *
  929. * @access public
  930. * @var bool
  931. */
  932. var $dir_auto_chmod;
  933. /**
  934. * Set this variable to the default chmod you want the class to use
  935. * when creating directories, or attempting to write in a directory
  936. *
  937. * Default value is 0777 (without quotes)
  938. *
  939. * @access public
  940. * @var bool
  941. */
  942. var $dir_chmod;
  943. /**
  944. * Set this variable tu true to allow overwriting of an existing file
  945. *
  946. * Default value is false, so no files will be overwritten
  947. *
  948. * @access public
  949. * @var bool
  950. */
  951. var $file_overwrite;
  952. /**
  953. * Set this variable to change the maximum size in bytes for an uploaded file
  954. *
  955. * Default value is the value <i>upload_max_filesize</i> from php.ini
  956. *
  957. * Value in bytes (integer) or shorthand byte values (string) is allowed.
  958. * The available options are K (for Kilobytes), M (for Megabytes) and G (for Gigabytes)
  959. *
  960. * @access public
  961. * @var double
  962. */
  963. var $file_max_size;
  964. /**
  965. * Set this variable to true to resize the file if it is an image
  966. *
  967. * You will probably want to set {@link image_x} and {@link image_y}, and maybe one of the ratio variables
  968. *
  969. * Default value is false (no resizing)
  970. *
  971. * @access public
  972. * @var bool
  973. */
  974. var $image_resize;
  975. /**
  976. * Set this variable to convert the file if it is an image
  977. *
  978. * Possibles values are : ''; 'png'; 'jpeg'; 'gif'; 'bmp'
  979. *
  980. * Default value is '' (no conversion)<br>
  981. * If {@link resize} is true, {@link convert} will be set to the source file extension
  982. *
  983. * @access public
  984. * @var string
  985. */
  986. var $image_convert;
  987. /**
  988. * Set this variable to the wanted (or maximum/minimum) width for the processed image, in pixels
  989. *
  990. * Default value is 150
  991. *
  992. * @access public
  993. * @var integer
  994. */
  995. var $image_x;
  996. /**
  997. * Set this variable to the wanted (or maximum/minimum) height for the processed image, in pixels
  998. *
  999. * Default value is 150
  1000. *
  1001. * @access public
  1002. * @var integer
  1003. */
  1004. var $image_y;
  1005. /**
  1006. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1007. *
  1008. * Default value is false
  1009. *
  1010. * @access public
  1011. * @var bool
  1012. */
  1013. var $image_ratio;
  1014. /**
  1015. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1016. *
  1017. * The image will be resized as to fill the whole space, and excedent will be cropped
  1018. *
  1019. * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right)
  1020. * If set as a string, it determines which side of the image is kept while cropping.
  1021. * By default, the part of the image kept is in the center, i.e. it crops equally on both sides
  1022. *
  1023. * Default value is false
  1024. *
  1025. * @access public
  1026. * @var mixed
  1027. */
  1028. var $image_ratio_crop;
  1029. /**
  1030. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1031. *
  1032. * The image will be resized to fit entirely in the space, and the rest will be colored.
  1033. * The default color is white, but can be set with {@link image_default_color}
  1034. *
  1035. * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right)
  1036. * If set as a string, it determines in which side of the space the image is displayed.
  1037. * By default, the image is displayed in the center, i.e. it fills the remaining space equally on both sides
  1038. *
  1039. * Default value is false
  1040. *
  1041. * @access public
  1042. * @var mixed
  1043. */
  1044. var $image_ratio_fill;
  1045. /**
  1046. * Set this variable to a number of pixels so that {@link image_x} and {@link image_y} are the best match possible
  1047. *
  1048. * The image will be resized to have approximatively the number of pixels
  1049. * The aspect ratio wil be conserved
  1050. *
  1051. * Default value is false
  1052. *
  1053. * @access public
  1054. * @var mixed
  1055. */
  1056. var $image_ratio_pixels;
  1057. /**
  1058. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y},
  1059. * but only if original image is bigger
  1060. *
  1061. * Default value is false
  1062. *
  1063. * @access public
  1064. * @var bool
  1065. */
  1066. var $image_ratio_no_zoom_in;
  1067. /**
  1068. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y},
  1069. * but only if original image is smaller
  1070. *
  1071. * Default value is false
  1072. *
  1073. * @access public
  1074. * @var bool
  1075. */
  1076. var $image_ratio_no_zoom_out;
  1077. /**
  1078. * Set this variable to calculate {@link image_x} automatically , using {@link image_y} and conserving ratio
  1079. *
  1080. * Default value is false
  1081. *
  1082. * @access public
  1083. * @var bool
  1084. */
  1085. var $image_ratio_x;
  1086. /**
  1087. * Set this variable to calculate {@link image_y} automatically , using {@link image_x} and conserving ratio
  1088. *
  1089. * Default value is false
  1090. *
  1091. * @access public
  1092. * @var bool
  1093. */
  1094. var $image_ratio_y;
  1095. /**
  1096. * Set this variable to set a maximum image width, above which the upload will be invalid
  1097. *
  1098. * Default value is null
  1099. *
  1100. * @access public
  1101. * @var integer
  1102. */
  1103. var $image_max_width;
  1104. /**
  1105. * Set this variable to set a maximum image height, above which the upload will be invalid
  1106. *
  1107. * Default value is null
  1108. *
  1109. * @access public
  1110. * @var integer
  1111. */
  1112. var $image_max_height;
  1113. /**
  1114. * Set this variable to set a maximum number of pixels for an image, above which the upload will be invalid
  1115. *
  1116. * Default value is null
  1117. *
  1118. * @access public
  1119. * @var long
  1120. */
  1121. var $image_max_pixels;
  1122. /**
  1123. * Set this variable to set a maximum image aspect ratio, above which the upload will be invalid
  1124. *
  1125. * Note that ratio = width / height
  1126. *
  1127. * Default value is null
  1128. *
  1129. * @access public
  1130. * @var float
  1131. */
  1132. var $image_max_ratio;
  1133. /**
  1134. * Set this variable to set a minimum image width, below which the upload will be invalid
  1135. *
  1136. * Default value is null
  1137. *
  1138. * @access public
  1139. * @var integer
  1140. */
  1141. var $image_min_width;
  1142. /**
  1143. * Set this variable to set a minimum image height, below which the upload will be invalid
  1144. *
  1145. * Default value is null
  1146. *
  1147. * @access public
  1148. * @var integer
  1149. */
  1150. var $image_min_height;
  1151. /**
  1152. * Set this variable to set a minimum number of pixels for an image, below which the upload will be invalid
  1153. *
  1154. * Default value is null
  1155. *
  1156. * @access public
  1157. * @var long
  1158. */
  1159. var $image_min_pixels;
  1160. /**
  1161. * Set this variable to set a minimum image aspect ratio, below which the upload will be invalid
  1162. *
  1163. * Note that ratio = width / height
  1164. *
  1165. * Default value is null
  1166. *
  1167. * @access public
  1168. * @var float
  1169. */
  1170. var $image_min_ratio;
  1171. /**
  1172. * Quality of JPEG created/converted destination image
  1173. *
  1174. * Default value is 85
  1175. *
  1176. * @access public
  1177. * @var integer
  1178. */
  1179. var $jpeg_quality;
  1180. /**
  1181. * Determines the quality of the JPG image to fit a desired file size
  1182. *
  1183. * The JPG quality will be set between 1 and 100%
  1184. * The calculations are approximations.
  1185. *
  1186. * Value in bytes (integer) or shorthand byte values (string) is allowed.
  1187. * The available options are K (for Kilobytes), M (for Megabytes) and G (for Gigabytes)
  1188. *
  1189. * Default value is null (no calculations)
  1190. *
  1191. * @access public
  1192. * @var integer
  1193. */
  1194. var $jpeg_size;
  1195. /**
  1196. * Preserve transparency when resizing or converting an image (deprecated)
  1197. *
  1198. * Default value is automatically set to true for transparent GIFs
  1199. * This setting is now deprecated
  1200. *
  1201. * @access public
  1202. * @var integer
  1203. */
  1204. var $preserve_transparency;
  1205. /**
  1206. * Flag set to true when the image is transparent
  1207. *
  1208. * This is actually used only for transparent GIFs
  1209. *
  1210. * @access public
  1211. * @var boolean
  1212. */
  1213. var $image_is_transparent;
  1214. /**
  1215. * Transparent color in a palette
  1216. *
  1217. * This is actually used only for transparent GIFs
  1218. *
  1219. * @access public
  1220. * @var boolean
  1221. */
  1222. var $image_transparent_color;
  1223. /**
  1224. * Background color, used to paint transparent areas with
  1225. *
  1226. * If set, it will forcibly remove transparency by painting transparent areas with the color
  1227. * This setting will fill in all transparent areas in PNG and GIF, as opposed to {@link image_default_color}
  1228. * which will do so only in BMP, JPEG, and alpha transparent areas in transparent GIFs
  1229. * This setting overrides {@link image_default_color}
  1230. *
  1231. * Default value is null
  1232. *
  1233. * @access public
  1234. * @var string
  1235. */
  1236. var $image_background_color;
  1237. /**
  1238. * Default color for non alpha-transparent images
  1239. *
  1240. * This setting is to be used to define a background color for semi transparent areas
  1241. * of an alpha transparent when the output format doesn't support alpha transparency
  1242. * This is useful when, from an alpha transparent PNG image, or an image with alpha transparent features
  1243. * if you want to output it as a transparent GIFs for instance, you can set a blending color for transparent areas
  1244. * If you output in JPEG or BMP, this color will be used to fill in the previously transparent areas
  1245. *
  1246. * The default color white
  1247. *
  1248. * @access public
  1249. * @var boolean
  1250. */
  1251. var $image_default_color;
  1252. /**
  1253. * Flag set to true when the image is not true color
  1254. *
  1255. * @access public
  1256. * @var boolean
  1257. */
  1258. var $image_is_palette;
  1259. /**
  1260. * Corrects the image brightness
  1261. *
  1262. * Value can range between -127 and 127
  1263. *
  1264. * Default value is null
  1265. *
  1266. * @access public
  1267. * @var integer
  1268. */
  1269. var $image_brightness;
  1270. /**
  1271. * Corrects the image contrast
  1272. *
  1273. * Value can range between -127 and 127
  1274. *
  1275. * Default value is null
  1276. *
  1277. * @access public
  1278. * @var integer
  1279. */
  1280. var $image_contrast;
  1281. /**
  1282. * Changes the image opacity
  1283. *
  1284. * Value can range between 0 and 100
  1285. *
  1286. * Default value is null
  1287. *
  1288. * @access public
  1289. * @var integer
  1290. */
  1291. var $image_opacity;
  1292. /**
  1293. * Applies threshold filter
  1294. *
  1295. * Value can range between -127 and 127
  1296. *
  1297. * Default value is null
  1298. *
  1299. * @access public
  1300. * @var integer
  1301. */
  1302. var $image_threshold;
  1303. /**
  1304. * Applies a tint on the image
  1305. *
  1306. * Value is an hexadecimal color, such as #FFFFFF
  1307. *
  1308. * Default value is null
  1309. *
  1310. * @access public
  1311. * @var string;
  1312. */
  1313. var $image_tint_color;
  1314. /**
  1315. * Applies a colored overlay on the image
  1316. *
  1317. * Value is an hexadecimal color, such as #FFFFFF
  1318. *
  1319. * To use with {@link image_overlay_opacity}
  1320. *
  1321. * Default value is null
  1322. *
  1323. * @access public
  1324. * @var string;
  1325. */
  1326. var $image_overlay_color;
  1327. /**
  1328. * Sets the opacity for the colored overlay
  1329. *
  1330. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1331. *
  1332. * Unless used with {@link image_overlay_color}, this setting has no effect
  1333. *
  1334. * Default value is 50
  1335. *
  1336. * @access public
  1337. * @var integer
  1338. */
  1339. var $image_overlay_opacity;
  1340. /**
  1341. * Soon to be deprecated old form of {@link image_overlay_opacity}
  1342. *
  1343. * @access public
  1344. * @var integer
  1345. */
  1346. var $image_overlay_percent;
  1347. /**
  1348. * Inverts the color of an image
  1349. *
  1350. * Default value is FALSE
  1351. *
  1352. * @access public
  1353. * @var boolean;
  1354. */
  1355. var $image_negative;
  1356. /**
  1357. * Turns the image into greyscale
  1358. *
  1359. * Default value is FALSE
  1360. *
  1361. * @access public
  1362. * @var boolean;
  1363. */
  1364. var $image_greyscale;
  1365. /**
  1366. * Applies an unsharp mask, with alpha transparency support
  1367. *
  1368. * Beware that this unsharp mask is quite resource-intensive
  1369. *
  1370. * Default value is FALSE
  1371. *
  1372. * @access public
  1373. * @var boolean;
  1374. */
  1375. var $image_unsharp;
  1376. /**
  1377. * Sets the unsharp mask amount
  1378. *
  1379. * Value is an integer between 0 and 500, typically between 50 and 200
  1380. *
  1381. * Unless used with {@link image_unsharp}, this setting has no effect
  1382. *
  1383. * Default value is 80
  1384. *
  1385. * @access public
  1386. * @var integer
  1387. */
  1388. var $image_unsharp_amount;
  1389. /**
  1390. * Sets the unsharp mask radius
  1391. *
  1392. * Value is an integer between 0 and 50, typically between 0.5 and 1
  1393. *
  1394. * Unless used with {@link image_unsharp}, this setting has no effect
  1395. *
  1396. * Default value is 0.5
  1397. *
  1398. * @access public
  1399. * @var integer
  1400. */
  1401. var $image_unsharp_radius;
  1402. /**
  1403. * Sets the unsharp mask threshold
  1404. *
  1405. * Value is an integer between 0 and 255, typically between 0 and 5
  1406. *
  1407. * Unless used with {@link image_unsharp}, this setting has no effect
  1408. *
  1409. * Default value is 1
  1410. *
  1411. * @access public
  1412. * @var integer
  1413. */
  1414. var $image_unsharp_threshold;
  1415. /**
  1416. * Adds a text label on the image
  1417. *
  1418. * Value is a string, any text. Text will not word-wrap, although you can use breaklines in your text "\n"
  1419. *
  1420. * If set, this setting allow the use of all other settings starting with image_text_
  1421. *
  1422. * Replacement tokens can be used in the string:
  1423. * <pre>
  1424. * gd_version src_name src_name_body src_name_ext
  1425. * src_pathname src_mime src_x src_y
  1426. * src_type src_bits src_pixels
  1427. * src_size src_size_kb src_size_mb src_size_human
  1428. * dst_path dst_name_body dst_pathname
  1429. * dst_name dst_name_ext dst_x dst_y
  1430. * date time host server ip
  1431. * </pre>
  1432. * The tokens must be enclosed in square brackets: [dst_x] will be replaced by the width of the picture
  1433. *
  1434. * Default value is null
  1435. *
  1436. * @access public
  1437. * @var string;
  1438. */
  1439. var $image_text;
  1440. /**
  1441. * Sets the text direction for the text label
  1442. *
  1443. * Value is either 'h' or 'v', as in horizontal and vertical
  1444. *
  1445. * Default value is h (horizontal)
  1446. *
  1447. * @access public
  1448. * @var string;
  1449. */
  1450. var $image_text_direction;
  1451. /**
  1452. * Sets the text color for the text label
  1453. *
  1454. * Value is an hexadecimal color, such as #FFFFFF
  1455. *
  1456. * Default value is #FFFFFF (white)
  1457. *
  1458. * @access public
  1459. * @var string;
  1460. */
  1461. var $image_text_color;
  1462. /**
  1463. * Sets the text opacity in the text label
  1464. *
  1465. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1466. *
  1467. * Default value is 100
  1468. *
  1469. * @access public
  1470. * @var integer
  1471. */
  1472. var $image_text_opacity;
  1473. /**
  1474. * Soon to be deprecated old form of {@link image_text_opacity}
  1475. *
  1476. * @access public
  1477. * @var integer
  1478. */
  1479. var $image_text_percent;
  1480. /**
  1481. * Sets the text background color for the text label
  1482. *
  1483. * Value is an hexadecimal color, such as #FFFFFF
  1484. *
  1485. * Default value is null (no background)
  1486. *
  1487. * @access public
  1488. * @var string;
  1489. */
  1490. var $image_text_background;
  1491. /**
  1492. * Sets the text background opacity in the text label
  1493. *
  1494. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1495. *
  1496. * Default value is 100
  1497. *
  1498. * @access public
  1499. * @var integer
  1500. */
  1501. var $image_text_background_opacity;
  1502. /**
  1503. * Soon to be deprecated old form of {@link image_text_background_opacity}
  1504. *
  1505. * @access public
  1506. * @var integer
  1507. */
  1508. var $image_text_background_percent;
  1509. /**
  1510. * Sets the text font in the text label
  1511. *
  1512. * Value is a an integer between 1 and 5 for GD built-in fonts. 1 is the smallest font, 5 the biggest
  1513. * 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.
  1514. *
  1515. * Default value is 5
  1516. *
  1517. * @access public
  1518. * @var mixed;
  1519. */
  1520. var $image_text_font;
  1521. /**
  1522. * Sets the text label position within the image
  1523. *
  1524. * Value is one or two out of 'TBLR' (top, bottom, left, right)
  1525. *
  1526. * The positions are as following:
  1527. * <pre>
  1528. * TL T TR
  1529. * L R
  1530. * BL B BR
  1531. * </pre>
  1532. *
  1533. * Default value is null (centered, horizontal and vertical)
  1534. *
  1535. * Note that is {@link image_text_x} and {@link image_text_y} are used, this setting has no effect
  1536. *
  1537. * @access public
  1538. * @var string;
  1539. */
  1540. var $image_text_position;
  1541. /**
  1542. * Sets the text label absolute X position within the image
  1543. *
  1544. * Value is in pixels, representing the distance between the left of the image and the label
  1545. * If a negative value is used, it will represent the distance between the right of the image and the label
  1546. *
  1547. * Default value is null (so {@link image_text_position} is used)
  1548. *
  1549. * @access public
  1550. * @var integer
  1551. */
  1552. var $image_text_x;
  1553. /**
  1554. * Sets the text label absolute Y position within the image
  1555. *
  1556. * Value is in pixels, representing the distance between the top of the image and the label
  1557. * If a negative value is used, it will represent the distance between the bottom of the image and the label
  1558. *
  1559. * Default value is null (so {@link image_text_position} is used)
  1560. *
  1561. * @access public
  1562. * @var integer
  1563. */
  1564. var $image_text_y;
  1565. /**
  1566. * Sets the text label padding
  1567. *
  1568. * Value is in pixels, representing the distance between the text and the label background border
  1569. *
  1570. * Default value is 0
  1571. *
  1572. * This setting can be overriden by {@link image_text_padding_x} and {@link image_text_padding_y}
  1573. *
  1574. * @access public
  1575. * @var integer
  1576. */
  1577. var $image_text_padding;
  1578. /**
  1579. * Sets the text label horizontal padding
  1580. *
  1581. * Value is in pixels, representing the distance between the text and the left and right label background borders
  1582. *
  1583. * Default value is null
  1584. *
  1585. * If set, this setting overrides the horizontal part of {@link image_text_padding}
  1586. *
  1587. * @access public
  1588. * @var integer
  1589. */
  1590. var $image_text_padding_x;
  1591. /**
  1592. * Sets the text label vertical padding
  1593. *
  1594. * Value is in pixels, representing the distance between the text and the top and bottom label background borders
  1595. *
  1596. * Default value is null
  1597. *
  1598. * If set, his setting overrides the vertical part of {@link image_text_padding}
  1599. *
  1600. * @access public
  1601. * @var integer
  1602. */
  1603. var $image_text_padding_y;
  1604. /**
  1605. * Sets the text alignment
  1606. *
  1607. * Value is a string, which can be either 'L', 'C' or 'R'
  1608. *
  1609. * Default value is 'C'
  1610. *
  1611. * This setting is relevant only if the text has several lines.
  1612. *
  1613. * @access public
  1614. * @var string;
  1615. */
  1616. var $image_text_alignment;
  1617. /**
  1618. * Sets the text line spacing
  1619. *
  1620. * Value is an integer, in pixels
  1621. *
  1622. * Default value is 0
  1623. *
  1624. * This setting is relevant only if the text has several lines.
  1625. *
  1626. * @access public
  1627. * @var integer
  1628. */
  1629. var $image_text_line_spacing;
  1630. /**
  1631. * Sets the height of the reflection
  1632. *
  1633. * Value is an integer in pixels, or a string which format can be in pixels or percentage.
  1634. * For instance, values can be : 40, '40', '40px' or '40%'
  1635. *
  1636. * Default value is null, no reflection
  1637. *
  1638. * @access public
  1639. * @var mixed;
  1640. */
  1641. var $image_reflection_height;
  1642. /**
  1643. * Sets the space between the source image and its relection
  1644. *
  1645. * Value is an integer in pixels, which can be negative
  1646. *
  1647. * Default value is 2
  1648. *
  1649. * This setting is relevant only if {@link image_reflection_height} is set
  1650. *
  1651. * @access public
  1652. * @var integer
  1653. */
  1654. var $image_reflection_space;
  1655. /**
  1656. * Sets the color of the reflection background (deprecated)
  1657. *
  1658. * Value is an hexadecimal color, such as #FFFFFF
  1659. *
  1660. * Default value is #FFFFFF
  1661. *
  1662. * This setting is relevant only if {@link image_reflection_height} is set
  1663. *
  1664. * This setting is now deprecated in favor of {@link image_default_color}
  1665. *
  1666. * @access public
  1667. * @var string;
  1668. */
  1669. var $image_reflection_color;
  1670. /**
  1671. * Sets the initial opacity of the reflection
  1672. *
  1673. * Value is an integer between 0 (no opacity) and 100 (full opacity).
  1674. * The reflection will start from {@link image_reflection_opacity} and end up at 0
  1675. *
  1676. * Default value is 60
  1677. *
  1678. * This setting is relevant only if {@link image_reflection_height} is set
  1679. *
  1680. * @access public
  1681. * @var integer
  1682. */
  1683. var $image_reflection_opacity;
  1684. /**
  1685. * Flips the image vertically or horizontally
  1686. *
  1687. * Value is either 'h' or 'v', as in horizontal and vertical
  1688. *
  1689. * Default value is null (no flip)
  1690. *
  1691. * @access public
  1692. * @var string;
  1693. */
  1694. var $image_flip;
  1695. /**
  1696. * Rotates the image by increments of 45 degrees
  1697. *
  1698. * Value is either 90, 180 or 270
  1699. *
  1700. * Default value is null (no rotation)
  1701. *
  1702. * @access public
  1703. * @var string;
  1704. */
  1705. var $image_rotate;
  1706. /**
  1707. * Crops an image
  1708. *
  1709. * Values are four dimensions, or two, or one (CSS style)
  1710. * They represent the amount cropped top, right, bottom and left.
  1711. * These values can either be in an array, or a space separated string.
  1712. * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1713. *
  1714. * For instance, are valid:
  1715. * <pre>
  1716. * $foo->image_crop = 20 OR array(20);
  1717. * $foo->image_crop = '20px' OR array('20px');
  1718. * $foo->image_crop = '20 40' OR array('20', 40);
  1719. * $foo->image_crop = '-20 25%' OR array(-20, '25%');
  1720. * $foo->image_crop = '20px 25%' OR array('20px', '25%');
  1721. * $foo->image_crop = '20% 25%' OR array('20%', '25%');
  1722. * $foo->image_crop = '20% 25% 10% 30%' OR array('20%', '25%', '10%', '30%');
  1723. * $foo->image_crop = '20px 25px 2px 2px' OR array('20px', '25%px', '2px', '2px');
  1724. * $foo->image_crop = '20 25% 40px 10%' OR array(20, '25%', '40px', '10%');
  1725. * </pre>
  1726. *
  1727. * If a value is negative, the image will be expanded, and the extra parts will be filled with black
  1728. *
  1729. * Default value is null (no cropping)
  1730. *
  1731. * @access public
  1732. * @var string OR array;
  1733. */
  1734. var $image_crop;
  1735. /**
  1736. * Crops an image, before an eventual resizing
  1737. *
  1738. * See {@link image_crop} for valid formats
  1739. *
  1740. * Default value is null (no cropping)
  1741. *
  1742. * @access public
  1743. * @var string OR array;
  1744. */
  1745. var $image_precrop;
  1746. /**
  1747. * Adds a bevel border on the image
  1748. *
  1749. * Value is a positive integer, representing the thickness of the bevel
  1750. *
  1751. * If the bevel colors are the same as the background, it makes a fade out effect
  1752. *
  1753. * Default value is null (no bevel)
  1754. *
  1755. * @access public
  1756. * @var integer
  1757. */
  1758. var $image_bevel;
  1759. /**
  1760. * Top and left bevel color
  1761. *
  1762. * Value is a color, in hexadecimal format
  1763. * This setting is used only if {@link image_bevel} is set
  1764. *
  1765. * Default value is #FFFFFF
  1766. *
  1767. * @access public
  1768. * @var string;
  1769. */
  1770. var $image_bevel_color1;
  1771. /**
  1772. * Right and bottom bevel color
  1773. *
  1774. * Value is a color, in hexadecimal format
  1775. * This setting is used only if {@link image_bevel} is set
  1776. *
  1777. * Default value is #000000
  1778. *
  1779. * @access public
  1780. * @var string;
  1781. */
  1782. var $image_bevel_color2;
  1783. /**
  1784. * Adds a single-color border on the outer of the image
  1785. *
  1786. * Values are four dimensions, or two, or one (CSS style)
  1787. * They represent the border thickness top, right, bottom and left.
  1788. * These values can either be in an array, or a space separated string.
  1789. * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1790. *
  1791. * See {@link image_crop} for valid formats
  1792. *
  1793. * If a value is negative, the image will be cropped.
  1794. * Note that the dimensions of the picture will be increased by the borders' thickness
  1795. *
  1796. * Default value is null (no border)
  1797. *
  1798. * @access public
  1799. * @var integer
  1800. */
  1801. var $image_border;
  1802. /**
  1803. * Border color
  1804. *
  1805. * Value is a color, in hexadecimal format.
  1806. * This setting is used only if {@link image_border} is set
  1807. *
  1808. * Default value is #FFFFFF
  1809. *
  1810. * @access public
  1811. * @var string;
  1812. */
  1813. var $image_border_color;
  1814. /**
  1815. * Sets the opacity for the borders
  1816. *
  1817. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1818. *
  1819. * Unless used with {@link image_border}, this setting has no effect
  1820. *
  1821. * Default value is 100
  1822. *
  1823. * @access public
  1824. * @var integer
  1825. */
  1826. var $image_border_opacity;
  1827. /**
  1828. * Adds a fading-to-transparent border on the image
  1829. *
  1830. * Values are four dimensions, or two, or one (CSS style)
  1831. * They represent the border thickness top, right, bottom and left.
  1832. * These values can either be in an array, or a space separated string.
  1833. * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1834. *
  1835. * See {@link image_crop} for valid formats
  1836. *
  1837. * Note that the dimensions of the picture will not be increased by the borders' thickness
  1838. *
  1839. * Default value is null (no border)
  1840. *
  1841. * @access public
  1842. * @var integer
  1843. */
  1844. var $image_border_transparent;
  1845. /**
  1846. * Adds a multi-color frame on the outer of the image
  1847. *
  1848. * Value is an integer. Two values are possible for now:
  1849. * 1 for flat border, meaning that the frame is mirrored horizontally and vertically
  1850. * 2 for crossed border, meaning that the frame will be inversed, as in a bevel effect
  1851. *
  1852. * The frame will be composed of colored lines set in {@link image_frame_colors}
  1853. *
  1854. * Note that the dimensions of the picture will be increased by the borders' thickness
  1855. *
  1856. * Default value is null (no frame)
  1857. *
  1858. * @access public
  1859. * @var integer
  1860. */
  1861. var $image_frame;
  1862. /**
  1863. * Sets the colors used to draw a frame
  1864. *
  1865. * Values is a list of n colors in hexadecimal format.
  1866. * These values can either be in an array, or a space separated string.
  1867. *
  1868. * The colors are listed in the following order: from the outset of the image to its center
  1869. *
  1870. * For instance, are valid:
  1871. * <pre>
  1872. * $foo->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
  1873. * $foo->image_frame_colors = array('#FFFFFF', '#999999', '#666666', '#000000');
  1874. * </pre>
  1875. *
  1876. * This setting is used only if {@link image_frame} is set
  1877. *
  1878. * Default value is '#FFFFFF #999999 #666666 #000000'
  1879. *
  1880. * @access public
  1881. * @var string OR array;
  1882. */
  1883. var $image_frame_colors;
  1884. /**
  1885. * Sets the opacity for the frame
  1886. *
  1887. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1888. *
  1889. * Unless used with {@link image_frame}, this setting has no effect
  1890. *
  1891. * Default value is 100
  1892. *
  1893. * @access public
  1894. * @var integer
  1895. */
  1896. var $image_frame_opacity;
  1897. /**
  1898. * Adds a watermark on the image
  1899. *
  1900. * Value is a local image filename, relative or absolute. GIF, JPG, BMP and PNG are supported, as well as PNG alpha.
  1901. *
  1902. * If set, this setting allow the use of all other settings starting with image_watermark_
  1903. *
  1904. * Default value is null
  1905. *
  1906. * @access public
  1907. * @var string;
  1908. */
  1909. var $image_watermark;
  1910. /**
  1911. * Sets the watermarkposition within the image
  1912. *
  1913. * Value is one or two out of 'TBLR' (top, bottom, left, right)
  1914. *
  1915. * The positions are as following: TL T TR
  1916. * L R
  1917. * BL B BR
  1918. *
  1919. * Default value is null (centered, horizontal and vertical)
  1920. *
  1921. * Note that is {@link image_watermark_x} and {@link image_watermark_y} are used, this setting has no effect
  1922. *
  1923. * @access public
  1924. * @var string;
  1925. */
  1926. var $image_watermark_position;
  1927. /**
  1928. * Sets the watermark absolute X position within the image
  1929. *
  1930. * Value is in pixels, representing the distance between the top of the image and the watermark
  1931. * If a negative value is used, it will represent the distance between the bottom of the image and the watermark
  1932. *
  1933. * Default value is null (so {@link image_watermark_position} is used)
  1934. *
  1935. * @access public
  1936. * @var integer
  1937. */
  1938. var $image_watermark_x;
  1939. /**
  1940. * Sets the twatermark absolute Y position within the image
  1941. *
  1942. * Value is in pixels, representing the distance between the left of the image and the watermark
  1943. * If a negative value is used, it will represent the distance between the right of the image and the watermark
  1944. *
  1945. * Default value is null (so {@link image_watermark_position} is used)
  1946. *
  1947. * @access public
  1948. * @var integer
  1949. */
  1950. var $image_watermark_y;
  1951. /**
  1952. * Prevents the watermark to be resized up if it is smaller than the image
  1953. *
  1954. * If the watermark if smaller than the destination image, taking in account the desired watermark position
  1955. * then it will be resized up to fill in the image (minus the {@link image_watermark_x} or {@link image_watermark_y} values)
  1956. *
  1957. * If you don't want your watermark to be resized in any way, then
  1958. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to true
  1959. * If you want your watermark to be resized up or doan to fill in the image better, then
  1960. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to false
  1961. *
  1962. * Default value is true (so the watermark will not be resized up, which is the behaviour most people expect)
  1963. *
  1964. * @access public
  1965. * @var integer
  1966. */
  1967. var $image_watermark_no_zoom_in;
  1968. /**
  1969. * Prevents the watermark to be resized down if it is bigger than the image
  1970. *
  1971. * If the watermark if bigger than the destination image, taking in account the desired watermark position
  1972. * then it will be resized down to fit in the image (minus the {@link image_watermark_x} or {@link image_watermark_y} values)
  1973. *
  1974. * If you don't want your watermark to be resized in any way, then
  1975. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to true
  1976. * If you want your watermark to be resized up or doan to fill in the image better, then
  1977. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to false
  1978. *
  1979. * Default value is false (so the watermark may be shrinked to fit in the image)
  1980. *
  1981. * @access public
  1982. * @var integer
  1983. */
  1984. var $image_watermark_no_zoom_out;
  1985. /**
  1986. * List of MIME types per extension
  1987. *
  1988. * @access private
  1989. * @var array
  1990. */
  1991. var $mime_types;
  1992. /**
  1993. * Allowed MIME types
  1994. *
  1995. * Default is a selection of safe mime-types, but you might want to change it
  1996. *
  1997. * Simple wildcards are allowed, such as image/* or application/*
  1998. * If there is only one MIME type allowed, then it can be a string instead of an array
  1999. *
  2000. * @access public
  2001. * @var array OR string
  2002. */
  2003. var $allowed;
  2004. /**
  2005. * Forbidden MIME types
  2006. *
  2007. * Default is a selection of safe mime-types, but you might want to change it
  2008. * To only check for forbidden MIME types, and allow everything else, set {@link allowed} to array('* / *') without the spaces
  2009. *
  2010. * Simple wildcards are allowed, such as image/* or application/*
  2011. * If there is only one MIME type forbidden, then it can be a string instead of an array
  2012. *
  2013. * @access public
  2014. * @var array OR string
  2015. */
  2016. var $forbidden;
  2017. /**
  2018. * Array of translated error messages
  2019. *
  2020. * By default, the language is english (en_GB)
  2021. * Translations can be in separate files, in a lang/ subdirectory
  2022. *
  2023. * @access public
  2024. * @var array
  2025. */
  2026. var $translation;
  2027. /**
  2028. * Language selected for the translations
  2029. *
  2030. * By default, the language is english ("en_GB")
  2031. *
  2032. * @access public
  2033. * @var array
  2034. */
  2035. var $language;
  2036. /**
  2037. * Init or re-init all the processing variables to their default values
  2038. *
  2039. * This function is called in the constructor, and after each call of {@link process}
  2040. *
  2041. * @access private
  2042. */
  2043. function init() {
  2044. // overiddable variables
  2045. $this->file_new_name_body = null; // replace the name body
  2046. $this->file_name_body_add = null; // append to the name body
  2047. $this->file_name_body_pre = null; // prepend to the name body
  2048. $this->file_new_name_ext = null; // replace the file extension
  2049. $this->file_safe_name = true; // format safely the filename
  2050. $this->file_force_extension = true; // forces extension if there isn't one
  2051. $this->file_overwrite = false; // allows overwritting if the file already exists
  2052. $this->file_auto_rename = true; // auto-rename if the file already exists
  2053. $this->dir_auto_create = true; // auto-creates directory if missing
  2054. $this->dir_auto_chmod = true; // auto-chmod directory if not writeable
  2055. $this->dir_chmod = 0777; // default chmod to use
  2056. $this->no_script = true; // turns scripts into test files
  2057. $this->mime_check = true; // checks the mime type against the allowed list
  2058. // these are the different MIME detection methods. if one of these method doesn't work on your
  2059. // system, you can deactivate it here; just set it to false
  2060. $this->mime_fileinfo = true; // MIME detection with Fileinfo PECL extension
  2061. $this->mime_file = true; // MIME detection with UNIX file() command
  2062. $this->mime_magic = true; // MIME detection with mime_magic (mime_content_type())
  2063. $this->mime_getimagesize = true; // MIME detection with getimagesize()
  2064. // get the default max size from php.ini
  2065. $this->file_max_size_raw = trim(ini_get('upload_max_filesize'));
  2066. $this->file_max_size = $this->getsize($this->file_max_size_raw);
  2067. $this->image_resize = false; // resize the image
  2068. $this->image_convert = ''; // convert. values :''; 'png'; 'jpeg'; 'gif'; 'bmp'
  2069. $this->image_x = 150;
  2070. $this->image_y = 150;
  2071. $this->image_ratio = false; // keeps aspect ratio with x and y dimensions
  2072. $this->image_ratio_crop = false; // keeps aspect ratio with x and y dimensions, filling the space
  2073. $this->image_ratio_fill = false; // keeps aspect ratio with x and y dimensions, fitting the image in the space, and coloring the rest
  2074. $this->image_ratio_pixels = false; // keeps aspect ratio, calculating x and y so that the image is approx the set number of pixels
  2075. $this->image_ratio_no_zoom_in = false;
  2076. $this->image_ratio_no_zoom_out = false;
  2077. $this->image_ratio_x = false; // calculate the $image_x if true
  2078. $this->image_ratio_y = false; // calculate the $image_y if true
  2079. $this->jpeg_quality = 85;
  2080. $this->jpeg_size = null;
  2081. $this->preserve_transparency = false;
  2082. $this->image_is_transparent = false;
  2083. $this->image_transparent_color = null;
  2084. $this->image_background_color = null;
  2085. $this->image_default_color = '#ffffff';
  2086. $this->image_is_palette = false;
  2087. $this->image_max_width = null;
  2088. $this->image_max_height = null;
  2089. $this->image_max_pixels = null;
  2090. $this->image_max_ratio = null;
  2091. $this->image_min_width = null;
  2092. $this->image_min_height = null;
  2093. $this->image_min_pixels = null;
  2094. $this->image_min_ratio = null;
  2095. $this->image_brightness = null;
  2096. $this->image_contrast = null;
  2097. $this->image_opacity = null;
  2098. $this->image_threshold = null;
  2099. $this->image_tint_color = null;
  2100. $this->image_overlay_color = null;
  2101. $this->image_overlay_opacity = null;
  2102. $this->image_overlay_percent = null;
  2103. $this->image_negative = false;
  2104. $this->image_greyscale = false;
  2105. $this->image_unsharp = false;
  2106. $this->image_unsharp_amount = 80;
  2107. $this->image_unsharp_radius = 0.5;
  2108. $this->image_unsharp_threshold = 1;
  2109. $this->image_text = null;
  2110. $this->image_text_direction = null;
  2111. $this->image_text_color = '#FFFFFF';
  2112. $this->image_text_opacity = 100;
  2113. $this->image_text_percent = 100;
  2114. $this->image_text_background = null;
  2115. $this->image_text_background_opacity = 100;
  2116. $this->image_text_background_percent = 100;
  2117. $this->image_text_font = 5;
  2118. $this->image_text_x = null;
  2119. $this->image_text_y = null;
  2120. $this->image_text_position = null;
  2121. $this->image_text_padding = 0;
  2122. $this->image_text_padding_x = null;
  2123. $this->image_text_padding_y = null;
  2124. $this->image_text_alignment = 'C';
  2125. $this->image_text_line_spacing = 0;
  2126. $this->image_reflection_height = null;
  2127. $this->image_reflection_space = 2;
  2128. $this->image_reflection_color = '#ffffff';
  2129. $this->image_reflection_opacity = 60;
  2130. $this->image_watermark = null;
  2131. $this->image_watermark_x = null;
  2132. $this->image_watermark_y = null;
  2133. $this->image_watermark_position = null;
  2134. $this->image_watermark_no_zoom_in = true;
  2135. $this->image_watermark_no_zoom_out = false;
  2136. $this->image_flip = null;
  2137. $this->image_rotate = null;
  2138. $this->image_crop = null;
  2139. $this->image_precrop = null;
  2140. $this->image_bevel = null;
  2141. $this->image_bevel_color1 = '#FFFFFF';
  2142. $this->image_bevel_color2 = '#000000';
  2143. $this->image_border = null;
  2144. $this->image_border_color = '#FFFFFF';
  2145. $this->image_border_opacity = 100;
  2146. $this->image_border_transparent = null;
  2147. $this->image_frame = null;
  2148. $this->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
  2149. $this->image_frame_opacity = 100;
  2150. $this->forbidden = array();
  2151. $this->allowed = array(
  2152. 'application/arj',
  2153. 'application/excel',
  2154. 'application/gnutar',
  2155. 'application/mspowerpoint',
  2156. 'application/msword',
  2157. 'application/octet-stream',
  2158. 'application/onenote',
  2159. 'application/pdf',
  2160. 'application/plain',
  2161. 'application/postscript',
  2162. 'application/powerpoint',
  2163. 'application/rar',
  2164. 'application/rtf',
  2165. 'application/vnd.ms-excel',
  2166. 'application/vnd.ms-excel.addin.macroEnabled.12',
  2167. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  2168. 'application/vnd.ms-excel.sheet.macroEnabled.12',
  2169. 'application/vnd.ms-excel.template.macroEnabled.12',
  2170. 'application/vnd.ms-office',
  2171. 'application/vnd.ms-officetheme',
  2172. 'application/vnd.ms-powerpoint',
  2173. 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  2174. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  2175. 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  2176. 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  2177. 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  2178. 'application/vnd.ms-word',
  2179. 'application/vnd.ms-word.document.macroEnabled.12',
  2180. 'application/vnd.ms-word.template.macroEnabled.12',
  2181. 'application/vnd.oasis.opendocument.chart',
  2182. 'application/vnd.oasis.opendocument.database',
  2183. 'application/vnd.oasis.opendocument.formula',
  2184. 'application/vnd.oasis.opendocument.graphics',
  2185. 'application/vnd.oasis.opendocument.graphics-template',
  2186. 'application/vnd.oasis.opendocument.image',
  2187. 'application/vnd.oasis.opendocument.presentation',
  2188. 'application/vnd.oasis.opendocument.presentation-template',
  2189. 'application/vnd.oasis.opendocument.spreadsheet',
  2190. 'application/vnd.oasis.opendocument.spreadsheet-template',
  2191. 'application/vnd.oasis.opendocument.text',
  2192. 'application/vnd.oasis.opendocument.text-master',
  2193. 'application/vnd.oasis.opendocument.text-template',
  2194. 'application/vnd.oasis.opendocument.text-web',
  2195. 'application/vnd.openofficeorg.extension',
  2196. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  2197. 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  2198. 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  2199. 'application/vnd.openxmlformats-officedocument.presentationml.template',
  2200. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  2201. 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  2202. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  2203. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  2204. 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  2205. 'application/vocaltec-media-file',
  2206. 'application/wordperfect',
  2207. 'application/x-bittorrent',
  2208. 'application/x-bzip',
  2209. 'application/x-bzip2',
  2210. 'application/x-compressed',
  2211. 'application/x-excel',
  2212. 'application/x-gzip',
  2213. 'application/x-latex',
  2214. 'application/x-midi',
  2215. 'application/xml',
  2216. 'application/x-msexcel',
  2217. 'application/x-rar',
  2218. 'application/x-rar-compressed',
  2219. 'application/x-rtf',
  2220. 'application/x-shockwave-flash',
  2221. 'application/x-sit',
  2222. 'application/x-stuffit',
  2223. 'application/x-troff-msvideo',
  2224. 'application/x-zip',
  2225. 'application/x-zip-compressed',
  2226. 'application/zip',
  2227. 'audio/*',
  2228. 'image/*',
  2229. 'multipart/x-gzip',
  2230. 'multipart/x-zip',
  2231. 'text/plain',
  2232. 'text/rtf',
  2233. 'text/richtext',
  2234. 'text/xml',
  2235. 'video/*'
  2236. );
  2237. $this->mime_types = array(
  2238. 'jpg' => 'image/jpeg',
  2239. 'jpeg' => 'image/jpeg',
  2240. 'jpe' => 'image/jpeg',
  2241. 'gif' => 'image/gif',
  2242. 'png' => 'image/png',
  2243. 'bmp' => 'image/bmp',
  2244. 'flv' => 'video/x-flv',
  2245. 'js' => 'application/x-javascript',
  2246. 'json' => 'application/json',
  2247. 'tiff' => 'image/tiff',
  2248. 'css' => 'text/css',
  2249. 'xml' => 'application/xml',
  2250. 'doc' => 'application/msword',
  2251. 'docx' => 'application/msword',
  2252. 'xls' => 'application/vnd.ms-excel',
  2253. 'xlt' => 'application/vnd.ms-excel',
  2254. 'xlm' => 'application/vnd.ms-excel',
  2255. 'xld' => 'application/vnd.ms-excel',
  2256. 'xla' => 'application/vnd.ms-excel',
  2257. 'xlc' => 'application/vnd.ms-excel',
  2258. 'xlw' => 'application/vnd.ms-excel',
  2259. 'xll' => 'application/vnd.ms-excel',
  2260. 'ppt' => 'application/vnd.ms-powerpoint',
  2261. 'pps' => 'application/vnd.ms-powerpoint',
  2262. 'rtf' => 'application/rtf',
  2263. 'pdf' => 'application/pdf',
  2264. 'html' => 'text/html',
  2265. 'htm' => 'text/html',
  2266. 'php' => 'text/html',
  2267. 'txt' => 'text/plain',
  2268. 'mpeg' => 'video/mpeg',
  2269. 'mpg' => 'video/mpeg',
  2270. 'mpe' => 'video/mpeg',
  2271. 'mp3' => 'audio/mpeg3',
  2272. 'wav' => 'audio/wav',
  2273. 'aiff' => 'audio/aiff',
  2274. 'aif' => 'audio/aiff',
  2275. 'avi' => 'video/msvideo',
  2276. 'wmv' => 'video/x-ms-wmv',
  2277. 'mov' => 'video/quicktime',
  2278. 'zip' => 'application/zip',
  2279. 'tar' => 'application/x-tar',
  2280. 'swf' => 'application/x-shockwave-flash',
  2281. 'odt' => 'application/vnd.oasis.opendocument.text',
  2282. 'ott' => 'application/vnd.oasis.opendocument.text-template',
  2283. 'oth' => 'application/vnd.oasis.opendocument.text-web',
  2284. 'odm' => 'application/vnd.oasis.opendocument.text-master',
  2285. 'odg' => 'application/vnd.oasis.opendocument.graphics',
  2286. 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
  2287. 'odp' => 'application/vnd.oasis.opendocument.presentation',
  2288. 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
  2289. 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  2290. 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
  2291. 'odc' => 'application/vnd.oasis.opendocument.chart',
  2292. 'odf' => 'application/vnd.oasis.opendocument.formula',
  2293. 'odb' => 'application/vnd.oasis.opendocument.database',
  2294. 'odi' => 'application/vnd.oasis.opendocument.image',
  2295. 'oxt' => 'application/vnd.openofficeorg.extension',
  2296. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  2297. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  2298. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  2299. 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  2300. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  2301. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  2302. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  2303. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  2304. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  2305. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  2306. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  2307. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  2308. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  2309. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  2310. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  2311. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  2312. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  2313. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  2314. 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  2315. 'thmx' => 'application/vnd.ms-officetheme',
  2316. 'onetoc' => 'application/onenote',
  2317. 'onetoc2' => 'application/onenote',
  2318. 'onetmp' => 'application/onenote',
  2319. 'onepkg' => 'application/onenote',
  2320. );
  2321. }
  2322. /**
  2323. * Constructor. Checks if the file has been uploaded
  2324. *
  2325. * The constructor takes $_FILES['form_field'] array as argument
  2326. * where form_field is the form field name
  2327. *
  2328. * The constructor will check if the file has been uploaded in its temporary location, and
  2329. * accordingly will set {@link uploaded} (and {@link error} is an error occurred)
  2330. *
  2331. * If the file has been uploaded, the constructor will populate all the variables holding the upload
  2332. * information (none of the processing class variables are used here).
  2333. * You can have access to information about the file (name, size, MIME type...).
  2334. *
  2335. *
  2336. * Alternatively, you can set the first argument to be a local filename (string)
  2337. * This allows processing of a local file, as if the file was uploaded
  2338. *
  2339. * The optional second argument allows you to set the language for the error messages
  2340. *
  2341. * @access private
  2342. * @param array $file $_FILES['form_field']
  2343. * or string $file Local filename
  2344. * @param string $lang Optional language code
  2345. */
  2346. function Upload($file, $lang = 'en_GB') {
  2347. $this->version = '0.31';
  2348. $this->file_src_name = '';
  2349. $this->file_src_name_body = '';
  2350. $this->file_src_name_ext = '';
  2351. $this->file_src_mime = '';
  2352. $this->file_src_size = '';
  2353. $this->file_src_error = '';
  2354. $this->file_src_pathname = '';
  2355. $this->file_src_temp = '';
  2356. $this->file_dst_path = '';
  2357. $this->file_dst_name = '';
  2358. $this->file_dst_name_body = '';
  2359. $this->file_dst_name_ext = '';
  2360. $this->file_dst_pathname = '';
  2361. $this->image_src_x = null;
  2362. $this->image_src_y = null;
  2363. $this->image_src_bits = null;
  2364. $this->image_src_type = null;
  2365. $this->image_src_pixels = null;
  2366. $this->image_dst_x = 0;
  2367. $this->image_dst_y = 0;
  2368. $this->uploaded = true;
  2369. $this->no_upload_check = false;
  2370. $this->processed = true;
  2371. $this->error = '';
  2372. $this->log = '';
  2373. $this->allowed = array();
  2374. $this->forbidden = array();
  2375. $this->file_is_image = false;
  2376. $this->init();
  2377. $info = null;
  2378. $mime_from_browser = null;
  2379. // sets default language
  2380. $this->translation = array();
  2381. $this->translation['file_error'] = 'File error. Please try again.';
  2382. $this->translation['local_file_missing'] = 'Local file doesn\'t exist.';
  2383. $this->translation['local_file_not_readable'] = 'Local file is not readable.';
  2384. $this->translation['uploaded_too_big_ini'] = 'File upload error (the uploaded file exceeds the upload_max_filesize directive in php.ini).';
  2385. $this->translation['uploaded_too_big_html'] = 'File upload error (the uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form).';
  2386. $this->translation['uploaded_partial'] = 'File upload error (the uploaded file was only partially uploaded).';
  2387. $this->translation['uploaded_missing'] = 'File upload error (no file was uploaded).';
  2388. $this->translation['uploaded_no_tmp_dir'] = 'File upload error (missing a temporary folder).';
  2389. $this->translation['uploaded_cant_write'] = 'File upload error (failed to write file to disk).';
  2390. $this->translation['uploaded_err_extension'] = 'File upload error (file upload stopped by extension).';
  2391. $this->translation['uploaded_unknown'] = 'File upload error (unknown error code).';
  2392. $this->translation['try_again'] = 'File upload error. Please try again.';
  2393. $this->translation['file_too_big'] = 'File too big.';
  2394. $this->translation['no_mime'] = 'MIME type can\'t be detected.';
  2395. $this->translation['incorrect_file'] = 'Incorrect type of file.';
  2396. $this->translation['image_too_wide'] = 'Image too wide.';
  2397. $this->translation['image_too_narrow'] = 'Image too narrow.';
  2398. $this->translation['image_too_high'] = 'Image too tall.';
  2399. $this->translation['image_too_short'] = 'Image too short.';
  2400. $this->translation['ratio_too_high'] = 'Image ratio too high (image too wide).';
  2401. $this->translation['ratio_too_low'] = 'Image ratio too low (image too high).';
  2402. $this->translation['too_many_pixels'] = 'Image has too many pixels.';
  2403. $this->translation['not_enough_pixels'] = 'Image has not enough pixels.';
  2404. $this->translation['file_not_uploaded'] = 'File not uploaded. Can\'t carry on a process.';
  2405. $this->translation['already_exists'] = '%s already exists. Please change the file name.';
  2406. $this->translation['temp_file_missing'] = 'No correct temp source file. Can\'t carry on a process.';
  2407. $this->translation['source_missing'] = 'No correct uploaded source file. Can\'t carry on a process.';
  2408. $this->translation['destination_dir'] = 'Destination directory can\'t be created. Can\'t carry on a process.';
  2409. $this->translation['destination_dir_missing'] = 'Destination directory doesn\'t exist. Can\'t carry on a process.';
  2410. $this->translation['destination_path_not_dir'] = 'Destination path is not a directory. Can\'t carry on a process.';
  2411. $this->translation['destination_dir_write'] = 'Destination directory can\'t be made writeable. Can\'t carry on a process.';
  2412. $this->translation['destination_path_write'] = 'Destination path is not a writeable. Can\'t carry on a process.';
  2413. $this->translation['temp_file'] = 'Can\'t create the temporary file. Can\'t carry on a process.';
  2414. $this->translation['source_not_readable'] = 'Source file is not readable. Can\'t carry on a process.';
  2415. $this->translation['no_create_support'] = 'No create from %s support.';
  2416. $this->translation['create_error'] = 'Error in creating %s image from source.';
  2417. $this->translation['source_invalid'] = 'Can\'t read image source. Not an image?.';
  2418. $this->translation['gd_missing'] = 'GD doesn\'t seem to be present.';
  2419. $this->translation['watermark_no_create_support'] = 'No create from %s support, can\'t read watermark.';
  2420. $this->translation['watermark_create_error'] = 'No %s read support, can\'t create watermark.';
  2421. $this->translation['watermark_invalid'] = 'Unknown image format, can\'t read watermark.';
  2422. $this->translation['file_create'] = 'No %s create support.';
  2423. $this->translation['no_conversion_type'] = 'No conversion type defined.';
  2424. $this->translation['copy_failed'] = 'Error copying file on the server. copy() failed.';
  2425. $this->translation['reading_failed'] = 'Error reading the file.';
  2426. // determines the language
  2427. $this->lang = $lang;
  2428. if ($this->lang != 'en_GB' && file_exists(dirname(__FILE__).'/lang') && file_exists(dirname(__FILE__).'/lang/class.upload.' . $lang . '.php')) {
  2429. $translation = null;
  2430. include(dirname(__FILE__).'/lang/class.upload.' . $lang . '.php');
  2431. if (is_array($translation)) {
  2432. $this->translation = array_merge($this->translation, $translation);
  2433. } else {
  2434. $this->lang = 'en_GB';
  2435. }
  2436. }
  2437. // determines the supported MIME types, and matching image format
  2438. $this->image_supported = array();
  2439. if ($this->gdversion()) {
  2440. if (imagetypes() & IMG_GIF) {
  2441. $this->image_supported['image/gif'] = 'gif';
  2442. }
  2443. if (imagetypes() & IMG_JPG) {
  2444. $this->image_supported['image/jpg'] = 'jpg';
  2445. $this->image_supported['image/jpeg'] = 'jpg';
  2446. $this->image_supported['image/pjpeg'] = 'jpg';
  2447. }
  2448. if (imagetypes() & IMG_PNG) {
  2449. $this->image_supported['image/png'] = 'png';
  2450. $this->image_supported['image/x-png'] = 'png';
  2451. }
  2452. if (imagetypes() & IMG_WBMP) {
  2453. $this->image_supported['image/bmp'] = 'bmp';
  2454. $this->image_supported['image/x-ms-bmp'] = 'bmp';
  2455. $this->image_supported['image/x-windows-bmp'] = 'bmp';
  2456. }
  2457. }
  2458. // display some system information
  2459. if (empty($this->log)) {
  2460. $this->log .= '<b>system information</b><br />';
  2461. if (function_exists('ini_get_all')) {
  2462. $inis = ini_get_all();
  2463. $open_basedir = (array_key_exists('open_basedir', $inis) && array_key_exists('local_value', $inis['open_basedir']) && !empty($inis['open_basedir']['local_value'])) ? $inis['open_basedir']['local_value'] : false;
  2464. } else {
  2465. $open_basedir = false;
  2466. }
  2467. $gd = $this->gdversion() ? $this->gdversion(true) : 'GD not present';
  2468. $supported = trim((in_array('png', $this->image_supported) ? 'png' : '') . ' ' . (in_array('jpg', $this->image_supported) ? 'jpg' : '') . ' ' . (in_array('gif', $this->image_supported) ? 'gif' : '') . ' ' . (in_array('bmp', $this->image_supported) ? 'bmp' : ''));
  2469. $this->log .= '-&nbsp;class version : ' . $this->version . '<br />';
  2470. $this->log .= '-&nbsp;operating system : ' . PHP_OS . '<br />';
  2471. $this->log .= '-&nbsp;PHP version : ' . PHP_VERSION . '<br />';
  2472. $this->log .= '-&nbsp;GD version : ' . $gd . '<br />';
  2473. $this->log .= '-&nbsp;supported image types : ' . (!empty($supported) ? $supported : 'none') . '<br />';
  2474. $this->log .= '-&nbsp;open_basedir : ' . (!empty($open_basedir) ? $open_basedir : 'no restriction') . '<br />';
  2475. $this->log .= '-&nbsp;upload_max_filesize : ' . $this->file_max_size_raw . ' (' . $this->file_max_size . ' bytes)<br />';
  2476. $this->log .= '-&nbsp;language : ' . $this->lang . '<br />';
  2477. }
  2478. if (!$file) {
  2479. $this->uploaded = false;
  2480. $this->error = $this->translate('file_error');
  2481. }
  2482. // check if we sent a local filename rather than a $_FILE element
  2483. if (!is_array($file)) {
  2484. if (empty($file)) {
  2485. $this->uploaded = false;
  2486. $this->error = $this->translate('file_error');
  2487. } else {
  2488. $this->no_upload_check = TRUE;
  2489. // this is a local filename, i.e.not uploaded
  2490. $this->log .= '<b>' . $this->translate("source is a local file") . ' ' . $file . '</b><br />';
  2491. if ($this->uploaded && !file_exists($file)) {
  2492. $this->uploaded = false;
  2493. $this->error = $this->translate('local_file_missing');
  2494. }
  2495. if ($this->uploaded && !is_readable($file)) {
  2496. $this->uploaded = false;
  2497. $this->error = $this->translate('local_file_not_readable');
  2498. }
  2499. if ($this->uploaded) {
  2500. $this->file_src_pathname = $file;
  2501. $this->file_src_name = basename($file);
  2502. $this->log .= '- local file name OK<br />';
  2503. preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
  2504. if (is_array($extension) && sizeof($extension) > 0) {
  2505. $this->file_src_name_ext = strtolower($extension[1]);
  2506. $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1);
  2507. } else {
  2508. $this->file_src_name_ext = '';
  2509. $this->file_src_name_body = $this->file_src_name;
  2510. }
  2511. $this->file_src_size = (file_exists($file) ? filesize($file) : 0);
  2512. }
  2513. $this->file_src_error = 0;
  2514. }
  2515. } else {
  2516. // this is an element from $_FILE, i.e. an uploaded file
  2517. $this->log .= '<b>source is an uploaded file</b><br />';
  2518. if ($this->uploaded) {
  2519. $this->file_src_error = trim($file['error']);
  2520. switch($this->file_src_error) {
  2521. case UPLOAD_ERR_OK:
  2522. // all is OK
  2523. $this->log .= '- upload OK<br />';
  2524. break;
  2525. case UPLOAD_ERR_INI_SIZE:
  2526. $this->uploaded = false;
  2527. $this->error = $this->translate('uploaded_too_big_ini');
  2528. break;
  2529. case UPLOAD_ERR_FORM_SIZE:
  2530. $this->uploaded = false;
  2531. $this->error = $this->translate('uploaded_too_big_html');
  2532. break;
  2533. case UPLOAD_ERR_PARTIAL:
  2534. $this->uploaded = false;
  2535. $this->error = $this->translate('uploaded_partial');
  2536. break;
  2537. case UPLOAD_ERR_NO_FILE:
  2538. $this->uploaded = false;
  2539. $this->error = $this->translate('uploaded_missing');
  2540. break;
  2541. case @UPLOAD_ERR_NO_TMP_DIR:
  2542. $this->uploaded = false;
  2543. $this->error = $this->translate('uploaded_no_tmp_dir');
  2544. break;
  2545. case @UPLOAD_ERR_CANT_WRITE:
  2546. $this->uploaded = false;
  2547. $this->error = $this->translate('uploaded_cant_write');
  2548. break;
  2549. case @UPLOAD_ERR_EXTENSION:
  2550. $this->uploaded = false;
  2551. $this->error = $this->translate('uploaded_err_extension');
  2552. break;
  2553. default:
  2554. $this->uploaded = false;
  2555. $this->error = $this->translate('uploaded_unknown') . ' ('.$this->file_src_error.')';
  2556. }
  2557. }
  2558. if ($this->uploaded) {
  2559. $this->file_src_pathname = $file['tmp_name'];
  2560. $this->file_src_name = $file['name'];
  2561. if ($this->file_src_name == '') {
  2562. $this->uploaded = false;
  2563. $this->error = $this->translate('try_again');
  2564. }
  2565. }
  2566. if ($this->uploaded) {
  2567. $this->log .= '- file name OK<br />';
  2568. preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
  2569. if (is_array($extension) && sizeof($extension) > 0) {
  2570. $this->file_src_name_ext = strtolower($extension[1]);
  2571. $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1);
  2572. } else {
  2573. $this->file_src_name_ext = '';
  2574. $this->file_src_name_body = $this->file_src_name;
  2575. }
  2576. $this->file_src_size = $file['size'];
  2577. $mime_from_browser = $file['type'];
  2578. }
  2579. }
  2580. if ($this->uploaded) {
  2581. $this->log .= '<b>determining MIME type</b><br />';
  2582. $this->file_src_mime = null;
  2583. // checks MIME type with Fileinfo PECL extension
  2584. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2585. if ($this->mime_fileinfo) {
  2586. $this->log .= '- Checking MIME type with Fileinfo PECL extension<br />';
  2587. if (function_exists('finfo_open')) {
  2588. if ($this->mime_fileinfo !== '') {
  2589. if ($this->mime_fileinfo === true) {
  2590. if (getenv('MAGIC') === FALSE) {
  2591. if (substr(PHP_OS, 0, 3) == 'WIN') {
  2592. $path = realpath(ini_get('extension_dir') . '/../') . 'extras/magic';
  2593. } else {
  2594. $path = '/usr/share/file/magic';
  2595. }
  2596. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path defaults to ' . $path . '<br />';
  2597. } else {
  2598. $path = getenv('MAGIC');
  2599. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' . $path . ' from MAGIC variable<br />';
  2600. }
  2601. } else {
  2602. $path = $this->mime_fileinfo;
  2603. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' . $path . '<br />';
  2604. }
  2605. $f = finfo_open(FILEINFO_MIME, $path);
  2606. } else {
  2607. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path will not be used<br />';
  2608. $f = finfo_open(FILEINFO_MIME);
  2609. }
  2610. if (is_resource($f)) {
  2611. $mime = finfo_file($f, realpath($this->file_src_pathname));
  2612. finfo_close($f);
  2613. $this->file_src_mime = $mime;
  2614. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  2615. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2616. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2617. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2618. } else {
  2619. $this->file_src_mime = null;
  2620. }
  2621. } else {
  2622. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo_open)<br />';
  2623. }
  2624. } elseif (class_exists('finfo', false)) {
  2625. $f = new finfo( FILEINFO_MIME );
  2626. if ($f) {
  2627. $this->file_src_mime = $f->file(realpath($this->file_src_pathname));
  2628. $this->log .= '- MIME type detected as ' . $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  2629. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2630. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2631. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2632. } else {
  2633. $this->file_src_mime = null;
  2634. }
  2635. } else {
  2636. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo)<br />';
  2637. }
  2638. } else {
  2639. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension not available<br />';
  2640. }
  2641. } else {
  2642. $this->log .= '- Fileinfo PECL extension deactivated<br />';
  2643. }
  2644. }
  2645. // checks MIME type with shell if unix access is authorized
  2646. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2647. if ($this->mime_file) {
  2648. $this->log .= '- Checking MIME type with UNIX file() command<br />';
  2649. if (substr(PHP_OS, 0, 3) != 'WIN') {
  2650. if (function_exists('exec')) {
  2651. if (strlen($mime = exec("file -bi ".escapeshellarg($this->file_src_pathname))) != 0) {
  2652. $this->file_src_mime = trim($mime);
  2653. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by UNIX file() command<br />';
  2654. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2655. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2656. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2657. } else {
  2658. $this->file_src_mime = null;
  2659. }
  2660. } else {
  2661. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command failed<br />';
  2662. }
  2663. } else {
  2664. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PHP exec() function is disabled<br />';
  2665. }
  2666. } else {
  2667. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command not availabled<br />';
  2668. }
  2669. } else {
  2670. $this->log .= '- UNIX file() command is deactivated<br />';
  2671. }
  2672. }
  2673. // checks MIME type with mime_magic
  2674. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2675. if ($this->mime_magic) {
  2676. $this->log .= '- Checking MIME type with mime.magic file (mime_content_type())<br />';
  2677. if (function_exists('mime_content_type')) {
  2678. $this->file_src_mime = mime_content_type($this->file_src_pathname);
  2679. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by mime_content_type()<br />';
  2680. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2681. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2682. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2683. } else {
  2684. $this->file_src_mime = null;
  2685. }
  2686. } else {
  2687. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;mime_content_type() is not available<br />';
  2688. }
  2689. } else {
  2690. $this->log .= '- mime.magic file (mime_content_type()) is deactivated<br />';
  2691. }
  2692. }
  2693. // checks MIME type with getimagesize()
  2694. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2695. if ($this->mime_getimagesize) {
  2696. $this->log .= '- Checking MIME type with getimagesize()<br />';
  2697. $info = getimagesize($this->file_src_pathname);
  2698. if (is_array($info) && array_key_exists('mime', $info)) {
  2699. $this->file_src_mime = trim($info['mime']);
  2700. if (empty($this->file_src_mime)) {
  2701. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME empty, guessing from type<br />';
  2702. $mime = (is_array($info) && array_key_exists(2, $info) ? $info[2] : null); // 1 = GIF, 2 = JPG, 3 = PNG
  2703. $this->file_src_mime = ($mime==IMAGETYPE_GIF ? 'image/gif' : ($mime==IMAGETYPE_JPEG ? 'image/jpeg' : ($mime==IMAGETYPE_PNG ? 'image/png' : ($mime==IMAGETYPE_BMP ? 'image/bmp' : null))));
  2704. }
  2705. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by PHP getimagesize() function<br />';
  2706. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2707. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2708. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2709. } else {
  2710. $this->file_src_mime = null;
  2711. }
  2712. } else {
  2713. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;getimagesize() failed<br />';
  2714. }
  2715. } else {
  2716. $this->log .= '- getimagesize() is deactivated<br />';
  2717. }
  2718. }
  2719. // default to MIME from browser (or Flash)
  2720. if (!empty($mime_from_browser) && !$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime)) {
  2721. $this->file_src_mime =$mime_from_browser;
  2722. $this->log .= '- MIME type detected as ' . $this->file_src_mime . ' by browser<br />';
  2723. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2724. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2725. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2726. } else {
  2727. $this->file_src_mime = null;
  2728. }
  2729. }
  2730. // we need to work some magic if we upload via Flash
  2731. if ($this->file_src_mime == 'application/octet-stream' || !$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2732. if ($this->file_src_mime == 'application/octet-stream') $this->log .= '- Flash may be rewriting MIME as application/octet-stream<br />';
  2733. $this->log .= '- Try to guess MIME type from file extension (' . $this->file_src_name_ext . '): ';
  2734. if (array_key_exists($this->file_src_name_ext, $this->mime_types)) $this->file_src_mime = $this->mime_types[$this->file_src_name_ext];
  2735. if ($this->file_src_mime == 'application/octet-stream') {
  2736. $this->log .= 'doesn\'t look like anything known<br />';
  2737. } else {
  2738. $this->log .= 'MIME type set to ' . $this->file_src_mime . '<br />';
  2739. }
  2740. }
  2741. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2742. $this->log .= '- MIME type couldn\'t be detected! (' . (string) $this->file_src_mime . ')<br />';
  2743. }
  2744. // determine whether the file is an image
  2745. if ($this->file_src_mime && is_string($this->file_src_mime) && !empty($this->file_src_mime) && array_key_exists($this->file_src_mime, $this->image_supported)) {
  2746. $this->file_is_image = true;
  2747. $this->image_src_type = $this->image_supported[$this->file_src_mime];
  2748. }
  2749. // if the file is an image, we gather some useful data
  2750. if ($this->file_is_image) {
  2751. if ($h = fopen($this->file_src_pathname, 'r')) {
  2752. fclose($h);
  2753. $info = getimagesize($this->file_src_pathname);
  2754. if (is_array($info)) {
  2755. $this->image_src_x = $info[0];
  2756. $this->image_src_y = $info[1];
  2757. $this->image_dst_x = $this->image_src_x;
  2758. $this->image_dst_y = $this->image_src_y;
  2759. $this->image_src_pixels = $this->image_src_x * $this->image_src_y;
  2760. $this->image_src_bits = array_key_exists('bits', $info) ? $info['bits'] : null;
  2761. } else {
  2762. $this->file_is_image = false;
  2763. $this->uploaded = false;
  2764. $this->log .= '- can\'t retrieve image information, image may have been tampered with<br />';
  2765. $this->error = $this->translate('source_invalid');
  2766. }
  2767. } else {
  2768. $this->log .= '- can\'t read source file directly. open_basedir restriction in place?<br />';
  2769. }
  2770. }
  2771. $this->log .= '<b>source variables</b><br />';
  2772. $this->log .= '- You can use all these before calling process()<br />';
  2773. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name : ' . $this->file_src_name . '<br />';
  2774. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_body : ' . $this->file_src_name_body . '<br />';
  2775. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_ext : ' . $this->file_src_name_ext . '<br />';
  2776. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_pathname : ' . $this->file_src_pathname . '<br />';
  2777. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_mime : ' . $this->file_src_mime . '<br />';
  2778. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_size : ' . $this->file_src_size . ' (max= ' . $this->file_max_size . ')<br />';
  2779. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_error : ' . $this->file_src_error . '<br />';
  2780. if ($this->file_is_image) {
  2781. $this->log .= '- source file is an image<br />';
  2782. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x : ' . $this->image_src_x . '<br />';
  2783. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_y : ' . $this->image_src_y . '<br />';
  2784. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_pixels : ' . $this->image_src_pixels . '<br />';
  2785. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_type : ' . $this->image_src_type . '<br />';
  2786. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_bits : ' . $this->image_src_bits . '<br />';
  2787. }
  2788. }
  2789. }
  2790. /**
  2791. * Returns the version of GD
  2792. *
  2793. * @access public
  2794. * @param boolean $full Optional flag to get precise version
  2795. * @return float GD version
  2796. */
  2797. function gdversion($full = false) {
  2798. static $gd_version = null;
  2799. static $gd_full_version = null;
  2800. if ($gd_version === null) {
  2801. if (function_exists('gd_info')) {
  2802. $gd = gd_info();
  2803. $gd = $gd["GD Version"];
  2804. $regex = "/([\d\.]+)/i";
  2805. } else {
  2806. ob_start();
  2807. phpinfo(8);
  2808. $gd = ob_get_contents();
  2809. ob_end_clean();
  2810. $regex = "/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i";
  2811. }
  2812. if (preg_match($regex, $gd, $m)) {
  2813. $gd_full_version = (string) $m[1];
  2814. $gd_version = (float) $m[1];
  2815. } else {
  2816. $gd_full_version = 'none';
  2817. $gd_version = 0;
  2818. }
  2819. }
  2820. if ($full) {
  2821. return $gd_full_version;
  2822. } else {
  2823. return $gd_version;
  2824. }
  2825. }
  2826. /**
  2827. * Creates directories recursively
  2828. *
  2829. * @access private
  2830. * @param string $path Path to create
  2831. * @param integer $mode Optional permissions
  2832. * @return boolean Success
  2833. */
  2834. function rmkdir($path, $mode = 0777) {
  2835. return is_dir($path) || ( $this->rmkdir(dirname($path), $mode) && $this->_mkdir($path, $mode) );
  2836. }
  2837. /**
  2838. * Creates directory
  2839. *
  2840. * @access private
  2841. * @param string $path Path to create
  2842. * @param integer $mode Optional permissions
  2843. * @return boolean Success
  2844. */
  2845. function _mkdir($path, $mode = 0777) {
  2846. $old = umask(0);
  2847. $res = mkdir($path, $mode);
  2848. umask($old);
  2849. return $res;
  2850. }
  2851. /**
  2852. * Translate error messages
  2853. *
  2854. * @access private
  2855. * @param string $str Message to translate
  2856. * @param array $tokens Optional token values
  2857. * @return string Translated string
  2858. */
  2859. function translate($str, $tokens = array()) {
  2860. if (array_key_exists($str, $this->translation)) $str = $this->translation[$str];
  2861. if (is_array($tokens) && sizeof($tokens) > 0) $str = vsprintf($str, $tokens);
  2862. return $str;
  2863. }
  2864. /**
  2865. * Decodes colors
  2866. *
  2867. * @access private
  2868. * @param string $color Color string
  2869. * @return array RGB colors
  2870. */
  2871. function getcolors($color) {
  2872. $r = sscanf($color, "#%2x%2x%2x");
  2873. $red = (array_key_exists(0, $r) && is_numeric($r[0]) ? $r[0] : 0);
  2874. $green = (array_key_exists(1, $r) && is_numeric($r[1]) ? $r[1] : 0);
  2875. $blue = (array_key_exists(2, $r) && is_numeric($r[2]) ? $r[2] : 0);
  2876. return array($red, $green, $blue);
  2877. }
  2878. /**
  2879. * Decodes sizes
  2880. *
  2881. * @access private
  2882. * @param string $size Size in bytes, or shorthand byte options
  2883. * @return integer Size in bytes
  2884. */
  2885. function getsize($size) {
  2886. $last = strtolower($size{strlen($size)-1});
  2887. switch($last) {
  2888. case 'g':
  2889. $size *= 1024;
  2890. case 'm':
  2891. $size *= 1024;
  2892. case 'k':
  2893. $size *= 1024;
  2894. }
  2895. return $size;
  2896. }
  2897. /**
  2898. * Decodes offsets
  2899. *
  2900. * @access private
  2901. * @param misc $offsets Offsets, as an integer, a string or an array
  2902. * @param integer $x Reference picture width
  2903. * @param integer $y Reference picture height
  2904. * @param boolean $round Round offsets before returning them
  2905. * @param boolean $negative Allow negative offsets to be returned
  2906. * @return array Array of four offsets (TRBL)
  2907. */
  2908. function getoffsets($offsets, $x, $y, $round = true, $negative = true) {
  2909. if (!is_array($offsets)) $offsets = explode(' ', $offsets);
  2910. if (sizeof($offsets) == 4) {
  2911. $ct = $offsets[0]; $cr = $offsets[1]; $cb = $offsets[2]; $cl = $offsets[3];
  2912. } else if (sizeof($offsets) == 2) {
  2913. $ct = $offsets[0]; $cr = $offsets[1]; $cb = $offsets[0]; $cl = $offsets[1];
  2914. } else {
  2915. $ct = $offsets[0]; $cr = $offsets[0]; $cb = $offsets[0]; $cl = $offsets[0];
  2916. }
  2917. if (strpos($ct, '%')>0) $ct = $y * (str_replace('%','',$ct) / 100);
  2918. if (strpos($cr, '%')>0) $cr = $x * (str_replace('%','',$cr) / 100);
  2919. if (strpos($cb, '%')>0) $cb = $y * (str_replace('%','',$cb) / 100);
  2920. if (strpos($cl, '%')>0) $cl = $x * (str_replace('%','',$cl) / 100);
  2921. if (strpos($ct, 'px')>0) $ct = str_replace('px','',$ct);
  2922. if (strpos($cr, 'px')>0) $cr = str_replace('px','',$cr);
  2923. if (strpos($cb, 'px')>0) $cb = str_replace('px','',$cb);
  2924. if (strpos($cl, 'px')>0) $cl = str_replace('px','',$cl);
  2925. $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl;
  2926. if ($round) {
  2927. $ct = round($ct);
  2928. $cr = round($cr);
  2929. $cb = round($cb);
  2930. $cl = round($cl);
  2931. }
  2932. if (!$negative) {
  2933. if ($ct < 0) $ct = 0;
  2934. if ($cr < 0) $cr = 0;
  2935. if ($cb < 0) $cb = 0;
  2936. if ($cl < 0) $cl = 0;
  2937. }
  2938. return array($ct, $cr, $cb, $cl);
  2939. }
  2940. /**
  2941. * Creates a container image
  2942. *
  2943. * @access private
  2944. * @param integer $x Width
  2945. * @param integer $y Height
  2946. * @param boolean $fill Optional flag to draw the background color or not
  2947. * @param boolean $trsp Optional flag to set the background to be transparent
  2948. * @return resource Container image
  2949. */
  2950. function imagecreatenew($x, $y, $fill = true, $trsp = false) {
  2951. if ($x < 1) $x = 1; if ($y < 1) $y = 1;
  2952. if ($this->gdversion() >= 2 && !$this->image_is_palette) {
  2953. // create a true color image
  2954. $dst_im = imagecreatetruecolor($x, $y);
  2955. // this preserves transparency in PNGs, in true color
  2956. if (empty($this->image_background_color) || $trsp) {
  2957. imagealphablending($dst_im, false );
  2958. imagefilledrectangle($dst_im, 0, 0, $x, $y, imagecolorallocatealpha($dst_im, 0, 0, 0, 127));
  2959. }
  2960. } else {
  2961. // creates a palette image
  2962. $dst_im = imagecreate($x, $y);
  2963. // preserves transparency for palette images, if the original image has transparency
  2964. if (($fill && $this->image_is_transparent && empty($this->image_background_color)) || $trsp) {
  2965. imagefilledrectangle($dst_im, 0, 0, $x, $y, $this->image_transparent_color);
  2966. imagecolortransparent($dst_im, $this->image_transparent_color);
  2967. }
  2968. }
  2969. // fills with background color if any is set
  2970. if ($fill && !empty($this->image_background_color) && !$trsp) {
  2971. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  2972. $background_color = imagecolorallocate($dst_im, $red, $green, $blue);
  2973. imagefilledrectangle($dst_im, 0, 0, $x, $y, $background_color);
  2974. }
  2975. return $dst_im;
  2976. }
  2977. /**
  2978. * Transfers an image from the container to the destination image
  2979. *
  2980. * @access private
  2981. * @param resource $src_im Container image
  2982. * @param resource $dst_im Destination image
  2983. * @return resource Destination image
  2984. */
  2985. function imagetransfer($src_im, $dst_im) {
  2986. if (is_resource($dst_im)) imagedestroy($dst_im);
  2987. $dst_im = & $src_im;
  2988. return $dst_im;
  2989. }
  2990. /**
  2991. * Merges two images
  2992. *
  2993. * If the output format is PNG, then we do it pixel per pixel to retain the alpha channel
  2994. *
  2995. * @access private
  2996. * @param resource $dst_img Destination image
  2997. * @param resource $src_img Overlay image
  2998. * @param int $dst_x x-coordinate of destination point
  2999. * @param int $dst_y y-coordinate of destination point
  3000. * @param int $src_x x-coordinate of source point
  3001. * @param int $src_y y-coordinate of source point
  3002. * @param int $src_w Source width
  3003. * @param int $src_h Source height
  3004. * @param int $pct Optional percentage of the overlay, between 0 and 100 (default: 100)
  3005. * @return resource Destination image
  3006. */
  3007. function imagecopymergealpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct = 0) {
  3008. $dst_x = (int) $dst_x;
  3009. $dst_y = (int) $dst_y;
  3010. $src_x = (int) $src_x;
  3011. $src_y = (int) $src_y;
  3012. $src_w = (int) $src_w;
  3013. $src_h = (int) $src_h;
  3014. $pct = (int) $pct;
  3015. $dst_w = imagesx($dst_im);
  3016. $dst_h = imagesy($dst_im);
  3017. for ($y = $src_y; $y < $src_h; $y++) {
  3018. for ($x = $src_x; $x < $src_w; $x++) {
  3019. if ($x + $dst_x >= 0 && $x + $dst_x < $dst_w && $x + $src_x >= 0 && $x + $src_x < $src_w
  3020. && $y + $dst_y >= 0 && $y + $dst_y < $dst_h && $y + $src_y >= 0 && $y + $src_y < $src_h) {
  3021. $dst_pixel = imagecolorsforindex($dst_im, imagecolorat($dst_im, $x + $dst_x, $y + $dst_y));
  3022. $src_pixel = imagecolorsforindex($src_im, imagecolorat($src_im, $x + $src_x, $y + $src_y));
  3023. $src_alpha = 1 - ($src_pixel['alpha'] / 127);
  3024. $dst_alpha = 1 - ($dst_pixel['alpha'] / 127);
  3025. $opacity = $src_alpha * $pct / 100;
  3026. if ($dst_alpha >= $opacity) $alpha = $dst_alpha;
  3027. if ($dst_alpha < $opacity) $alpha = $opacity;
  3028. if ($alpha > 1) $alpha = 1;
  3029. if ($opacity > 0) {
  3030. $dst_red = round(( ($dst_pixel['red'] * $dst_alpha * (1 - $opacity)) ) );
  3031. $dst_green = round(( ($dst_pixel['green'] * $dst_alpha * (1 - $opacity)) ) );
  3032. $dst_blue = round(( ($dst_pixel['blue'] * $dst_alpha * (1 - $opacity)) ) );
  3033. $src_red = round((($src_pixel['red'] * $opacity)) );
  3034. $src_green = round((($src_pixel['green'] * $opacity)) );
  3035. $src_blue = round((($src_pixel['blue'] * $opacity)) );
  3036. $red = round(($dst_red + $src_red ) / ($dst_alpha * (1 - $opacity) + $opacity));
  3037. $green = round(($dst_green + $src_green) / ($dst_alpha * (1 - $opacity) + $opacity));
  3038. $blue = round(($dst_blue + $src_blue ) / ($dst_alpha * (1 - $opacity) + $opacity));
  3039. if ($red > 255) $red = 255;
  3040. if ($green > 255) $green = 255;
  3041. if ($blue > 255) $blue = 255;
  3042. $alpha = round((1 - $alpha) * 127);
  3043. $color = imagecolorallocatealpha($dst_im, $red, $green, $blue, $alpha);
  3044. imagesetpixel($dst_im, $x + $dst_x, $y + $dst_y, $color);
  3045. }
  3046. }
  3047. }
  3048. }
  3049. return true;
  3050. }
  3051. /**
  3052. * Actually uploads the file, and act on it according to the set processing class variables
  3053. *
  3054. * This function copies the uploaded file to the given location, eventually performing actions on it.
  3055. * Typically, you can call {@link process} several times for the same file,
  3056. * for instance to create a resized image and a thumbnail of the same file.
  3057. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times.
  3058. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls.
  3059. *
  3060. * According to the processing class variables set in the calling file, the file can be renamed,
  3061. * and if it is an image, can be resized or converted.
  3062. *
  3063. * When the processing is completed, and the file copied to its new location, the
  3064. * processing class variables will be reset to their default value.
  3065. * This allows you to set new properties, and perform another {@link process} on the same uploaded file
  3066. *
  3067. * If the function is called with a null or empty argument, then it will return the content of the picture
  3068. *
  3069. * It will set {@link processed} (and {@link error} is an error occurred)
  3070. *
  3071. * @access public
  3072. * @param string $server_path Optional path location of the uploaded file, with an ending slash
  3073. * @return string Optional content of the image
  3074. */
  3075. function process($server_path = null) {
  3076. $this->error = '';
  3077. $this->processed = true;
  3078. $return_mode = false;
  3079. $return_content = null;
  3080. // clean up dst variables
  3081. $this->file_dst_path = '';
  3082. $this->file_dst_pathname = '';
  3083. $this->file_dst_name = '';
  3084. $this->file_dst_name_body = '';
  3085. $this->file_dst_name_ext = '';
  3086. // clean up some parameters
  3087. $this->file_max_size = $this->getsize($this->file_max_size);
  3088. $this->jpeg_size = $this->getsize($this->jpeg_size);
  3089. // some parameters are being deprecated, and replaced with others
  3090. if (is_null($this->image_overlay_opacity)) $this->image_overlay_opacity = $this->image_overlay_percent;
  3091. if ($this->image_text_opacity == 100) $this->image_text_opacity = $this->image_text_percent;
  3092. if ($this->image_text_background_opacity == 100) $this->image_text_background_opacity = $this->image_text_background_percent;
  3093. // copy some variables as we need to keep them clean
  3094. $file_src_name = $this->file_src_name;
  3095. $file_src_name_body = $this->file_src_name_body;
  3096. $file_src_name_ext = $this->file_src_name_ext;
  3097. if (!$this->uploaded) {
  3098. $this->error = $this->translate('file_not_uploaded');
  3099. $this->processed = false;
  3100. }
  3101. if ($this->processed) {
  3102. if (empty($server_path) || is_null($server_path)) {
  3103. $this->log .= '<b>process file and return the content</b><br />';
  3104. $return_mode = true;
  3105. } else {
  3106. if(strtolower(substr(PHP_OS, 0, 3)) === 'win') {
  3107. if (substr($server_path, -1, 1) != '\\') $server_path = $server_path . '\\';
  3108. } else {
  3109. if (substr($server_path, -1, 1) != '/') $server_path = $server_path . '/';
  3110. }
  3111. $this->log .= '<b>process file to ' . $server_path . '</b><br />';
  3112. }
  3113. }
  3114. if ($this->processed) {
  3115. // checks file max size
  3116. if ($this->file_src_size > $this->file_max_size) {
  3117. $this->processed = false;
  3118. $this->error = $this->translate('file_too_big');
  3119. } else {
  3120. $this->log .= '- file size OK<br />';
  3121. }
  3122. }
  3123. if ($this->processed) {
  3124. // if we have an image without extension, set it
  3125. if ($this->file_force_extension && $this->file_is_image && !$this->file_src_name_ext) $file_src_name_ext = $this->image_src_type;
  3126. // turn dangerous scripts into text files
  3127. if ($this->no_script) {
  3128. // if the file has no extension, we try to guess it from the MIME type
  3129. if ($this->file_force_extension && empty($file_src_name_ext)) {
  3130. if ($key = array_search($this->file_src_mime, $this->mime_types)) {
  3131. $file_src_name_ext = $key;
  3132. $file_src_name = $file_src_name_body . '.' . $file_src_name_ext;
  3133. $this->log .= '- file renamed as ' . $file_src_name_body . '.' . $file_src_name_ext . '!<br />';
  3134. }
  3135. }
  3136. // if the file is text based, or has a dangerous extension, we rename it as .txt
  3137. if ((((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf') || strpos($this->file_src_mime, 'javascript') !== false) && (substr($file_src_name, -4) != '.txt'))
  3138. || preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $this->file_src_name)
  3139. || $this->file_force_extension && empty($file_src_name_ext)) {
  3140. $this->file_src_mime = 'text/plain';
  3141. if ($this->file_src_name_ext) $file_src_name_body = $file_src_name_body . '.' . $this->file_src_name_ext;
  3142. $file_src_name_ext = 'txt';
  3143. $file_src_name = $file_src_name_body . '.' . $file_src_name_ext;
  3144. $this->log .= '- script renamed as ' . $file_src_name_body . '.' . $file_src_name_ext . '!<br />';
  3145. }
  3146. }
  3147. if ($this->mime_check && empty($this->file_src_mime)) {
  3148. $this->processed = false;
  3149. $this->error = $this->translate('no_mime');
  3150. } else if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) {
  3151. list($m1, $m2) = explode('/', $this->file_src_mime);
  3152. $allowed = false;
  3153. // check wether the mime type is allowed
  3154. if (!is_array($this->allowed)) $this->allowed = array($this->allowed);
  3155. foreach($this->allowed as $k => $v) {
  3156. list($v1, $v2) = explode('/', $v);
  3157. if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  3158. $allowed = true;
  3159. break;
  3160. }
  3161. }
  3162. // check wether the mime type is forbidden
  3163. if (!is_array($this->forbidden)) $this->forbidden = array($this->forbidden);
  3164. foreach($this->forbidden as $k => $v) {
  3165. list($v1, $v2) = explode('/', $v);
  3166. if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  3167. $allowed = false;
  3168. break;
  3169. }
  3170. }
  3171. if (!$allowed) {
  3172. $this->processed = false;
  3173. $this->error = $this->translate('incorrect_file');
  3174. } else {
  3175. $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />';
  3176. }
  3177. } else {
  3178. $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />';
  3179. }
  3180. // if the file is an image, we can check on its dimensions
  3181. // these checks are not available if open_basedir restrictions are in place
  3182. if ($this->file_is_image) {
  3183. if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) {
  3184. $ratio = $this->image_src_x / $this->image_src_y;
  3185. if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) {
  3186. $this->processed = false;
  3187. $this->error = $this->translate('image_too_wide');
  3188. }
  3189. if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) {
  3190. $this->processed = false;
  3191. $this->error = $this->translate('image_too_narrow');
  3192. }
  3193. if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) {
  3194. $this->processed = false;
  3195. $this->error = $this->translate('image_too_high');
  3196. }
  3197. if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) {
  3198. $this->processed = false;
  3199. $this->error = $this->translate('image_too_short');
  3200. }
  3201. if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) {
  3202. $this->processed = false;
  3203. $this->error = $this->translate('ratio_too_high');
  3204. }
  3205. if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) {
  3206. $this->processed = false;
  3207. $this->error = $this->translate('ratio_too_low');
  3208. }
  3209. if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) {
  3210. $this->processed = false;
  3211. $this->error = $this->translate('too_many_pixels');
  3212. }
  3213. if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) {
  3214. $this->processed = false;
  3215. $this->error = $this->translate('not_enough_pixels');
  3216. }
  3217. } else {
  3218. $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />';
  3219. }
  3220. }
  3221. }
  3222. if ($this->processed) {
  3223. $this->file_dst_path = $server_path;
  3224. // repopulate dst variables from src
  3225. $this->file_dst_name = $file_src_name;
  3226. $this->file_dst_name_body = $file_src_name_body;
  3227. $this->file_dst_name_ext = $file_src_name_ext;
  3228. if ($this->file_overwrite) $this->file_auto_rename = false;
  3229. if ($this->image_convert && $this->file_is_image) { // if we convert as an image
  3230. if ($this->file_src_name_ext) $this->file_dst_name_ext = $this->image_convert;
  3231. $this->log .= '- new file name ext : ' . $this->image_convert . '<br />';
  3232. }
  3233. if (!is_null($this->file_new_name_body)) { // rename file body
  3234. $this->file_dst_name_body = $this->file_new_name_body;
  3235. $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />';
  3236. }
  3237. if (!is_null($this->file_new_name_ext)) { // rename file ext
  3238. $this->file_dst_name_ext = $this->file_new_name_ext;
  3239. $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />';
  3240. }
  3241. if (!is_null($this->file_name_body_add)) { // append a string to the name
  3242. $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add;
  3243. $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />';
  3244. }
  3245. if (!is_null($this->file_name_body_pre)) { // prepend a string to the name
  3246. $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body;
  3247. $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />';
  3248. }
  3249. if ($this->file_safe_name) { // formats the name
  3250. $this->file_dst_name_body = strtr($this->file_dst_name_body, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
  3251. $this->file_dst_name_body = strtr($this->file_dst_name_body, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));
  3252. $this->file_dst_name_body = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $this->file_dst_name_body);
  3253. $this->log .= '- file name safe format<br />';
  3254. }
  3255. $this->log .= '- destination variables<br />';
  3256. if (empty($this->file_dst_path) || is_null($this->file_dst_path)) {
  3257. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path : n/a<br />';
  3258. } else {
  3259. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path : ' . $this->file_dst_path . '<br />';
  3260. }
  3261. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_body : ' . $this->file_dst_name_body . '<br />';
  3262. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />';
  3263. // do we do some image manipulation?
  3264. $image_manipulation = ($this->file_is_image && (
  3265. $this->image_resize
  3266. || $this->image_convert != ''
  3267. || is_numeric($this->image_brightness)
  3268. || is_numeric($this->image_contrast)
  3269. || is_numeric($this->image_opacity)
  3270. || is_numeric($this->image_threshold)
  3271. || !empty($this->image_tint_color)
  3272. || !empty($this->image_overlay_color)
  3273. || $this->image_unsharp
  3274. || !empty($this->image_text)
  3275. || $this->image_greyscale
  3276. || $this->image_negative
  3277. || !empty($this->image_watermark)
  3278. || is_numeric($this->image_rotate)
  3279. || is_numeric($this->jpeg_size)
  3280. || !empty($this->image_flip)
  3281. || !empty($this->image_crop)
  3282. || !empty($this->image_precrop)
  3283. || !empty($this->image_border)
  3284. || !empty($this->image_border_transparent)
  3285. || $this->image_frame > 0
  3286. || $this->image_bevel > 0
  3287. || $this->image_reflection_height));
  3288. // set the destination file name
  3289. $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  3290. if (!$return_mode) {
  3291. if (!$this->file_auto_rename) {
  3292. $this->log .= '- no auto_rename if same filename exists<br />';
  3293. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  3294. } else {
  3295. $this->log .= '- checking for auto_rename<br />';
  3296. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  3297. $body = $this->file_dst_name_body;
  3298. $ext = '';
  3299. // if we have changed the extension, then we add our increment before
  3300. if ($file_src_name_ext != $this->file_src_name_ext) {
  3301. if (substr($this->file_dst_name_body, -1 - strlen($this->file_src_name_ext)) == '.' . $this->file_src_name_ext) {
  3302. $body = substr($this->file_dst_name_body, 0, strlen($this->file_dst_name_body) - 1 - strlen($this->file_src_name_ext));
  3303. $ext = '.' . $this->file_src_name_ext;
  3304. }
  3305. }
  3306. $cpt = 1;
  3307. while (file_exists($this->file_dst_pathname)) {
  3308. $this->file_dst_name_body = $body . '_' . $cpt . $ext;
  3309. $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  3310. $cpt++;
  3311. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  3312. }
  3313. if ($cpt>1) $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;auto_rename to ' . $this->file_dst_name . '<br />';
  3314. }
  3315. $this->log .= '- destination file details<br />';
  3316. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name : ' . $this->file_dst_name . '<br />';
  3317. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_pathname : ' . $this->file_dst_pathname . '<br />';
  3318. if ($this->file_overwrite) {
  3319. $this->log .= '- no overwrite checking<br />';
  3320. } else {
  3321. if (@file_exists($this->file_dst_pathname)) {
  3322. $this->processed = false;
  3323. $this->error = $this->translate('already_exists', array($this->file_dst_name));
  3324. } else {
  3325. $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />';
  3326. }
  3327. }
  3328. }
  3329. }
  3330. if ($this->processed) {
  3331. // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists
  3332. if (!empty($this->file_src_temp)) {
  3333. $this->log .= '- use the temp file instead of the original file since it is a second process<br />';
  3334. $this->file_src_pathname = $this->file_src_temp;
  3335. if (!file_exists($this->file_src_pathname)) {
  3336. $this->processed = false;
  3337. $this->error = $this->translate('temp_file_missing');
  3338. }
  3339. // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file()
  3340. } else if (!$this->no_upload_check) {
  3341. if (!is_uploaded_file($this->file_src_pathname)) {
  3342. $this->processed = false;
  3343. $this->error = $this->translate('source_missing');
  3344. }
  3345. // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists()
  3346. } else {
  3347. if (!file_exists($this->file_src_pathname)) {
  3348. $this->processed = false;
  3349. $this->error = $this->translate('source_missing');
  3350. }
  3351. }
  3352. // checks if the destination directory exists, and attempt to create it
  3353. if (!$return_mode) {
  3354. if ($this->processed && !file_exists($this->file_dst_path)) {
  3355. if ($this->dir_auto_create) {
  3356. $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:';
  3357. if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) {
  3358. $this->log .= ' failed<br />';
  3359. $this->processed = false;
  3360. $this->error = $this->translate('destination_dir');
  3361. } else {
  3362. $this->log .= ' success<br />';
  3363. }
  3364. } else {
  3365. $this->error = $this->translate('destination_dir_missing');
  3366. }
  3367. }
  3368. if ($this->processed && !is_dir($this->file_dst_path)) {
  3369. $this->processed = false;
  3370. $this->error = $this->translate('destination_path_not_dir');
  3371. }
  3372. // checks if the destination directory is writeable, and attempt to make it writeable
  3373. $hash = md5($this->file_dst_name_body . rand(1, 1000));
  3374. if ($this->processed && !($f = fopen($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''), 'a+'))) {
  3375. if ($this->dir_auto_chmod) {
  3376. $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:';
  3377. if (!chmod($this->file_dst_path, $this->dir_chmod)) {
  3378. $this->log .= ' failed<br />';
  3379. $this->processed = false;
  3380. $this->error = $this->translate('destination_dir_write');
  3381. } else {
  3382. $this->log .= ' success<br />';
  3383. if (!($f = fopen($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''), 'a+'))) { // we re-check
  3384. $this->processed = false;
  3385. $this->error = $this->translate('destination_dir_write');
  3386. } else {
  3387. fclose($f);
  3388. }
  3389. }
  3390. } else {
  3391. $this->processed = false;
  3392. $this->error = $this->translate('destination_path_write');
  3393. }
  3394. } else {
  3395. if ($this->processed) fclose($f);
  3396. unlink($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''));
  3397. }
  3398. // 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)
  3399. // then we create a temp file that will be used as the source file in subsequent processes
  3400. // 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)
  3401. if (!$this->no_upload_check && empty($this->file_src_temp) && !file_exists($this->file_src_pathname)) {
  3402. $this->log .= '- attempting to use a temp file:';
  3403. $hash = md5($this->file_dst_name_body . rand(1, 1000));
  3404. if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''))) {
  3405. $this->file_src_pathname = $this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  3406. $this->file_src_temp = $this->file_src_pathname;
  3407. $this->log .= ' file created<br />';
  3408. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;temp file is: ' . $this->file_src_temp . '<br />';
  3409. } else {
  3410. $this->log .= ' failed<br />';
  3411. $this->processed = false;
  3412. $this->error = $this->translate('temp_file');
  3413. }
  3414. }
  3415. }
  3416. }
  3417. if ($this->processed) {
  3418. // we do a quick check to ensure the file is really an image
  3419. // we can do this only now, as it would have failed before in case of open_basedir
  3420. if ($image_manipulation && !getimagesize($this->file_src_pathname)) {
  3421. $this->log .= '- the file is not an image!<br />';
  3422. $image_manipulation = false;
  3423. }
  3424. if ($image_manipulation) {
  3425. // checks if the source file is readable
  3426. if ($this->processed && !($f = fopen($this->file_src_pathname, 'r'))) {
  3427. $this->processed = false;
  3428. $this->error = $this->translate('source_not_readable');
  3429. } else {
  3430. fclose($f);
  3431. }
  3432. // we now do all the image manipulations
  3433. $this->log .= '- image resizing or conversion wanted<br />';
  3434. if ($this->gdversion()) {
  3435. switch($this->image_src_type) {
  3436. case 'jpg':
  3437. if (!function_exists('imagecreatefromjpeg')) {
  3438. $this->processed = false;
  3439. $this->error = $this->translate('no_create_support', array('JPEG'));
  3440. } else {
  3441. $image_src = imagecreatefromjpeg($this->file_src_pathname);
  3442. if (!$image_src) {
  3443. $this->processed = false;
  3444. $this->error = $this->translate('create_error', array('JPEG'));
  3445. } else {
  3446. $this->log .= '- source image is JPEG<br />';
  3447. }
  3448. }
  3449. break;
  3450. case 'png':
  3451. if (!function_exists('imagecreatefrompng')) {
  3452. $this->processed = false;
  3453. $this->error = $this->translate('no_create_support', array('PNG'));
  3454. } else {
  3455. $image_src = imagecreatefrompng($this->file_src_pathname);
  3456. if (!$image_src) {
  3457. $this->processed = false;
  3458. $this->error = $this->translate('create_error', array('PNG'));
  3459. } else {
  3460. $this->log .= '- source image is PNG<br />';
  3461. }
  3462. }
  3463. break;
  3464. case 'gif':
  3465. if (!function_exists('imagecreatefromgif')) {
  3466. $this->processed = false;
  3467. $this->error = $this->translate('no_create_support', array('GIF'));
  3468. } else {
  3469. $image_src = imagecreatefromgif($this->file_src_pathname);
  3470. if (!$image_src) {
  3471. $this->processed = false;
  3472. $this->error = $this->translate('create_error', array('GIF'));
  3473. } else {
  3474. $this->log .= '- source image is GIF<br />';
  3475. }
  3476. }
  3477. break;
  3478. case 'bmp':
  3479. if (!method_exists($this, 'imagecreatefrombmp')) {
  3480. $this->processed = false;
  3481. $this->error = $this->translate('no_create_support', array('BMP'));
  3482. } else {
  3483. $image_src = $this->imagecreatefrombmp($this->file_src_pathname);
  3484. if (!$image_src) {
  3485. $this->processed = false;
  3486. $this->error = $this->translate('create_error', array('BMP'));
  3487. } else {
  3488. $this->log .= '- source image is BMP<br />';
  3489. }
  3490. }
  3491. break;
  3492. default:
  3493. $this->processed = false;
  3494. $this->error = $this->translate('source_invalid');
  3495. }
  3496. } else {
  3497. $this->processed = false;
  3498. $this->error = $this->translate('gd_missing');
  3499. }
  3500. if ($this->processed && $image_src) {
  3501. // we have to set image_convert if it is not already
  3502. if (empty($this->image_convert)) {
  3503. $this->log .= '- setting destination file type to ' . $this->image_src_type . '<br />';
  3504. $this->image_convert = $this->image_src_type;
  3505. }
  3506. if (!in_array($this->image_convert, $this->image_supported)) {
  3507. $this->image_convert = 'jpg';
  3508. }
  3509. // we set the default color to be the background color if we don't output in a transparent format
  3510. if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color) && empty($this->image_background_color)) $this->image_background_color = $this->image_default_color;
  3511. if (!empty($this->image_background_color)) $this->image_default_color = $this->image_background_color;
  3512. if (empty($this->image_default_color)) $this->image_default_color = '#FFFFFF';
  3513. $this->image_src_x = imagesx($image_src);
  3514. $this->image_src_y = imagesy($image_src);
  3515. $gd_version = $this->gdversion();
  3516. $ratio_crop = null;
  3517. if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif'
  3518. $this->log .= '- image is detected as having a palette<br />';
  3519. $this->image_is_palette = true;
  3520. $this->image_transparent_color = imagecolortransparent($image_src);
  3521. if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) {
  3522. $this->image_is_transparent = true;
  3523. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;palette image is detected as transparent<br />';
  3524. }
  3525. // if the image has a palette (GIF), we convert it to true color, preserving transparency
  3526. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;convert palette image to true color<br />';
  3527. $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y);
  3528. imagealphablending($true_color, false);
  3529. imagesavealpha($true_color, true);
  3530. for ($x = 0; $x < $this->image_src_x; $x++) {
  3531. for ($y = 0; $y < $this->image_src_y; $y++) {
  3532. if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) {
  3533. imagesetpixel($true_color, $x, $y, 127 << 24);
  3534. } else {
  3535. $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y));
  3536. imagesetpixel($true_color, $x, $y, ($rgb['alpha'] << 24) | ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue']);
  3537. }
  3538. }
  3539. }
  3540. $image_src = $this->imagetransfer($true_color, $image_src);
  3541. imagealphablending($image_src, false);
  3542. imagesavealpha($image_src, true);
  3543. $this->image_is_palette = false;
  3544. }
  3545. $image_dst = & $image_src;
  3546. // pre-crop image, before resizing
  3547. if ((!empty($this->image_precrop))) {
  3548. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_precrop, $this->image_src_x, $this->image_src_y, true, true);
  3549. $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
  3550. $this->image_src_x = $this->image_src_x - $cl - $cr;
  3551. $this->image_src_y = $this->image_src_y - $ct - $cb;
  3552. if ($this->image_src_x < 1) $this->image_src_x = 1;
  3553. if ($this->image_src_y < 1) $this->image_src_y = 1;
  3554. $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y);
  3555. // we copy the image into the recieving image
  3556. imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y);
  3557. // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  3558. if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0 ) {
  3559. // use the background color if present
  3560. if (!empty($this->image_background_color)) {
  3561. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  3562. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  3563. } else {
  3564. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  3565. }
  3566. // fills eventual negative margins
  3567. if ($ct < 0) imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill);
  3568. if ($cr < 0) imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill);
  3569. if ($cb < 0) imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill);
  3570. if ($cl < 0) imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill);
  3571. }
  3572. // we transfert tmp into image_dst
  3573. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3574. }
  3575. // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y)
  3576. if ($this->image_resize) {
  3577. $this->log .= '- resizing...<br />';
  3578. if ($this->image_ratio_x) {
  3579. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x size<br />';
  3580. $this->image_dst_x = round(($this->image_src_x * $this->image_y) / $this->image_src_y);
  3581. $this->image_dst_y = $this->image_y;
  3582. } else if ($this->image_ratio_y) {
  3583. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate y size<br />';
  3584. $this->image_dst_x = $this->image_x;
  3585. $this->image_dst_y = round(($this->image_src_y * $this->image_x) / $this->image_src_x);
  3586. } else if (is_numeric($this->image_ratio_pixels)) {
  3587. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x/y size to match a number of pixels<br />';
  3588. $pixels = $this->image_src_y * $this->image_src_x;
  3589. $diff = sqrt($this->image_ratio_pixels / $pixels);
  3590. $this->image_dst_x = round($this->image_src_x * $diff);
  3591. $this->image_dst_y = round($this->image_src_y * $diff);
  3592. } else if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) {
  3593. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;check x/y sizes<br />';
  3594. if ((!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out)
  3595. || ($this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y))
  3596. || ($this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y)) {
  3597. $this->image_dst_x = $this->image_x;
  3598. $this->image_dst_y = $this->image_y;
  3599. if ($this->image_ratio_crop) {
  3600. if (!is_string($this->image_ratio_crop)) $this->image_ratio_crop = '';
  3601. $this->image_ratio_crop = strtolower($this->image_ratio_crop);
  3602. if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) {
  3603. $this->image_dst_y = $this->image_y;
  3604. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  3605. $ratio_crop = array();
  3606. $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
  3607. if (strpos($this->image_ratio_crop, 'l') !== false) {
  3608. $ratio_crop['l'] = 0;
  3609. $ratio_crop['r'] = $ratio_crop['x'];
  3610. } else if (strpos($this->image_ratio_crop, 'r') !== false) {
  3611. $ratio_crop['l'] = $ratio_crop['x'];
  3612. $ratio_crop['r'] = 0;
  3613. } else {
  3614. $ratio_crop['l'] = round($ratio_crop['x']/2);
  3615. $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
  3616. }
  3617. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
  3618. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3619. } else {
  3620. $this->image_dst_x = $this->image_x;
  3621. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  3622. $ratio_crop = array();
  3623. $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
  3624. if (strpos($this->image_ratio_crop, 't') !== false) {
  3625. $ratio_crop['t'] = 0;
  3626. $ratio_crop['b'] = $ratio_crop['y'];
  3627. } else if (strpos($this->image_ratio_crop, 'b') !== false) {
  3628. $ratio_crop['t'] = $ratio_crop['y'];
  3629. $ratio_crop['b'] = 0;
  3630. } else {
  3631. $ratio_crop['t'] = round($ratio_crop['y']/2);
  3632. $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
  3633. }
  3634. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
  3635. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3636. }
  3637. } else if ($this->image_ratio_fill) {
  3638. if (!is_string($this->image_ratio_fill)) $this->image_ratio_fill = '';
  3639. $this->image_ratio_fill = strtolower($this->image_ratio_fill);
  3640. if (($this->image_src_x/$this->image_x) < ($this->image_src_y/$this->image_y)) {
  3641. $this->image_dst_y = $this->image_y;
  3642. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  3643. $ratio_crop = array();
  3644. $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
  3645. if (strpos($this->image_ratio_fill, 'l') !== false) {
  3646. $ratio_crop['l'] = 0;
  3647. $ratio_crop['r'] = $ratio_crop['x'];
  3648. } else if (strpos($this->image_ratio_fill, 'r') !== false) {
  3649. $ratio_crop['l'] = $ratio_crop['x'];
  3650. $ratio_crop['r'] = 0;
  3651. } else {
  3652. $ratio_crop['l'] = round($ratio_crop['x']/2);
  3653. $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
  3654. }
  3655. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
  3656. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3657. } else {
  3658. $this->image_dst_x = $this->image_x;
  3659. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  3660. $ratio_crop = array();
  3661. $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
  3662. if (strpos($this->image_ratio_fill, 't') !== false) {
  3663. $ratio_crop['t'] = 0;
  3664. $ratio_crop['b'] = $ratio_crop['y'];
  3665. } else if (strpos($this->image_ratio_fill, 'b') !== false) {
  3666. $ratio_crop['t'] = $ratio_crop['y'];
  3667. $ratio_crop['b'] = 0;
  3668. } else {
  3669. $ratio_crop['t'] = round($ratio_crop['y']/2);
  3670. $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
  3671. }
  3672. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
  3673. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3674. }
  3675. } else {
  3676. if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) {
  3677. $this->image_dst_x = $this->image_x;
  3678. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  3679. } else {
  3680. $this->image_dst_y = $this->image_y;
  3681. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  3682. }
  3683. }
  3684. } else {
  3685. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;doesn\'t calculate x/y sizes<br />';
  3686. $this->image_dst_x = $this->image_src_x;
  3687. $this->image_dst_y = $this->image_src_y;
  3688. }
  3689. } else {
  3690. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;use plain sizes<br />';
  3691. $this->image_dst_x = $this->image_x;
  3692. $this->image_dst_y = $this->image_y;
  3693. }
  3694. if ($this->image_dst_x < 1) $this->image_dst_x = 1;
  3695. if ($this->image_dst_y < 1) $this->image_dst_y = 1;
  3696. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3697. if ($gd_version >= 2) {
  3698. $res = imagecopyresampled($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
  3699. } else {
  3700. $res = imagecopyresized($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
  3701. }
  3702. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;resized image object created<br />';
  3703. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />';
  3704. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />';
  3705. // we transfert tmp into image_dst
  3706. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3707. } else {
  3708. $this->image_dst_x = $this->image_src_x;
  3709. $this->image_dst_y = $this->image_src_y;
  3710. }
  3711. // crop image (and also crops if image_ratio_crop is used)
  3712. if ((!empty($this->image_crop) || !is_null($ratio_crop))) {
  3713. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_crop, $this->image_dst_x, $this->image_dst_y, true, true);
  3714. // we adjust the cropping if we use image_ratio_crop
  3715. if (!is_null($ratio_crop)) {
  3716. if (array_key_exists('t', $ratio_crop)) $ct += $ratio_crop['t'];
  3717. if (array_key_exists('r', $ratio_crop)) $cr += $ratio_crop['r'];
  3718. if (array_key_exists('b', $ratio_crop)) $cb += $ratio_crop['b'];
  3719. if (array_key_exists('l', $ratio_crop)) $cl += $ratio_crop['l'];
  3720. }
  3721. $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
  3722. $this->image_dst_x = $this->image_dst_x - $cl - $cr;
  3723. $this->image_dst_y = $this->image_dst_y - $ct - $cb;
  3724. if ($this->image_dst_x < 1) $this->image_dst_x = 1;
  3725. if ($this->image_dst_y < 1) $this->image_dst_y = 1;
  3726. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3727. // we copy the image into the recieving image
  3728. imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y);
  3729. // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  3730. if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0 ) {
  3731. // use the background color if present
  3732. if (!empty($this->image_background_color)) {
  3733. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  3734. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  3735. } else {
  3736. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  3737. }
  3738. // fills eventual negative margins
  3739. if ($ct < 0) imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct-1, $fill);
  3740. if ($cr < 0) imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill);
  3741. if ($cb < 0) imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill);
  3742. if ($cl < 0) imagefilledrectangle($tmp, 0, 0, -$cl-1, $this->image_dst_y, $fill);
  3743. }
  3744. // we transfert tmp into image_dst
  3745. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3746. }
  3747. // flip image
  3748. if ($gd_version >= 2 && !empty($this->image_flip)) {
  3749. $this->image_flip = strtolower($this->image_flip);
  3750. $this->log .= '- flip image : ' . $this->image_flip . '<br />';
  3751. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3752. for ($x = 0; $x < $this->image_dst_x; $x++) {
  3753. for ($y = 0; $y < $this->image_dst_y; $y++){
  3754. if (strpos($this->image_flip, 'v') !== false) {
  3755. imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1);
  3756. } else {
  3757. imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1);
  3758. }
  3759. }
  3760. }
  3761. // we transfert tmp into image_dst
  3762. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3763. }
  3764. // rotate image
  3765. if ($gd_version >= 2 && is_numeric($this->image_rotate)) {
  3766. if (!in_array($this->image_rotate, array(0, 90, 180, 270))) $this->image_rotate = 0;
  3767. if ($this->image_rotate != 0) {
  3768. if ($this->image_rotate == 90 || $this->image_rotate == 270) {
  3769. $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x);
  3770. } else {
  3771. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3772. }
  3773. $this->log .= '- rotate image : ' . $this->image_rotate . '<br />';
  3774. for ($x = 0; $x < $this->image_dst_x; $x++) {
  3775. for ($y = 0; $y < $this->image_dst_y; $y++){
  3776. if ($this->image_rotate == 90) {
  3777. imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1);
  3778. } else if ($this->image_rotate == 180) {
  3779. imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1);
  3780. } else if ($this->image_rotate == 270) {
  3781. imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1);
  3782. } else {
  3783. imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1);
  3784. }
  3785. }
  3786. }
  3787. if ($this->image_rotate == 90 || $this->image_rotate == 270) {
  3788. $t = $this->image_dst_y;
  3789. $this->image_dst_y = $this->image_dst_x;
  3790. $this->image_dst_x = $t;
  3791. }
  3792. // we transfert tmp into image_dst
  3793. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3794. }
  3795. }
  3796. // unsharp mask
  3797. if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) {
  3798. // Unsharp Mask for PHP - version 2.1.1
  3799. // Unsharp mask algorithm by Torstein Hønsi 2003-07.
  3800. // Used with permission
  3801. // Modified to support alpha transparency
  3802. if ($this->image_unsharp_amount > 500) $this->image_unsharp_amount = 500;
  3803. $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016;
  3804. if ($this->image_unsharp_radius > 50) $this->image_unsharp_radius = 50;
  3805. $this->image_unsharp_radius = $this->image_unsharp_radius * 2;
  3806. if ($this->image_unsharp_threshold > 255) $this->image_unsharp_threshold = 255;
  3807. $this->image_unsharp_radius = abs(round($this->image_unsharp_radius));
  3808. if ($this->image_unsharp_radius != 0) {
  3809. $this->image_dst_x = imagesx($image_dst); $this->image_dst_y = imagesy($image_dst);
  3810. $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
  3811. $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
  3812. if (function_exists('imageconvolution')) { // PHP >= 5.1
  3813. $matrix = array(array( 1, 2, 1 ), array( 2, 4, 2 ), array( 1, 2, 1 ));
  3814. imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  3815. imageconvolution($blur, $matrix, 16, 0);
  3816. } else {
  3817. for ($i = 0; $i < $this->image_unsharp_radius; $i++) {
  3818. imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y); // left
  3819. $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // right
  3820. $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // center
  3821. imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  3822. $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333 ); // up
  3823. $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25); // down
  3824. }
  3825. }
  3826. $p_new = array();
  3827. if($this->image_unsharp_threshold>0) {
  3828. for ($x = 0; $x < $this->image_dst_x-1; $x++) {
  3829. for ($y = 0; $y < $this->image_dst_y; $y++) {
  3830. $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3831. $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
  3832. $p_new['red'] = (abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red'])) + $p_orig['red'])) : $p_orig['red'];
  3833. $p_new['green'] = (abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green'])) + $p_orig['green'])) : $p_orig['green'];
  3834. $p_new['blue'] = (abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue'])) + $p_orig['blue'])) : $p_orig['blue'];
  3835. if (($p_orig['red'] != $p_new['red']) || ($p_orig['green'] != $p_new['green']) || ($p_orig['blue'] != $p_new['blue'])) {
  3836. $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
  3837. imagesetpixel($image_dst, $x, $y, $color);
  3838. }
  3839. }
  3840. }
  3841. } else {
  3842. for ($x = 0; $x < $this->image_dst_x; $x++) {
  3843. for ($y = 0; $y < $this->image_dst_y; $y++) {
  3844. $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3845. $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
  3846. $p_new['red'] = ($this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red'])) + $p_orig['red'];
  3847. if ($p_new['red']>255) { $p_new['red']=255; } elseif ($p_new['red']<0) { $p_new['red']=0; }
  3848. $p_new['green'] = ($this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green'])) + $p_orig['green'];
  3849. if ($p_new['green']>255) { $p_new['green']=255; } elseif ($p_new['green']<0) { $p_new['green']=0; }
  3850. $p_new['blue'] = ($this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue'])) + $p_orig['blue'];
  3851. if ($p_new['blue']>255) { $p_new['blue']=255; } elseif ($p_new['blue']<0) { $p_new['blue']=0; }
  3852. $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
  3853. imagesetpixel($image_dst, $x, $y, $color);
  3854. }
  3855. }
  3856. }
  3857. imagedestroy($canvas);
  3858. imagedestroy($blur);
  3859. }
  3860. }
  3861. // add color overlay
  3862. if ($gd_version >= 2 && (is_numeric($this->image_overlay_opacity) && $this->image_overlay_opacity > 0 && !empty($this->image_overlay_color))) {
  3863. $this->log .= '- apply color overlay<br />';
  3864. list($red, $green, $blue) = $this->getcolors($this->image_overlay_color);
  3865. $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y);
  3866. $color = imagecolorallocate($filter, $red, $green, $blue);
  3867. imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color);
  3868. $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_opacity);
  3869. imagedestroy($filter);
  3870. }
  3871. // add brightness, contrast and tint, turns to greyscale and inverts colors
  3872. 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))) {
  3873. $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />';
  3874. if (!empty($this->image_tint_color)) list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color);
  3875. //imagealphablending($image_dst, true);
  3876. for($y=0; $y < $this->image_dst_y; $y++) {
  3877. for($x=0; $x < $this->image_dst_x; $x++) {
  3878. if ($this->image_greyscale) {
  3879. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3880. $r = $g = $b = round((0.2125 * $pixel['red']) + (0.7154 * $pixel['green']) + (0.0721 * $pixel['blue']));
  3881. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  3882. imagesetpixel($image_dst, $x, $y, $color);
  3883. unset($color); unset($pixel);
  3884. }
  3885. if (is_numeric($this->image_threshold)) {
  3886. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3887. $c = (round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3) - 127;
  3888. $r = $g = $b = ($c > $this->image_threshold ? 255 : 0);
  3889. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  3890. imagesetpixel($image_dst, $x, $y, $color);
  3891. unset($color); unset($pixel);
  3892. }
  3893. if (is_numeric($this->image_brightness)) {
  3894. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3895. $r = max(min(round($pixel['red'] + (($this->image_brightness * 2))), 255), 0);
  3896. $g = max(min(round($pixel['green'] + (($this->image_brightness * 2))), 255), 0);
  3897. $b = max(min(round($pixel['blue'] + (($this->image_brightness * 2))), 255), 0);
  3898. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  3899. imagesetpixel($image_dst, $x, $y, $color);
  3900. unset($color); unset($pixel);
  3901. }
  3902. if (is_numeric($this->image_contrast)) {
  3903. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3904. $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0);
  3905. $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0);
  3906. $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0);
  3907. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  3908. imagesetpixel($image_dst, $x, $y, $color);
  3909. unset($color); unset($pixel);
  3910. }
  3911. if (!empty($this->image_tint_color)) {
  3912. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3913. $r = min(round($tint_red * $pixel['red'] / 169), 255);
  3914. $g = min(round($tint_green * $pixel['green'] / 169), 255);
  3915. $b = min(round($tint_blue * $pixel['blue'] / 169), 255);
  3916. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  3917. imagesetpixel($image_dst, $x, $y, $color);
  3918. unset($color); unset($pixel);
  3919. }
  3920. if (!empty($this->image_negative)) {
  3921. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3922. $r = round(255 - $pixel['red']);
  3923. $g = round(255 - $pixel['green']);
  3924. $b = round(255 - $pixel['blue']);
  3925. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  3926. imagesetpixel($image_dst, $x, $y, $color);
  3927. unset($color); unset($pixel);
  3928. }
  3929. }
  3930. }
  3931. }
  3932. // adds a border
  3933. if ($gd_version >= 2 && !empty($this->image_border)) {
  3934. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_border, $this->image_dst_x, $this->image_dst_y, true, false);
  3935. $this->log .= '- add border : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . '<br />';
  3936. $this->image_dst_x = $this->image_dst_x + $cl + $cr;
  3937. $this->image_dst_y = $this->image_dst_y + $ct + $cb;
  3938. if (!empty($this->image_border_color)) list($red, $green, $blue) = $this->getcolors($this->image_border_color);
  3939. $opacity = (is_numeric($this->image_border_opacity) ? (int) (127 - $this->image_border_opacity / 100 * 127): 0);
  3940. // we now create an image, that we fill with the border color
  3941. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3942. $background = imagecolorallocatealpha($tmp, $red, $green, $blue, $opacity);
  3943. imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background);
  3944. // we then copy the source image into the new image, without merging so that only the border is actually kept
  3945. imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct);
  3946. // we transfert tmp into image_dst
  3947. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3948. }
  3949. // adds a fading-to-transparent border
  3950. if ($gd_version >= 2 && !empty($this->image_border_transparent)) {
  3951. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_border_transparent, $this->image_dst_x, $this->image_dst_y, true, false);
  3952. $this->log .= '- add transparent border : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . '<br />';
  3953. // we now create an image, that we fill with the border color
  3954. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3955. // we then copy the source image into the new image, without the borders
  3956. imagecopy($tmp, $image_dst, $cl, $ct, $cl, $ct, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct);
  3957. // we now add the top border
  3958. $opacity = 100;
  3959. for ($y = $ct - 1; $y >= 0; $y--) {
  3960. $il = (int) ($ct > 0 ? ($cl * ($y / $ct)) : 0);
  3961. $ir = (int) ($ct > 0 ? ($cr * ($y / $ct)) : 0);
  3962. for ($x = $il; $x < $this->image_dst_x - $ir; $x++) {
  3963. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3964. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  3965. if ($alpha > 0) {
  3966. if ($alpha > 1) $alpha = 1;
  3967. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  3968. imagesetpixel($tmp, $x, $y, $color);
  3969. }
  3970. }
  3971. if ($opacity > 0) $opacity = $opacity - (100 / $ct);
  3972. }
  3973. // we now add the right border
  3974. $opacity = 100;
  3975. for ($x = $this->image_dst_x - $cr; $x < $this->image_dst_x; $x++) {
  3976. $it = (int) ($cr > 0 ? ($ct * (($this->image_dst_x - $x - 1) / $cr)) : 0);
  3977. $ib = (int) ($cr > 0 ? ($cb * (($this->image_dst_x - $x - 1) / $cr)) : 0);
  3978. for ($y = $it; $y < $this->image_dst_y - $ib; $y++) {
  3979. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3980. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  3981. if ($alpha > 0) {
  3982. if ($alpha > 1) $alpha = 1;
  3983. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  3984. imagesetpixel($tmp, $x, $y, $color);
  3985. }
  3986. }
  3987. if ($opacity > 0) $opacity = $opacity - (100 / $cr);
  3988. }
  3989. // we now add the bottom border
  3990. $opacity = 100;
  3991. for ($y = $this->image_dst_y - $cb; $y < $this->image_dst_y; $y++) {
  3992. $il = (int) ($cb > 0 ? ($cl * (($this->image_dst_y - $y - 1) / $cb)) : 0);
  3993. $ir = (int) ($cb > 0 ? ($cr * (($this->image_dst_y - $y - 1) / $cb)) : 0);
  3994. for ($x = $il; $x < $this->image_dst_x - $ir; $x++) {
  3995. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3996. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  3997. if ($alpha > 0) {
  3998. if ($alpha > 1) $alpha = 1;
  3999. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  4000. imagesetpixel($tmp, $x, $y, $color);
  4001. }
  4002. }
  4003. if ($opacity > 0) $opacity = $opacity - (100 / $cb);
  4004. }
  4005. // we now add the left border
  4006. $opacity = 100;
  4007. for ($x = $cl - 1; $x >= 0; $x--) {
  4008. $it = (int) ($cl > 0 ? ($ct * ($x / $cl)) : 0);
  4009. $ib = (int) ($cl > 0 ? ($cb * ($x / $cl)) : 0);
  4010. for ($y = $it; $y < $this->image_dst_y - $ib; $y++) {
  4011. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4012. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  4013. if ($alpha > 0) {
  4014. if ($alpha > 1) $alpha = 1;
  4015. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  4016. imagesetpixel($tmp, $x, $y, $color);
  4017. }
  4018. }
  4019. if ($opacity > 0) $opacity = $opacity - (100 / $cl);
  4020. }
  4021. // we transfert tmp into image_dst
  4022. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4023. }
  4024. // add frame border
  4025. if ($gd_version >= 2 && is_numeric($this->image_frame)) {
  4026. if (is_array($this->image_frame_colors)) {
  4027. $vars = $this->image_frame_colors;
  4028. $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />';
  4029. } else {
  4030. $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />';
  4031. $vars = explode(' ', $this->image_frame_colors);
  4032. }
  4033. $nb = sizeof($vars);
  4034. $this->image_dst_x = $this->image_dst_x + ($nb * 2);
  4035. $this->image_dst_y = $this->image_dst_y + ($nb * 2);
  4036. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  4037. imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - ($nb * 2), $this->image_dst_y - ($nb * 2));
  4038. $opacity = (is_numeric($this->image_frame_opacity) ? (int) (127 - $this->image_frame_opacity / 100 * 127): 0);
  4039. for ($i=0; $i<$nb; $i++) {
  4040. list($red, $green, $blue) = $this->getcolors($vars[$i]);
  4041. $c = imagecolorallocatealpha($tmp, $red, $green, $blue, $opacity);
  4042. if ($this->image_frame == 1) {
  4043. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c);
  4044. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $this->image_dst_x - $i -1, $i, $c);
  4045. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c);
  4046. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c);
  4047. } else {
  4048. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c);
  4049. imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c);
  4050. imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c);
  4051. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c);
  4052. }
  4053. }
  4054. // we transfert tmp into image_dst
  4055. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4056. }
  4057. // add bevel border
  4058. if ($gd_version >= 2 && $this->image_bevel > 0) {
  4059. if (empty($this->image_bevel_color1)) $this->image_bevel_color1 = '#FFFFFF';
  4060. if (empty($this->image_bevel_color2)) $this->image_bevel_color2 = '#000000';
  4061. list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1);
  4062. list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2);
  4063. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  4064. imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  4065. imagealphablending($tmp, true);
  4066. for ($i=0; $i<$this->image_bevel; $i++) {
  4067. $alpha = round(($i / $this->image_bevel) * 127);
  4068. $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha);
  4069. $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha);
  4070. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c1);
  4071. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i, $this->image_dst_x - $i -1, $i, $c2);
  4072. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c2);
  4073. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c1);
  4074. }
  4075. // we transfert tmp into image_dst
  4076. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4077. }
  4078. // add watermark image
  4079. if ($this->image_watermark!='' && file_exists($this->image_watermark)) {
  4080. $this->log .= '- add watermark<br />';
  4081. $this->image_watermark_position = strtolower($this->image_watermark_position);
  4082. $watermark_info = getimagesize($this->image_watermark);
  4083. $watermark_type = (array_key_exists(2, $watermark_info) ? $watermark_info[2] : null); // 1 = GIF, 2 = JPG, 3 = PNG
  4084. $watermark_checked = false;
  4085. if ($watermark_type == IMAGETYPE_GIF) {
  4086. if (!function_exists('imagecreatefromgif')) {
  4087. $this->error = $this->translate('watermark_no_create_support', array('GIF'));
  4088. } else {
  4089. $filter = imagecreatefromgif($this->image_watermark);
  4090. if (!$filter) {
  4091. $this->error = $this->translate('watermark_create_error', array('GIF'));
  4092. } else {
  4093. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is GIF<br />';
  4094. $watermark_checked = true;
  4095. }
  4096. }
  4097. } else if ($watermark_type == IMAGETYPE_JPEG) {
  4098. if (!function_exists('imagecreatefromjpeg')) {
  4099. $this->error = $this->translate('watermark_no_create_support', array('JPEG'));
  4100. } else {
  4101. $filter = imagecreatefromjpeg($this->image_watermark);
  4102. if (!$filter) {
  4103. $this->error = $this->translate('watermark_create_error', array('JPEG'));
  4104. } else {
  4105. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is JPEG<br />';
  4106. $watermark_checked = true;
  4107. }
  4108. }
  4109. } else if ($watermark_type == IMAGETYPE_PNG) {
  4110. if (!function_exists('imagecreatefrompng')) {
  4111. $this->error = $this->translate('watermark_no_create_support', array('PNG'));
  4112. } else {
  4113. $filter = imagecreatefrompng($this->image_watermark);
  4114. if (!$filter) {
  4115. $this->error = $this->translate('watermark_create_error', array('PNG'));
  4116. } else {
  4117. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is PNG<br />';
  4118. $watermark_checked = true;
  4119. }
  4120. }
  4121. } else if ($watermark_type == IMAGETYPE_BMP) {
  4122. if (!method_exists($this, 'imagecreatefrombmp')) {
  4123. $this->error = $this->translate('watermark_no_create_support', array('BMP'));
  4124. } else {
  4125. $filter = $this->imagecreatefrombmp($this->image_watermark);
  4126. if (!$filter) {
  4127. $this->error = $this->translate('watermark_create_error', array('BMP'));
  4128. } else {
  4129. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is BMP<br />';
  4130. $watermark_checked = true;
  4131. }
  4132. }
  4133. } else {
  4134. $this->error = $this->translate('watermark_invalid');
  4135. }
  4136. if ($watermark_checked) {
  4137. $watermark_dst_width = $watermark_src_width = imagesx($filter);
  4138. $watermark_dst_height = $watermark_src_height = imagesy($filter);
  4139. // if watermark is too large/tall, resize it first
  4140. if ((!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y))
  4141. || (!$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y)) {
  4142. $canvas_width = $this->image_dst_x - abs($this->image_watermark_x);
  4143. $canvas_height = $this->image_dst_y - abs($this->image_watermark_y);
  4144. if (($watermark_src_width/$canvas_width) > ($watermark_src_height/$canvas_height)) {
  4145. $watermark_dst_width = $canvas_width;
  4146. $watermark_dst_height = intval($watermark_src_height*($canvas_width / $watermark_src_width));
  4147. } else {
  4148. $watermark_dst_height = $canvas_height;
  4149. $watermark_dst_width = intval($watermark_src_width*($canvas_height / $watermark_src_height));
  4150. }
  4151. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark resized from '.$watermark_src_width.'x'.$watermark_src_height.' to '.$watermark_dst_width.'x'.$watermark_dst_height.'<br />';
  4152. }
  4153. // determine watermark position
  4154. $watermark_x = 0;
  4155. $watermark_y = 0;
  4156. if (is_numeric($this->image_watermark_x)) {
  4157. if ($this->image_watermark_x < 0) {
  4158. $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x;
  4159. } else {
  4160. $watermark_x = $this->image_watermark_x;
  4161. }
  4162. } else {
  4163. if (strpos($this->image_watermark_position, 'r') !== false) {
  4164. $watermark_x = $this->image_dst_x - $watermark_dst_width;
  4165. } else if (strpos($this->image_watermark_position, 'l') !== false) {
  4166. $watermark_x = 0;
  4167. } else {
  4168. $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2;
  4169. }
  4170. }
  4171. if (is_numeric($this->image_watermark_y)) {
  4172. if ($this->image_watermark_y < 0) {
  4173. $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y;
  4174. } else {
  4175. $watermark_y = $this->image_watermark_y;
  4176. }
  4177. } else {
  4178. if (strpos($this->image_watermark_position, 'b') !== false) {
  4179. $watermark_y = $this->image_dst_y - $watermark_dst_height;
  4180. } else if (strpos($this->image_watermark_position, 't') !== false) {
  4181. $watermark_y = 0;
  4182. } else {
  4183. $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2;
  4184. }
  4185. }
  4186. imagealphablending($image_dst, true);
  4187. imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height);
  4188. } else {
  4189. $this->error = $this->translate('watermark_invalid');
  4190. }
  4191. }
  4192. // add text
  4193. if (!empty($this->image_text)) {
  4194. $this->log .= '- add text<br />';
  4195. // calculate sizes in human readable format
  4196. $src_size = $this->file_src_size / 1024;
  4197. $src_size_mb = number_format($src_size / 1024, 1, ".", " ");
  4198. $src_size_kb = number_format($src_size, 1, ".", " ");
  4199. $src_size_human = ($src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb");
  4200. $this->image_text = str_replace(
  4201. array('[src_name]',
  4202. '[src_name_body]',
  4203. '[src_name_ext]',
  4204. '[src_pathname]',
  4205. '[src_mime]',
  4206. '[src_size]',
  4207. '[src_size_kb]',
  4208. '[src_size_mb]',
  4209. '[src_size_human]',
  4210. '[src_x]',
  4211. '[src_y]',
  4212. '[src_pixels]',
  4213. '[src_type]',
  4214. '[src_bits]',
  4215. '[dst_path]',
  4216. '[dst_name_body]',
  4217. '[dst_name_ext]',
  4218. '[dst_name]',
  4219. '[dst_pathname]',
  4220. '[dst_x]',
  4221. '[dst_y]',
  4222. '[date]',
  4223. '[time]',
  4224. '[host]',
  4225. '[server]',
  4226. '[ip]',
  4227. '[gd_version]'),
  4228. array($this->file_src_name,
  4229. $this->file_src_name_body,
  4230. $this->file_src_name_ext,
  4231. $this->file_src_pathname,
  4232. $this->file_src_mime,
  4233. $this->file_src_size,
  4234. $src_size_kb,
  4235. $src_size_mb,
  4236. $src_size_human,
  4237. $this->image_src_x,
  4238. $this->image_src_y,
  4239. $this->image_src_pixels,
  4240. $this->image_src_type,
  4241. $this->image_src_bits,
  4242. $this->file_dst_path,
  4243. $this->file_dst_name_body,
  4244. $this->file_dst_name_ext,
  4245. $this->file_dst_name,
  4246. $this->file_dst_pathname,
  4247. $this->image_dst_x,
  4248. $this->image_dst_y,
  4249. date('Y-m-d'),
  4250. date('H:i:s'),
  4251. (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a'),
  4252. (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a'),
  4253. (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a'),
  4254. $this->gdversion(true)),
  4255. $this->image_text);
  4256. if (!is_numeric($this->image_text_padding)) $this->image_text_padding = 0;
  4257. if (!is_numeric($this->image_text_line_spacing)) $this->image_text_line_spacing = 0;
  4258. if (!is_numeric($this->image_text_padding_x)) $this->image_text_padding_x = $this->image_text_padding;
  4259. if (!is_numeric($this->image_text_padding_y)) $this->image_text_padding_y = $this->image_text_padding;
  4260. $this->image_text_position = strtolower($this->image_text_position);
  4261. $this->image_text_direction = strtolower($this->image_text_direction);
  4262. $this->image_text_alignment = strtolower($this->image_text_alignment);
  4263. // if the font is a string, we assume that we might want to load a font
  4264. if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') {
  4265. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;try to load font ' . $this->image_text_font . '... ';
  4266. if ($this->image_text_font = imageloadfont($this->image_text_font)) {
  4267. $this->log .= 'success<br />';
  4268. } else {
  4269. $this->log .= 'error<br />';
  4270. $this->image_text_font = 5;
  4271. }
  4272. }
  4273. $text = explode("\n", $this->image_text);
  4274. $char_width = imagefontwidth($this->image_text_font);
  4275. $char_height = imagefontheight($this->image_text_font);
  4276. $text_height = 0;
  4277. $text_width = 0;
  4278. $line_height = 0;
  4279. $line_width = 0;
  4280. foreach ($text as $k => $v) {
  4281. if ($this->image_text_direction == 'v') {
  4282. $h = ($char_width * strlen($v));
  4283. if ($h > $text_height) $text_height = $h;
  4284. $line_width = $char_height;
  4285. $text_width += $line_width + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0);
  4286. } else {
  4287. $w = ($char_width * strlen($v));
  4288. if ($w > $text_width) $text_width = $w;
  4289. $line_height = $char_height;
  4290. $text_height += $line_height + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0);
  4291. }
  4292. }
  4293. $text_width += (2 * $this->image_text_padding_x);
  4294. $text_height += (2 * $this->image_text_padding_y);
  4295. $text_x = 0;
  4296. $text_y = 0;
  4297. if (is_numeric($this->image_text_x)) {
  4298. if ($this->image_text_x < 0) {
  4299. $text_x = $this->image_dst_x - $text_width + $this->image_text_x;
  4300. } else {
  4301. $text_x = $this->image_text_x;
  4302. }
  4303. } else {
  4304. if (strpos($this->image_text_position, 'r') !== false) {
  4305. $text_x = $this->image_dst_x - $text_width;
  4306. } else if (strpos($this->image_text_position, 'l') !== false) {
  4307. $text_x = 0;
  4308. } else {
  4309. $text_x = ($this->image_dst_x - $text_width) / 2;
  4310. }
  4311. }
  4312. if (is_numeric($this->image_text_y)) {
  4313. if ($this->image_text_y < 0) {
  4314. $text_y = $this->image_dst_y - $text_height + $this->image_text_y;
  4315. } else {
  4316. $text_y = $this->image_text_y;
  4317. }
  4318. } else {
  4319. if (strpos($this->image_text_position, 'b') !== false) {
  4320. $text_y = $this->image_dst_y - $text_height;
  4321. } else if (strpos($this->image_text_position, 't') !== false) {
  4322. $text_y = 0;
  4323. } else {
  4324. $text_y = ($this->image_dst_y - $text_height) / 2;
  4325. }
  4326. }
  4327. // add a background, maybe transparent
  4328. if (!empty($this->image_text_background)) {
  4329. list($red, $green, $blue) = $this->getcolors($this->image_text_background);
  4330. if ($gd_version >= 2 && (is_numeric($this->image_text_background_opacity)) && $this->image_text_background_opacity >= 0 && $this->image_text_background_opacity <= 100) {
  4331. $filter = imagecreatetruecolor($text_width, $text_height);
  4332. $background_color = imagecolorallocate($filter, $red, $green, $blue);
  4333. imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color);
  4334. $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_opacity);
  4335. imagedestroy($filter);
  4336. } else {
  4337. $background_color = imagecolorallocate($image_dst ,$red, $green, $blue);
  4338. imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color);
  4339. }
  4340. }
  4341. $text_x += $this->image_text_padding_x;
  4342. $text_y += $this->image_text_padding_y;
  4343. $t_width = $text_width - (2 * $this->image_text_padding_x);
  4344. $t_height = $text_height - (2 * $this->image_text_padding_y);
  4345. list($red, $green, $blue) = $this->getcolors($this->image_text_color);
  4346. // add the text, maybe transparent
  4347. if ($gd_version >= 2 && (is_numeric($this->image_text_opacity)) && $this->image_text_opacity >= 0 && $this->image_text_opacity <= 100) {
  4348. if ($t_width < 0) $t_width = 0;
  4349. if ($t_height < 0) $t_height = 0;
  4350. $filter = $this->imagecreatenew($t_width, $t_height, false, true);
  4351. $text_color = imagecolorallocate($filter ,$red, $green, $blue);
  4352. foreach ($text as $k => $v) {
  4353. if ($this->image_text_direction == 'v') {
  4354. imagestringup($filter,
  4355. $this->image_text_font,
  4356. $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4357. $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))) ,
  4358. $v,
  4359. $text_color);
  4360. } else {
  4361. imagestring($filter,
  4362. $this->image_text_font,
  4363. ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))),
  4364. $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4365. $v,
  4366. $text_color);
  4367. }
  4368. }
  4369. $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_opacity);
  4370. imagedestroy($filter);
  4371. } else {
  4372. $text_color = imageColorAllocate($image_dst ,$red, $green, $blue);
  4373. foreach ($text as $k => $v) {
  4374. if ($this->image_text_direction == 'v') {
  4375. imagestringup($image_dst,
  4376. $this->image_text_font,
  4377. $text_x + $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4378. $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))),
  4379. $v,
  4380. $text_color);
  4381. } else {
  4382. imagestring($image_dst,
  4383. $this->image_text_font,
  4384. $text_x + ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))),
  4385. $text_y + $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4386. $v,
  4387. $text_color);
  4388. }
  4389. }
  4390. }
  4391. }
  4392. // add a reflection
  4393. if ($this->image_reflection_height) {
  4394. $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />';
  4395. // we decode image_reflection_height, which can be a integer, a string in pixels or percentage
  4396. $image_reflection_height = $this->image_reflection_height;
  4397. if (strpos($image_reflection_height, '%')>0) $image_reflection_height = $this->image_dst_y * (str_replace('%','',$image_reflection_height / 100));
  4398. if (strpos($image_reflection_height, 'px')>0) $image_reflection_height = str_replace('px','',$image_reflection_height);
  4399. $image_reflection_height = (int) $image_reflection_height;
  4400. if ($image_reflection_height > $this->image_dst_y) $image_reflection_height = $this->image_dst_y;
  4401. if (empty($this->image_reflection_opacity)) $this->image_reflection_opacity = 60;
  4402. // create the new destination image
  4403. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true);
  4404. $transparency = $this->image_reflection_opacity;
  4405. // copy the original image
  4406. 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));
  4407. // we have to make sure the extra bit is the right color, or transparent
  4408. if ($image_reflection_height + $this->image_reflection_space > 0) {
  4409. // use the background color if present
  4410. if (!empty($this->image_background_color)) {
  4411. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  4412. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  4413. } else {
  4414. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  4415. }
  4416. // fill in from the edge of the extra bit
  4417. imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill);
  4418. }
  4419. // copy the reflection
  4420. for ($y = 0; $y < $image_reflection_height; $y++) {
  4421. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4422. $pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space));
  4423. $pixel_o = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)));
  4424. $alpha_o = 1 - ($pixel_o['alpha'] / 127);
  4425. $alpha_b = 1 - ($pixel_b['alpha'] / 127);
  4426. $opacity = $alpha_o * $transparency / 100;
  4427. if ($opacity > 0) {
  4428. $red = round((($pixel_o['red'] * $opacity) + ($pixel_b['red'] ) * $alpha_b) / ($alpha_b + $opacity));
  4429. $green = round((($pixel_o['green'] * $opacity) + ($pixel_b['green']) * $alpha_b) / ($alpha_b + $opacity));
  4430. $blue = round((($pixel_o['blue'] * $opacity) + ($pixel_b['blue'] ) * $alpha_b) / ($alpha_b + $opacity));
  4431. $alpha = ($opacity + $alpha_b);
  4432. if ($alpha > 1) $alpha = 1;
  4433. $alpha = round((1 - $alpha) * 127);
  4434. $color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha);
  4435. imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color);
  4436. }
  4437. }
  4438. if ($transparency > 0) $transparency = $transparency - ($this->image_reflection_opacity / $image_reflection_height);
  4439. }
  4440. // copy the resulting image into the destination image
  4441. $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space;
  4442. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4443. }
  4444. // change opacity
  4445. if ($gd_version >= 2 && is_numeric($this->image_opacity) && $this->image_opacity < 100) {
  4446. $this->log .= '- change opacity<br />';
  4447. // create the new destination image
  4448. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, true);
  4449. for($y=0; $y < $this->image_dst_y; $y++) {
  4450. for($x=0; $x < $this->image_dst_x; $x++) {
  4451. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4452. $alpha = $pixel['alpha'] + round((127 - $pixel['alpha']) * (100 - $this->image_opacity) / 100);
  4453. if ($alpha > 127) $alpha = 127;
  4454. if ($alpha > 0) {
  4455. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], $alpha);
  4456. imagesetpixel($tmp, $x, $y, $color);
  4457. }
  4458. }
  4459. }
  4460. // copy the resulting image into the destination image
  4461. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4462. }
  4463. // reduce the JPEG image to a set desired size
  4464. if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) {
  4465. // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net
  4466. $this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />';
  4467. // calculate size of each image. 75%, 50%, and 25% quality
  4468. ob_start(); imagejpeg($image_dst,'',75); $buffer = ob_get_contents(); ob_end_clean();
  4469. $size75 = strlen($buffer);
  4470. ob_start(); imagejpeg($image_dst,'',50); $buffer = ob_get_contents(); ob_end_clean();
  4471. $size50 = strlen($buffer);
  4472. ob_start(); imagejpeg($image_dst,'',25); $buffer = ob_get_contents(); ob_end_clean();
  4473. $size25 = strlen($buffer);
  4474. // make sure we won't divide by 0
  4475. if ($size50 == $size25) $size50++;
  4476. if ($size75 == $size50 || $size75 == $size25) $size75++;
  4477. // calculate gradient of size reduction by quality
  4478. $mgrad1 = 25 / ($size50-$size25);
  4479. $mgrad2 = 25 / ($size75-$size50);
  4480. $mgrad3 = 50 / ($size75-$size25);
  4481. $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3;
  4482. // result of approx. quality factor for expected size
  4483. $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50);
  4484. if ($q_factor<1) {
  4485. $this->jpeg_quality=1;
  4486. } elseif ($q_factor>100) {
  4487. $this->jpeg_quality=100;
  4488. } else {
  4489. $this->jpeg_quality=$q_factor;
  4490. }
  4491. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG quality factor set to ' . $this->jpeg_quality . '<br />';
  4492. }
  4493. // converts image from true color, and fix transparency if needed
  4494. $this->log .= '- converting...<br />';
  4495. switch($this->image_convert) {
  4496. case 'gif':
  4497. // if the image is true color, we convert it to a palette
  4498. if (imageistruecolor($image_dst)) {
  4499. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;true color to palette<br />';
  4500. // creates a black and white mask
  4501. $mask = array(array());
  4502. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4503. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4504. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4505. $mask[$x][$y] = $pixel['alpha'];
  4506. }
  4507. }
  4508. list($red, $green, $blue) = $this->getcolors($this->image_default_color);
  4509. // first, we merge the image with the background color, so we know which colors we will have
  4510. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4511. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4512. if ($mask[$x][$y] > 0){
  4513. // we have some transparency. we combine the color with the default color
  4514. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4515. $alpha = ($mask[$x][$y] / 127);
  4516. $pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
  4517. $pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
  4518. $pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
  4519. $color = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
  4520. imagesetpixel($image_dst, $x, $y, $color);
  4521. }
  4522. }
  4523. }
  4524. // transforms the true color image into palette, with its merged default color
  4525. if (empty($this->image_background_color)) {
  4526. imagetruecolortopalette($image_dst, true, 255);
  4527. $transparency = imagecolorallocate($image_dst, 254, 1, 253);
  4528. imagecolortransparent($image_dst, $transparency);
  4529. // make the transparent areas transparent
  4530. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4531. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4532. // we test wether we have enough opacity to justify keeping the color
  4533. if ($mask[$x][$y] > 120) imagesetpixel($image_dst, $x, $y, $transparency);
  4534. }
  4535. }
  4536. }
  4537. unset($mask);
  4538. }
  4539. break;
  4540. case 'jpg':
  4541. case 'bmp':
  4542. // if the image doesn't support any transparency, then we merge it with the default color
  4543. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;fills in transparency with default color<br />';
  4544. list($red, $green, $blue) = $this->getcolors($this->image_default_color);
  4545. $transparency = imagecolorallocate($image_dst, $red, $green, $blue);
  4546. // make the transaparent areas transparent
  4547. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4548. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4549. // we test wether we have some transparency, in which case we will merge the colors
  4550. if (imageistruecolor($image_dst)) {
  4551. $rgba = imagecolorat($image_dst, $x, $y);
  4552. $pixel = array('red' => ($rgba >> 16) & 0xFF,
  4553. 'green' => ($rgba >> 8) & 0xFF,
  4554. 'blue' => $rgba & 0xFF,
  4555. 'alpha' => ($rgba & 0x7F000000) >> 24);
  4556. } else {
  4557. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4558. }
  4559. if ($pixel['alpha'] == 127) {
  4560. // we have full transparency. we make the pixel transparent
  4561. imagesetpixel($image_dst, $x, $y, $transparency);
  4562. } else if ($pixel['alpha'] > 0) {
  4563. // we have some transparency. we combine the color with the default color
  4564. $alpha = ($pixel['alpha'] / 127);
  4565. $pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
  4566. $pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
  4567. $pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
  4568. $color = imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
  4569. imagesetpixel($image_dst, $x, $y, $color);
  4570. }
  4571. }
  4572. }
  4573. break;
  4574. default:
  4575. break;
  4576. }
  4577. // outputs image
  4578. $this->log .= '- saving image...<br />';
  4579. switch($this->image_convert) {
  4580. case 'jpeg':
  4581. case 'jpg':
  4582. if (!$return_mode) {
  4583. $result = imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality);
  4584. } else {
  4585. ob_start();
  4586. $result = imagejpeg($image_dst, '', $this->jpeg_quality);
  4587. $return_content = ob_get_contents();
  4588. ob_end_clean();
  4589. }
  4590. if (!$result) {
  4591. $this->processed = false;
  4592. $this->error = $this->translate('file_create', array('JPEG'));
  4593. } else {
  4594. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG image created<br />';
  4595. }
  4596. break;
  4597. case 'png':
  4598. imagealphablending( $image_dst, false );
  4599. imagesavealpha( $image_dst, true );
  4600. if (!$return_mode) {
  4601. $result = imagepng($image_dst, $this->file_dst_pathname);
  4602. } else {
  4603. ob_start();
  4604. $result = imagepng($image_dst);
  4605. $return_content = ob_get_contents();
  4606. ob_end_clean();
  4607. }
  4608. if (!$result) {
  4609. $this->processed = false;
  4610. $this->error = $this->translate('file_create', array('PNG'));
  4611. } else {
  4612. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PNG image created<br />';
  4613. }
  4614. break;
  4615. case 'gif':
  4616. if (!$return_mode) {
  4617. $result = imagegif($image_dst, $this->file_dst_pathname);
  4618. } else {
  4619. ob_start();
  4620. $result = imagegif($image_dst);
  4621. $return_content = ob_get_contents();
  4622. ob_end_clean();
  4623. }
  4624. if (!$result) {
  4625. $this->processed = false;
  4626. $this->error = $this->translate('file_create', array('GIF'));
  4627. } else {
  4628. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;GIF image created<br />';
  4629. }
  4630. break;
  4631. case 'bmp':
  4632. if (!$return_mode) {
  4633. $result = $this->imagebmp($image_dst, $this->file_dst_pathname);
  4634. } else {
  4635. ob_start();
  4636. $result = $this->imagebmp($image_dst);
  4637. $return_content = ob_get_contents();
  4638. ob_end_clean();
  4639. }
  4640. if (!$result) {
  4641. $this->processed = false;
  4642. $this->error = $this->translate('file_create', array('BMP'));
  4643. } else {
  4644. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;BMP image created<br />';
  4645. }
  4646. break;
  4647. default:
  4648. $this->processed = false;
  4649. $this->error = $this->translate('no_conversion_type');
  4650. }
  4651. if ($this->processed) {
  4652. if (is_resource($image_src)) imagedestroy($image_src);
  4653. if (is_resource($image_dst)) imagedestroy($image_dst);
  4654. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image objects destroyed<br />';
  4655. }
  4656. }
  4657. } else {
  4658. $this->log .= '- no image processing wanted<br />';
  4659. if (!$return_mode) {
  4660. // copy the file to its final destination. we don't use move_uploaded_file here
  4661. // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file
  4662. if (!copy($this->file_src_pathname, $this->file_dst_pathname)) {
  4663. $this->processed = false;
  4664. $this->error = $this->translate('copy_failed');
  4665. }
  4666. } else {
  4667. // returns the file, so that its content can be received by the caller
  4668. $return_content = file_get_contents($this->file_src_pathname);
  4669. if ($return_content === FALSE) {
  4670. $this->processed = false;
  4671. $this->error = $this->translate('reading_failed');
  4672. }
  4673. }
  4674. }
  4675. }
  4676. if ($this->processed) {
  4677. $this->log .= '- <b>process OK</b><br />';
  4678. } else {
  4679. $this->log .= '- <b>error</b>: ' . $this->error . '<br />';
  4680. }
  4681. // we reinit all the vars
  4682. $this->init();
  4683. // we may return the image content
  4684. if ($return_mode) return $return_content;
  4685. }
  4686. /**
  4687. * Deletes the uploaded file from its temporary location
  4688. *
  4689. * When PHP uploads a file, it stores it in a temporary location.
  4690. * When you {@link process} the file, you actually copy the resulting file to the given location, it doesn't alter the original file.
  4691. * Once you have processed the file as many times as you wanted, you can delete the uploaded file.
  4692. * If there is open_basedir restrictions, the uploaded file is in fact a temporary file
  4693. *
  4694. * You might want not to use this function if you work on local files, as it will delete the source file
  4695. *
  4696. * @access public
  4697. */
  4698. function clean() {
  4699. $this->log .= '<b>cleanup</b><br />';
  4700. $this->log .= '- delete temp file ' . $this->file_src_pathname . '<br />';
  4701. @unlink($this->file_src_pathname);
  4702. }
  4703. /**
  4704. * Opens a BMP image
  4705. *
  4706. * This function has been written by DHKold, and is used with permission of the author
  4707. *
  4708. * @access public
  4709. */
  4710. function imagecreatefrombmp($filename) {
  4711. if (! $f1 = fopen($filename,"rb")) return false;
  4712. $file = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
  4713. if ($file['file_type'] != 19778) return false;
  4714. $bmp = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  4715. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  4716. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
  4717. $bmp['colors'] = pow(2,$bmp['bits_per_pixel']);
  4718. if ($bmp['size_bitmap'] == 0) $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
  4719. $bmp['bytes_per_pixel'] = $bmp['bits_per_pixel']/8;
  4720. $bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
  4721. $bmp['decal'] = ($bmp['width']*$bmp['bytes_per_pixel']/4);
  4722. $bmp['decal'] -= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
  4723. $bmp['decal'] = 4-(4*$bmp['decal']);
  4724. if ($bmp['decal'] == 4) $bmp['decal'] = 0;
  4725. $palette = array();
  4726. if ($bmp['colors'] < 16777216) {
  4727. $palette = unpack('V'.$bmp['colors'], fread($f1,$bmp['colors']*4));
  4728. }
  4729. $im = fread($f1,$bmp['size_bitmap']);
  4730. $vide = chr(0);
  4731. $res = imagecreatetruecolor($bmp['width'],$bmp['height']);
  4732. $P = 0;
  4733. $Y = $bmp['height']-1;
  4734. while ($Y >= 0) {
  4735. $X=0;
  4736. while ($X < $bmp['width']) {
  4737. if ($bmp['bits_per_pixel'] == 24)
  4738. $color = unpack("V",substr($im,$P,3).$vide);
  4739. elseif ($bmp['bits_per_pixel'] == 16) {
  4740. $color = unpack("n",substr($im,$P,2));
  4741. $color[1] = $palette[$color[1]+1];
  4742. } elseif ($bmp['bits_per_pixel'] == 8) {
  4743. $color = unpack("n",$vide.substr($im,$P,1));
  4744. $color[1] = $palette[$color[1]+1];
  4745. } elseif ($bmp['bits_per_pixel'] == 4) {
  4746. $color = unpack("n",$vide.substr($im,floor($P),1));
  4747. if (($P*2)%2 == 0) $color[1] = ($color[1] >> 4) ; else $color[1] = ($color[1] & 0x0F);
  4748. $color[1] = $palette[$color[1]+1];
  4749. } elseif ($bmp['bits_per_pixel'] == 1) {
  4750. $color = unpack("n",$vide.substr($im,floor($P),1));
  4751. if (($P*8)%8 == 0) $color[1] = $color[1] >>7;
  4752. elseif (($P*8)%8 == 1) $color[1] = ($color[1] & 0x40)>>6;
  4753. elseif (($P*8)%8 == 2) $color[1] = ($color[1] & 0x20)>>5;
  4754. elseif (($P*8)%8 == 3) $color[1] = ($color[1] & 0x10)>>4;
  4755. elseif (($P*8)%8 == 4) $color[1] = ($color[1] & 0x8)>>3;
  4756. elseif (($P*8)%8 == 5) $color[1] = ($color[1] & 0x4)>>2;
  4757. elseif (($P*8)%8 == 6) $color[1] = ($color[1] & 0x2)>>1;
  4758. elseif (($P*8)%8 == 7) $color[1] = ($color[1] & 0x1);
  4759. $color[1] = $palette[$color[1]+1];
  4760. } else
  4761. return FALSE;
  4762. imagesetpixel($res,$X,$Y,$color[1]);
  4763. $X++;
  4764. $P += $bmp['bytes_per_pixel'];
  4765. }
  4766. $Y--;
  4767. $P+=$bmp['decal'];
  4768. }
  4769. fclose($f1);
  4770. return $res;
  4771. }
  4772. /**
  4773. * Saves a BMP image
  4774. *
  4775. * This function has been published on the PHP website, and can be used freely
  4776. *
  4777. * @access public
  4778. */
  4779. function imagebmp(&$im, $filename = "") {
  4780. if (!$im) return false;
  4781. $w = imagesx($im);
  4782. $h = imagesy($im);
  4783. $result = '';
  4784. // if the image is not true color, we convert it first
  4785. if (!imageistruecolor($im)) {
  4786. $tmp = imagecreatetruecolor($w, $h);
  4787. imagecopy($tmp, $im, 0, 0, 0, 0, $w, $h);
  4788. imagedestroy($im);
  4789. $im = & $tmp;
  4790. }
  4791. $biBPLine = $w * 3;
  4792. $biStride = ($biBPLine + 3) & ~3;
  4793. $biSizeImage = $biStride * $h;
  4794. $bfOffBits = 54;
  4795. $bfSize = $bfOffBits + $biSizeImage;
  4796. $result .= substr('BM', 0, 2);
  4797. $result .= pack ('VvvV', $bfSize, 0, 0, $bfOffBits);
  4798. $result .= pack ('VVVvvVVVVVV', 40, $w, $h, 1, 24, 0, $biSizeImage, 0, 0, 0, 0);
  4799. $numpad = $biStride - $biBPLine;
  4800. for ($y = $h - 1; $y >= 0; --$y) {
  4801. for ($x = 0; $x < $w; ++$x) {
  4802. $col = imagecolorat ($im, $x, $y);
  4803. $result .= substr(pack ('V', $col), 0, 3);
  4804. }
  4805. for ($i = 0; $i < $numpad; ++$i)
  4806. $result .= pack ('C', 0);
  4807. }
  4808. if($filename==""){
  4809. echo $result;
  4810. } else {
  4811. $file = fopen($filename, "wb");
  4812. fwrite($file, $result);
  4813. fclose($file);
  4814. }
  4815. return true;
  4816. }
  4817. }
  4818. ?>