Просмотр файла system/inc/library/class_zip/uploads.class.php

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