PageRenderTime 99ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 1ms

/saf/lib/Ext/upload/class.upload.php

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