Просмотр файла system/classes/upload.php

Размер файла: 181.69Kb
  1. <?php
  2. /**
  3. * Class upload
  4. *
  5. * @version 0.31
  6. * @author Colin Verot <colin@verot.net>
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. * @copyright Colin Verot
  9. * @package cmf
  10. * @subpackage external
  11. */
  12.  
  13. class upload {
  14.  
  15. var $version;
  16. var $file_src_name;
  17. var $file_src_name_body;
  18. var $file_src_name_ext;
  19. var $file_src_mime;
  20. var $file_src_size;
  21. var $file_src_error;
  22. var $file_src_pathname;
  23. var $file_src_temp;
  24. var $file_dst_path;
  25. var $file_dst_name;
  26. var $file_dst_name_body;
  27. var $file_dst_name_ext;
  28. var $file_dst_pathname;
  29. var $image_src_x;
  30. var $image_src_y;
  31. var $image_src_bits;
  32. var $image_src_pixels;
  33. var $image_src_type;
  34. var $image_dst_x;
  35. var $image_dst_y;
  36. var $image_supported;
  37. var $file_is_image;
  38. var $uploaded;
  39. var $no_upload_check;
  40. var $processed;
  41. var $error;
  42. var $log;
  43.  
  44. // overiddable processing variables
  45.  
  46. var $file_new_name_body;
  47. var $file_name_body_add;
  48. var $file_name_body_pre;
  49. var $file_new_name_ext;
  50. var $file_safe_name;
  51. var $file_force_extension;
  52. var $mime_check;
  53. var $mime_fileinfo;
  54. var $mime_file;
  55. var $mime_magic;
  56. var $mime_getimagesize;
  57. var $no_script;
  58. var $file_auto_rename;
  59. var $dir_auto_create;
  60. var $dir_auto_chmod;
  61. var $dir_chmod;
  62. var $file_overwrite;
  63. var $file_max_size;
  64. var $image_resize;
  65. var $image_convert;
  66. var $image_x;
  67. var $image_y;
  68. var $image_ratio;
  69. var $image_ratio_crop;
  70. var $image_ratio_fill;
  71. var $image_ratio_pixels;
  72. var $image_ratio_no_zoom_in;
  73. var $image_ratio_no_zoom_out;
  74. var $image_ratio_x;
  75. var $image_ratio_y;
  76. var $image_max_width;
  77. var $image_max_height;
  78. var $image_max_pixels;
  79. var $image_max_ratio;
  80. var $image_min_width;
  81. var $image_min_height;
  82. var $image_min_pixels;
  83. var $image_min_ratio;
  84. var $jpeg_quality;
  85. var $jpeg_size;
  86. var $preserve_transparency;
  87. var $image_is_transparent;
  88. var $image_transparent_color;
  89. var $image_background_color;
  90. var $image_default_color;
  91. var $image_is_palette;
  92. var $image_brightness;
  93. var $image_contrast;
  94. var $image_opacity;
  95. var $image_threshold;
  96. var $image_tint_color;
  97. var $image_overlay_color;
  98. var $image_overlay_opacity;
  99. var $image_overlay_percent;
  100. var $image_negative;
  101. var $image_greyscale;
  102. var $image_unsharp;
  103. var $image_unsharp_amount;
  104. var $image_unsharp_radius;
  105. var $image_unsharp_threshold;
  106. var $image_text;
  107. var $image_text_direction;
  108. var $image_text_color;
  109. var $image_text_opacity;
  110. var $image_text_percent;
  111. var $image_text_background;
  112. var $image_text_background_opacity;
  113. var $image_text_background_percent;
  114. var $image_text_font;
  115. var $image_text_position;
  116. var $image_text_x;
  117. var $image_text_y;
  118. var $image_text_padding;
  119. var $image_text_padding_x;
  120. var $image_text_padding_y;
  121. var $image_text_alignment;
  122. var $image_text_line_spacing;
  123. var $image_reflection_height;
  124. var $image_reflection_space;
  125. var $image_reflection_color;
  126. var $image_reflection_opacity;
  127. var $image_flip;
  128. var $image_rotate;
  129. var $image_crop;
  130. var $image_precrop;
  131. var $image_bevel;
  132. var $image_bevel_color1;
  133. var $image_bevel_color2;
  134. var $image_border;
  135. var $image_border_color;
  136. var $image_border_opacity;
  137. var $image_border_transparent;
  138. var $image_frame;
  139. var $image_frame_colors;
  140. var $image_frame_opacity;
  141. var $image_watermark;
  142. var $image_watermark_position;
  143. var $image_watermark_x;
  144. var $image_watermark_y;
  145. var $image_watermark_no_zoom_in;
  146. var $image_watermark_no_zoom_out;
  147. var $mime_types;
  148. var $allowed;
  149. var $forbidden;
  150. var $translation;
  151. var $language;
  152. function init() {
  153.  
  154. // overiddable variables
  155. $this->file_new_name_body = null; // replace the name body
  156. $this->file_name_body_add = null; // append to the name body
  157. $this->file_name_body_pre = null; // prepend to the name body
  158. $this->file_new_name_ext = null; // replace the file extension
  159. $this->file_safe_name = true; // format safely the filename
  160. $this->file_force_extension = true; // forces extension if there isn't one
  161. $this->file_overwrite = false; // allows overwritting if the file already exists
  162. $this->file_auto_rename = true; // auto-rename if the file already exists
  163. $this->dir_auto_create = true; // auto-creates directory if missing
  164. $this->dir_auto_chmod = true; // auto-chmod directory if not writeable
  165. $this->dir_chmod = 0777; // default chmod to use
  166.  
  167. $this->no_script = true; // turns scripts into test files
  168. $this->mime_check = true; // checks the mime type against the allowed list
  169.  
  170. // these are the different MIME detection methods. if one of these method doesn't work on your
  171. // system, you can deactivate it here; just set it to false
  172. $this->mime_fileinfo = true; // MIME detection with Fileinfo PECL extension
  173. $this->mime_file = true; // MIME detection with UNIX file() command
  174. $this->mime_magic = true; // MIME detection with mime_magic (mime_content_type())
  175. $this->mime_getimagesize = true; // MIME detection with getimagesize()
  176.  
  177. // get the default max size from php.ini
  178. $this->file_max_size_raw = trim(ini_get('upload_max_filesize'));
  179. $this->file_max_size = $this->getsize($this->file_max_size_raw);
  180.  
  181. $this->image_resize = false; // resize the image
  182. $this->image_convert = ''; // convert. values :''; 'png'; 'jpeg'; 'gif'; 'bmp'
  183.  
  184. $this->image_x = 150;
  185. $this->image_y = 150;
  186. $this->image_ratio = false; // keeps aspect ratio with x and y dimensions
  187. $this->image_ratio_crop = false; // keeps aspect ratio with x and y dimensions, filling the space
  188. $this->image_ratio_fill = false; // keeps aspect ratio with x and y dimensions, fitting the image in the space, and coloring the rest
  189. $this->image_ratio_pixels = false; // keeps aspect ratio, calculating x and y so that the image is approx the set number of pixels
  190. $this->image_ratio_no_zoom_in = false;
  191. $this->image_ratio_no_zoom_out = false;
  192. $this->image_ratio_x = false; // calculate the $image_x if true
  193. $this->image_ratio_y = false; // calculate the $image_y if true
  194. $this->jpeg_quality = 85;
  195. $this->jpeg_size = null;
  196. $this->preserve_transparency = false;
  197. $this->image_is_transparent = false;
  198. $this->image_transparent_color = null;
  199. $this->image_background_color = null;
  200. $this->image_default_color = '#ffffff';
  201. $this->image_is_palette = false;
  202.  
  203. $this->image_max_width = null;
  204. $this->image_max_height = null;
  205. $this->image_max_pixels = null;
  206. $this->image_max_ratio = null;
  207. $this->image_min_width = null;
  208. $this->image_min_height = null;
  209. $this->image_min_pixels = null;
  210. $this->image_min_ratio = null;
  211.  
  212. $this->image_brightness = null;
  213. $this->image_contrast = null;
  214. $this->image_opacity = null;
  215. $this->image_threshold = null;
  216. $this->image_tint_color = null;
  217. $this->image_overlay_color = null;
  218. $this->image_overlay_opacity = null;
  219. $this->image_overlay_percent = null;
  220. $this->image_negative = false;
  221. $this->image_greyscale = false;
  222. $this->image_unsharp = false;
  223. $this->image_unsharp_amount = 80;
  224. $this->image_unsharp_radius = 0.5;
  225. $this->image_unsharp_threshold = 1;
  226.  
  227. $this->image_text = null;
  228. $this->image_text_direction = null;
  229. $this->image_text_color = '#FFFFFF';
  230. $this->image_text_opacity = 100;
  231. $this->image_text_percent = 100;
  232. $this->image_text_background = null;
  233. $this->image_text_background_opacity = 100;
  234. $this->image_text_background_percent = 100;
  235. $this->image_text_font = 5;
  236. $this->image_text_x = null;
  237. $this->image_text_y = null;
  238. $this->image_text_position = null;
  239. $this->image_text_padding = 0;
  240. $this->image_text_padding_x = null;
  241. $this->image_text_padding_y = null;
  242. $this->image_text_alignment = 'C';
  243. $this->image_text_line_spacing = 0;
  244.  
  245. $this->image_reflection_height = null;
  246. $this->image_reflection_space = 2;
  247. $this->image_reflection_color = '#ffffff';
  248. $this->image_reflection_opacity = 60;
  249.  
  250. $this->image_watermark = null;
  251. $this->image_watermark_x = null;
  252. $this->image_watermark_y = null;
  253. $this->image_watermark_position = null;
  254. $this->image_watermark_no_zoom_in = true;
  255. $this->image_watermark_no_zoom_out = false;
  256.  
  257. $this->image_flip = null;
  258. $this->image_rotate = null;
  259. $this->image_crop = null;
  260. $this->image_precrop = null;
  261.  
  262. $this->image_bevel = null;
  263. $this->image_bevel_color1 = '#FFFFFF';
  264. $this->image_bevel_color2 = '#000000';
  265. $this->image_border = null;
  266. $this->image_border_color = '#FFFFFF';
  267. $this->image_border_opacity = 100;
  268. $this->image_border_transparent = null;
  269. $this->image_frame = null;
  270. $this->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
  271. $this->image_frame_opacity = 100;
  272.  
  273. $this->forbidden = array();
  274. $this->allowed = array(
  275. 'application/arj',
  276. 'application/excel',
  277. 'application/gnutar',
  278. 'application/mspowerpoint',
  279. 'application/msword',
  280. 'application/octet-stream',
  281. 'application/onenote',
  282. 'application/pdf',
  283. 'application/plain',
  284. 'application/postscript',
  285. 'application/powerpoint',
  286. 'application/rar',
  287. 'application/rtf',
  288. 'application/vnd.ms-excel',
  289. 'application/vnd.ms-excel.addin.macroEnabled.12',
  290. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  291. 'application/vnd.ms-excel.sheet.macroEnabled.12',
  292. 'application/vnd.ms-excel.template.macroEnabled.12',
  293. 'application/vnd.ms-office',
  294. 'application/vnd.ms-officetheme',
  295. 'application/vnd.ms-powerpoint',
  296. 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  297. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  298. 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  299. 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  300. 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  301. 'application/vnd.ms-word',
  302. 'application/vnd.ms-word.document.macroEnabled.12',
  303. 'application/vnd.ms-word.template.macroEnabled.12',
  304. 'application/vnd.oasis.opendocument.chart',
  305. 'application/vnd.oasis.opendocument.database',
  306. 'application/vnd.oasis.opendocument.formula',
  307. 'application/vnd.oasis.opendocument.graphics',
  308. 'application/vnd.oasis.opendocument.graphics-template',
  309. 'application/vnd.oasis.opendocument.image',
  310. 'application/vnd.oasis.opendocument.presentation',
  311. 'application/vnd.oasis.opendocument.presentation-template',
  312. 'application/vnd.oasis.opendocument.spreadsheet',
  313. 'application/vnd.oasis.opendocument.spreadsheet-template',
  314. 'application/vnd.oasis.opendocument.text',
  315. 'application/vnd.oasis.opendocument.text-master',
  316. 'application/vnd.oasis.opendocument.text-template',
  317. 'application/vnd.oasis.opendocument.text-web',
  318. 'application/vnd.openofficeorg.extension',
  319. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  320. 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  321. 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  322. 'application/vnd.openxmlformats-officedocument.presentationml.template',
  323. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  324. 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  325. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  326. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  327. 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  328. 'application/vocaltec-media-file',
  329. 'application/wordperfect',
  330. 'application/x-bittorrent',
  331. 'application/x-bzip',
  332. 'application/x-bzip2',
  333. 'application/x-compressed',
  334. 'application/x-excel',
  335. 'application/x-gzip',
  336. 'application/x-latex',
  337. 'application/x-midi',
  338. 'application/xml',
  339. 'application/x-msexcel',
  340. 'application/x-rar',
  341. 'application/x-rar-compressed',
  342. 'application/x-rtf',
  343. 'application/x-shockwave-flash',
  344. 'application/x-sit',
  345. 'application/x-stuffit',
  346. 'application/x-troff-msvideo',
  347. 'application/x-zip',
  348. 'application/x-zip-compressed',
  349. 'application/zip',
  350. 'audio/*',
  351. 'image/*',
  352. 'multipart/x-gzip',
  353. 'multipart/x-zip',
  354. 'text/plain',
  355. 'text/rtf',
  356. 'text/richtext',
  357. 'text/xml',
  358. 'video/*'
  359. );
  360.  
  361. $this->mime_types = array(
  362. 'jpg' => 'image/jpeg',
  363. 'jpeg' => 'image/jpeg',
  364. 'jpe' => 'image/jpeg',
  365. 'gif' => 'image/gif',
  366. 'png' => 'image/png',
  367. 'bmp' => 'image/bmp',
  368. 'flv' => 'video/x-flv',
  369. 'js' => 'application/x-javascript',
  370. 'json' => 'application/json',
  371. 'tiff' => 'image/tiff',
  372. 'css' => 'text/css',
  373. 'xml' => 'application/xml',
  374. 'doc' => 'application/msword',
  375. 'docx' => 'application/msword',
  376. 'xls' => 'application/vnd.ms-excel',
  377. 'xlt' => 'application/vnd.ms-excel',
  378. 'xlm' => 'application/vnd.ms-excel',
  379. 'xld' => 'application/vnd.ms-excel',
  380. 'xla' => 'application/vnd.ms-excel',
  381. 'xlc' => 'application/vnd.ms-excel',
  382. 'xlw' => 'application/vnd.ms-excel',
  383. 'xll' => 'application/vnd.ms-excel',
  384. 'ppt' => 'application/vnd.ms-powerpoint',
  385. 'pps' => 'application/vnd.ms-powerpoint',
  386. 'rtf' => 'application/rtf',
  387. 'pdf' => 'application/pdf',
  388. 'html' => 'text/html',
  389. 'htm' => 'text/html',
  390. 'php' => 'text/html',
  391. 'txt' => 'text/plain',
  392. 'mpeg' => 'video/mpeg',
  393. 'mpg' => 'video/mpeg',
  394. 'mpe' => 'video/mpeg',
  395. 'mp3' => 'audio/mpeg3',
  396. 'wav' => 'audio/wav',
  397. 'aiff' => 'audio/aiff',
  398. 'aif' => 'audio/aiff',
  399. 'avi' => 'video/msvideo',
  400. 'wmv' => 'video/x-ms-wmv',
  401. 'mov' => 'video/quicktime',
  402. 'zip' => 'application/zip',
  403. 'tar' => 'application/x-tar',
  404. 'swf' => 'application/x-shockwave-flash',
  405. 'odt' => 'application/vnd.oasis.opendocument.text',
  406. 'ott' => 'application/vnd.oasis.opendocument.text-template',
  407. 'oth' => 'application/vnd.oasis.opendocument.text-web',
  408. 'odm' => 'application/vnd.oasis.opendocument.text-master',
  409. 'odg' => 'application/vnd.oasis.opendocument.graphics',
  410. 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
  411. 'odp' => 'application/vnd.oasis.opendocument.presentation',
  412. 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
  413. 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  414. 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
  415. 'odc' => 'application/vnd.oasis.opendocument.chart',
  416. 'odf' => 'application/vnd.oasis.opendocument.formula',
  417. 'odb' => 'application/vnd.oasis.opendocument.database',
  418. 'odi' => 'application/vnd.oasis.opendocument.image',
  419. 'oxt' => 'application/vnd.openofficeorg.extension',
  420. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  421. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  422. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  423. 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  424. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  425. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  426. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  427. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  428. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  429. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  430. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  431. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  432. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  433. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  434. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  435. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  436. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  437. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  438. 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  439. 'thmx' => 'application/vnd.ms-officetheme',
  440. 'onetoc' => 'application/onenote',
  441. 'onetoc2' => 'application/onenote',
  442. 'onetmp' => 'application/onenote',
  443. 'onepkg' => 'application/onenote',
  444. );
  445.  
  446. }
  447.  
  448. /**
  449. * Constructor. Checks if the file has been uploaded
  450. *
  451. * The constructor takes $_FILES['form_field'] array as argument
  452. * where form_field is the form field name
  453. *
  454. * The constructor will check if the file has been uploaded in its temporary location, and
  455. * accordingly will set {@link uploaded} (and {@link error} is an error occurred)
  456. *
  457. * If the file has been uploaded, the constructor will populate all the variables holding the upload
  458. * information (none of the processing class variables are used here).
  459. * You can have access to information about the file (name, size, MIME type...).
  460. *
  461. *
  462. * Alternatively, you can set the first argument to be a local filename (string)
  463. * This allows processing of a local file, as if the file was uploaded
  464. *
  465. * The optional second argument allows you to set the language for the error messages
  466. *
  467. * @access private
  468. * @param array $file $_FILES['form_field']
  469. * or string $file Local filename
  470. * @param string $lang Optional language code
  471. */
  472. function upload($file, $lang = 'en_GB') {
  473.  
  474. $this->version = '0.31';
  475.  
  476. $this->file_src_name = '';
  477. $this->file_src_name_body = '';
  478. $this->file_src_name_ext = '';
  479. $this->file_src_mime = '';
  480. $this->file_src_size = '';
  481. $this->file_src_error = '';
  482. $this->file_src_pathname = '';
  483. $this->file_src_temp = '';
  484.  
  485. $this->file_dst_path = '';
  486. $this->file_dst_name = '';
  487. $this->file_dst_name_body = '';
  488. $this->file_dst_name_ext = '';
  489. $this->file_dst_pathname = '';
  490.  
  491. $this->image_src_x = null;
  492. $this->image_src_y = null;
  493. $this->image_src_bits = null;
  494. $this->image_src_type = null;
  495. $this->image_src_pixels = null;
  496. $this->image_dst_x = 0;
  497. $this->image_dst_y = 0;
  498.  
  499. $this->uploaded = true;
  500. $this->no_upload_check = false;
  501. $this->processed = true;
  502. $this->error = '';
  503. $this->log = '';
  504. $this->allowed = array();
  505. $this->forbidden = array();
  506. $this->file_is_image = false;
  507. $this->init();
  508. $info = null;
  509. $mime_from_browser = null;
  510.  
  511. // sets default language
  512. $this->translation = array();
  513. $this->translation['file_error'] = 'File error. Please try again.';
  514. $this->translation['local_file_missing'] = 'Local file doesn\'t exist.';
  515. $this->translation['local_file_not_readable'] = 'Local file is not readable.';
  516. $this->translation['uploaded_too_big_ini'] = 'File upload error (the uploaded file exceeds the upload_max_filesize directive in php.ini).';
  517. $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).';
  518. $this->translation['uploaded_partial'] = 'File upload error (the uploaded file was only partially uploaded).';
  519. $this->translation['uploaded_missing'] = 'File upload error (no file was uploaded).';
  520. $this->translation['uploaded_no_tmp_dir'] = 'File upload error (missing a temporary folder).';
  521. $this->translation['uploaded_cant_write'] = 'File upload error (failed to write file to disk).';
  522. $this->translation['uploaded_err_extension'] = 'File upload error (file upload stopped by extension).';
  523. $this->translation['uploaded_unknown'] = 'File upload error (unknown error code).';
  524. $this->translation['try_again'] = 'File upload error. Please try again.';
  525. $this->translation['file_too_big'] = 'File too big.';
  526. $this->translation['no_mime'] = 'MIME type can\'t be detected.';
  527. $this->translation['incorrect_file'] = 'Incorrect type of file.';
  528. $this->translation['image_too_wide'] = 'Image too wide.';
  529. $this->translation['image_too_narrow'] = 'Image too narrow.';
  530. $this->translation['image_too_high'] = 'Image too tall.';
  531. $this->translation['image_too_short'] = 'Image too short.';
  532. $this->translation['ratio_too_high'] = 'Image ratio too high (image too wide).';
  533. $this->translation['ratio_too_low'] = 'Image ratio too low (image too high).';
  534. $this->translation['too_many_pixels'] = 'Image has too many pixels.';
  535. $this->translation['not_enough_pixels'] = 'Image has not enough pixels.';
  536. $this->translation['file_not_uploaded'] = 'File not uploaded. Can\'t carry on a process.';
  537. $this->translation['already_exists'] = '%s already exists. Please change the file name.';
  538. $this->translation['temp_file_missing'] = 'No correct temp source file. Can\'t carry on a process.';
  539. $this->translation['source_missing'] = 'No correct uploaded source file. Can\'t carry on a process.';
  540. $this->translation['destination_dir'] = 'Destination directory can\'t be created. Can\'t carry on a process.';
  541. $this->translation['destination_dir_missing'] = 'Destination directory doesn\'t exist. Can\'t carry on a process.';
  542. $this->translation['destination_path_not_dir'] = 'Destination path is not a directory. Can\'t carry on a process.';
  543. $this->translation['destination_dir_write'] = 'Destination directory can\'t be made writeable. Can\'t carry on a process.';
  544. $this->translation['destination_path_write'] = 'Destination path is not a writeable. Can\'t carry on a process.';
  545. $this->translation['temp_file'] = 'Can\'t create the temporary file. Can\'t carry on a process.';
  546. $this->translation['source_not_readable'] = 'Source file is not readable. Can\'t carry on a process.';
  547. $this->translation['no_create_support'] = 'No create from %s support.';
  548. $this->translation['create_error'] = 'Error in creating %s image from source.';
  549. $this->translation['source_invalid'] = 'Can\'t read image source. Not an image?.';
  550. $this->translation['gd_missing'] = 'GD doesn\'t seem to be present.';
  551. $this->translation['watermark_no_create_support'] = 'No create from %s support, can\'t read watermark.';
  552. $this->translation['watermark_create_error'] = 'No %s read support, can\'t create watermark.';
  553. $this->translation['watermark_invalid'] = 'Unknown image format, can\'t read watermark.';
  554. $this->translation['file_create'] = 'No %s create support.';
  555. $this->translation['no_conversion_type'] = 'No conversion type defined.';
  556. $this->translation['copy_failed'] = 'Error copying file on the server. copy() failed.';
  557. $this->translation['reading_failed'] = 'Error reading the file.';
  558.  
  559. // determines the language
  560. $this->lang = $lang;
  561. if ($this->lang != 'en_GB' && file_exists(dirname(__FILE__).'/lang') && file_exists(dirname(__FILE__).'/lang/class.upload.' . $lang . '.php')) {
  562. $translation = null;
  563. include(dirname(__FILE__).'/lang/class.upload.' . $lang . '.php');
  564. if (is_array($translation)) {
  565. $this->translation = array_merge($this->translation, $translation);
  566. } else {
  567. $this->lang = 'en_GB';
  568. }
  569. }
  570.  
  571.  
  572. // determines the supported MIME types, and matching image format
  573. $this->image_supported = array();
  574. if ($this->gdversion()) {
  575. if (imagetypes() & IMG_GIF) {
  576. $this->image_supported['image/gif'] = 'gif';
  577. }
  578. if (imagetypes() & IMG_JPG) {
  579. $this->image_supported['image/jpg'] = 'jpg';
  580. $this->image_supported['image/jpeg'] = 'jpg';
  581. $this->image_supported['image/pjpeg'] = 'jpg';
  582. }
  583. if (imagetypes() & IMG_PNG) {
  584. $this->image_supported['image/png'] = 'png';
  585. $this->image_supported['image/x-png'] = 'png';
  586. }
  587. if (imagetypes() & IMG_WBMP) {
  588. $this->image_supported['image/bmp'] = 'bmp';
  589. $this->image_supported['image/x-ms-bmp'] = 'bmp';
  590. $this->image_supported['image/x-windows-bmp'] = 'bmp';
  591. }
  592. }
  593.  
  594. // display some system information
  595. if (empty($this->log)) {
  596. $this->log .= '<b>system information</b><br />';
  597. if (function_exists('ini_get_all')) {
  598. $inis = ini_get_all();
  599. $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;
  600. } else {
  601. $open_basedir = false;
  602. }
  603. $gd = $this->gdversion() ? $this->gdversion(true) : 'GD not present';
  604. $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' : ''));
  605. $this->log .= '-&nbsp;class version : ' . $this->version . '<br />';
  606. $this->log .= '-&nbsp;operating system : ' . PHP_OS . '<br />';
  607. $this->log .= '-&nbsp;PHP version : ' . PHP_VERSION . '<br />';
  608. $this->log .= '-&nbsp;GD version : ' . $gd . '<br />';
  609. $this->log .= '-&nbsp;supported image types : ' . (!empty($supported) ? $supported : 'none') . '<br />';
  610. $this->log .= '-&nbsp;open_basedir : ' . (!empty($open_basedir) ? $open_basedir : 'no restriction') . '<br />';
  611. $this->log .= '-&nbsp;upload_max_filesize : ' . $this->file_max_size_raw . ' (' . $this->file_max_size . ' bytes)<br />';
  612. $this->log .= '-&nbsp;language : ' . $this->lang . '<br />';
  613. }
  614.  
  615. if (!$file) {
  616. $this->uploaded = false;
  617. $this->error = $this->translate('file_error');
  618. }
  619.  
  620. // check if we sent a local filename rather than a $_FILE element
  621. if (!is_array($file)) {
  622. if (empty($file)) {
  623. $this->uploaded = false;
  624. $this->error = $this->translate('file_error');
  625. } else {
  626. $this->no_upload_check = TRUE;
  627. // this is a local filename, i.e.not uploaded
  628. $this->log .= '<b>' . $this->translate("source is a local file") . ' ' . $file . '</b><br />';
  629.  
  630. if ($this->uploaded && !file_exists($file)) {
  631. $this->uploaded = false;
  632. $this->error = $this->translate('local_file_missing');
  633. }
  634.  
  635. if ($this->uploaded && !is_readable($file)) {
  636. $this->uploaded = false;
  637. $this->error = $this->translate('local_file_not_readable');
  638. }
  639.  
  640. if ($this->uploaded) {
  641. $this->file_src_pathname = $file;
  642. $this->file_src_name = basename($file);
  643. $this->log .= '- local file name OK<br />';
  644. preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
  645. if (is_array($extension) && sizeof($extension) > 0) {
  646. $this->file_src_name_ext = strtolower($extension[1]);
  647. $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1);
  648. } else {
  649. $this->file_src_name_ext = '';
  650. $this->file_src_name_body = $this->file_src_name;
  651. }
  652. $this->file_src_size = (file_exists($file) ? filesize($file) : 0);
  653. }
  654. $this->file_src_error = 0;
  655. }
  656. } else {
  657. // this is an element from $_FILE, i.e. an uploaded file
  658. $this->log .= '<b>source is an uploaded file</b><br />';
  659. if ($this->uploaded) {
  660. $this->file_src_error = trim($file['error']);
  661. switch($this->file_src_error) {
  662. case UPLOAD_ERR_OK:
  663. // all is OK
  664. $this->log .= '- upload OK<br />';
  665. break;
  666. case UPLOAD_ERR_INI_SIZE:
  667. $this->uploaded = false;
  668. $this->error = $this->translate('uploaded_too_big_ini');
  669. break;
  670. case UPLOAD_ERR_FORM_SIZE:
  671. $this->uploaded = false;
  672. $this->error = $this->translate('uploaded_too_big_html');
  673. break;
  674. case UPLOAD_ERR_PARTIAL:
  675. $this->uploaded = false;
  676. $this->error = $this->translate('uploaded_partial');
  677. break;
  678. case UPLOAD_ERR_NO_FILE:
  679. $this->uploaded = false;
  680. $this->error = $this->translate('uploaded_missing');
  681. break;
  682. case @UPLOAD_ERR_NO_TMP_DIR:
  683. $this->uploaded = false;
  684. $this->error = $this->translate('uploaded_no_tmp_dir');
  685. break;
  686. case @UPLOAD_ERR_CANT_WRITE:
  687. $this->uploaded = false;
  688. $this->error = $this->translate('uploaded_cant_write');
  689. break;
  690. case @UPLOAD_ERR_EXTENSION:
  691. $this->uploaded = false;
  692. $this->error = $this->translate('uploaded_err_extension');
  693. break;
  694. default:
  695. $this->uploaded = false;
  696. $this->error = $this->translate('uploaded_unknown') . ' ('.$this->file_src_error.')';
  697. }
  698. }
  699.  
  700. if ($this->uploaded) {
  701. $this->file_src_pathname = $file['tmp_name'];
  702. $this->file_src_name = $file['name'];
  703. if ($this->file_src_name == '') {
  704. $this->uploaded = false;
  705. $this->error = $this->translate('try_again');
  706. }
  707. }
  708.  
  709. if ($this->uploaded) {
  710. $this->log .= '- file name OK<br />';
  711. preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
  712. if (is_array($extension) && sizeof($extension) > 0) {
  713. $this->file_src_name_ext = strtolower($extension[1]);
  714. $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1);
  715. } else {
  716. $this->file_src_name_ext = '';
  717. $this->file_src_name_body = $this->file_src_name;
  718. }
  719. $this->file_src_size = $file['size'];
  720. $mime_from_browser = $file['type'];
  721. }
  722. }
  723.  
  724. if ($this->uploaded) {
  725. $this->log .= '<b>determining MIME type</b><br />';
  726. $this->file_src_mime = null;
  727.  
  728. // checks MIME type with Fileinfo PECL extension
  729. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  730. if ($this->mime_fileinfo) {
  731. $this->log .= '- Checking MIME type with Fileinfo PECL extension<br />';
  732. if (function_exists('finfo_open')) {
  733. if ($this->mime_fileinfo !== '') {
  734. if ($this->mime_fileinfo === true) {
  735. if (getenv('MAGIC') === FALSE) {
  736. if (substr(PHP_OS, 0, 3) == 'WIN') {
  737. $path = realpath(ini_get('extension_dir') . '/../') . 'extras/magic';
  738. } else {
  739. $path = '/usr/share/file/magic';
  740. }
  741. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path defaults to ' . $path . '<br />';
  742. } else {
  743. $path = getenv('MAGIC');
  744. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' . $path . ' from MAGIC variable<br />';
  745. }
  746. } else {
  747. $path = $this->mime_fileinfo;
  748. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' . $path . '<br />';
  749. }
  750. $f = @finfo_open(FILEINFO_MIME, $path);
  751. } else {
  752. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path will not be used<br />';
  753. $f = @finfo_open(FILEINFO_MIME);
  754. }
  755. if (is_resource($f)) {
  756. $mime = finfo_file($f, realpath($this->file_src_pathname));
  757. finfo_close($f);
  758. $this->file_src_mime = $mime;
  759. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  760. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  761. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  762. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  763. } else {
  764. $this->file_src_mime = null;
  765. }
  766. } else {
  767. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo_open)<br />';
  768. }
  769. } elseif (@class_exists('finfo')) {
  770. $f = new finfo( FILEINFO_MIME );
  771. if ($f) {
  772. $this->file_src_mime = $f->file(realpath($this->file_src_pathname));
  773. $this->log .= '- MIME type detected as ' . $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  774. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  775. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  776. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  777. } else {
  778. $this->file_src_mime = null;
  779. }
  780. } else {
  781. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo)<br />';
  782. }
  783. } else {
  784. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension not available<br />';
  785. }
  786. } else {
  787. $this->log .= '- Fileinfo PECL extension deactivated<br />';
  788. }
  789. }
  790.  
  791. // checks MIME type with shell if unix access is authorized
  792. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  793. if ($this->mime_file) {
  794. $this->log .= '- Checking MIME type with UNIX file() command<br />';
  795. if (substr(PHP_OS, 0, 3) != 'WIN') {
  796. if (function_exists('exec')) {
  797. if (strlen($mime = @exec("file -bi ".escapeshellarg($this->file_src_pathname))) != 0) {
  798. $this->file_src_mime = trim($mime);
  799. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by UNIX file() command<br />';
  800. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  801. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  802. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  803. } else {
  804. $this->file_src_mime = null;
  805. }
  806. } else {
  807. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command failed<br />';
  808. }
  809. } else {
  810. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PHP exec() function is disabled<br />';
  811. }
  812. } else {
  813. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command not availabled<br />';
  814. }
  815. } else {
  816. $this->log .= '- UNIX file() command is deactivated<br />';
  817. }
  818. }
  819.  
  820. // checks MIME type with mime_magic
  821. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  822. if ($this->mime_magic) {
  823. $this->log .= '- Checking MIME type with mime.magic file (mime_content_type())<br />';
  824. if (function_exists('mime_content_type')) {
  825. $this->file_src_mime = mime_content_type($this->file_src_pathname);
  826. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by mime_content_type()<br />';
  827. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  828. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  829. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  830. } else {
  831. $this->file_src_mime = null;
  832. }
  833. } else {
  834. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;mime_content_type() is not available<br />';
  835. }
  836. } else {
  837. $this->log .= '- mime.magic file (mime_content_type()) is deactivated<br />';
  838. }
  839. }
  840.  
  841. // checks MIME type with getimagesize()
  842. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  843. if ($this->mime_getimagesize) {
  844. $this->log .= '- Checking MIME type with getimagesize()<br />';
  845. $info = getimagesize($this->file_src_pathname);
  846. if (is_array($info) && array_key_exists('mime', $info)) {
  847. $this->file_src_mime = trim($info['mime']);
  848. if (empty($this->file_src_mime)) {
  849. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME empty, guessing from type<br />';
  850. $mime = (is_array($info) && array_key_exists(2, $info) ? $info[2] : null); // 1 = GIF, 2 = JPG, 3 = PNG
  851. $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))));
  852. }
  853. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by PHP getimagesize() function<br />';
  854. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  855. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  856. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  857. } else {
  858. $this->file_src_mime = null;
  859. }
  860. } else {
  861. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;getimagesize() failed<br />';
  862. }
  863. } else {
  864. $this->log .= '- getimagesize() is deactivated<br />';
  865. }
  866. }
  867.  
  868. // default to MIME from browser (or Flash)
  869. if (!empty($mime_from_browser) && !$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime)) {
  870. $this->file_src_mime =$mime_from_browser;
  871. $this->log .= '- MIME type detected as ' . $this->file_src_mime . ' by browser<br />';
  872. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  873. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  874. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  875. } else {
  876. $this->file_src_mime = null;
  877. }
  878. }
  879.  
  880. // we need to work some magic if we upload via Flash
  881. 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) {
  882. if ($this->file_src_mime == 'application/octet-stream') $this->log .= '- Flash may be rewriting MIME as application/octet-stream<br />';
  883. $this->log .= '- Try to guess MIME type from file extension (' . $this->file_src_name_ext . '): ';
  884. if (array_key_exists($this->file_src_name_ext, $this->mime_types)) $this->file_src_mime = $this->mime_types[$this->file_src_name_ext];
  885. if ($this->file_src_mime == 'application/octet-stream') {
  886. $this->log .= 'doesn\'t look like anything known<br />';
  887. } else {
  888. $this->log .= 'MIME type set to ' . $this->file_src_mime . '<br />';
  889. }
  890. }
  891.  
  892. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  893. $this->log .= '- MIME type couldn\'t be detected! (' . (string) $this->file_src_mime . ')<br />';
  894. }
  895.  
  896. // determine whether the file is an image
  897. 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)) {
  898. $this->file_is_image = true;
  899. $this->image_src_type = $this->image_supported[$this->file_src_mime];
  900. }
  901.  
  902. // if the file is an image, we gather some useful data
  903. if ($this->file_is_image) {
  904. if ($h = fopen($this->file_src_pathname, 'r')) {
  905. fclose($h);
  906. $info = getimagesize($this->file_src_pathname);
  907. if (is_array($info)) {
  908. $this->image_src_x = $info[0];
  909. $this->image_src_y = $info[1];
  910. $this->image_dst_x = $this->image_src_x;
  911. $this->image_dst_y = $this->image_src_y;
  912. $this->image_src_pixels = $this->image_src_x * $this->image_src_y;
  913. $this->image_src_bits = array_key_exists('bits', $info) ? $info['bits'] : null;
  914. } else {
  915. $this->file_is_image = false;
  916. $this->uploaded = false;
  917. $this->log .= '- can\'t retrieve image information, image may have been tampered with<br />';
  918. $this->error = $this->translate('source_invalid');
  919. }
  920. } else {
  921. $this->log .= '- can\'t read source file directly. open_basedir restriction in place?<br />';
  922. }
  923. }
  924.  
  925. $this->log .= '<b>source variables</b><br />';
  926. $this->log .= '- You can use all these before calling process()<br />';
  927. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name : ' . $this->file_src_name . '<br />';
  928. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_body : ' . $this->file_src_name_body . '<br />';
  929. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_ext : ' . $this->file_src_name_ext . '<br />';
  930. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_pathname : ' . $this->file_src_pathname . '<br />';
  931. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_mime : ' . $this->file_src_mime . '<br />';
  932. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_size : ' . $this->file_src_size . ' (max= ' . $this->file_max_size . ')<br />';
  933. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_error : ' . $this->file_src_error . '<br />';
  934.  
  935. if ($this->file_is_image) {
  936. $this->log .= '- source file is an image<br />';
  937. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x : ' . $this->image_src_x . '<br />';
  938. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_y : ' . $this->image_src_y . '<br />';
  939. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_pixels : ' . $this->image_src_pixels . '<br />';
  940. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_type : ' . $this->image_src_type . '<br />';
  941. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_bits : ' . $this->image_src_bits . '<br />';
  942. }
  943. }
  944.  
  945. }
  946.  
  947. /**
  948. * Returns the version of GD
  949. *
  950. * @access public
  951. * @param boolean $full Optional flag to get precise version
  952. * @return float GD version
  953. */
  954. function gdversion($full = false) {
  955. static $gd_version = null;
  956. static $gd_full_version = null;
  957. if ($gd_version === null) {
  958. if (function_exists('gd_info')) {
  959. $gd = gd_info();
  960. $gd = $gd["GD Version"];
  961. $regex = "/([\d\.]+)/i";
  962. } else {
  963. ob_start();
  964. phpinfo(8);
  965. $gd = ob_get_contents();
  966. ob_end_clean();
  967. $regex = "/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i";
  968. }
  969. if (preg_match($regex, $gd, $m)) {
  970. $gd_full_version = (string) $m[1];
  971. $gd_version = (float) $m[1];
  972. } else {
  973. $gd_full_version = 'none';
  974. $gd_version = 0;
  975. }
  976. }
  977. if ($full) {
  978. return $gd_full_version;
  979. } else {
  980. return $gd_version;
  981. }
  982. }
  983.  
  984. /**
  985. * Creates directories recursively
  986. *
  987. * @access private
  988. * @param string $path Path to create
  989. * @param integer $mode Optional permissions
  990. * @return boolean Success
  991. */
  992. function rmkdir($path, $mode = 0777) {
  993. return is_dir($path) || ( $this->rmkdir(dirname($path), $mode) && $this->_mkdir($path, $mode) );
  994. }
  995.  
  996. /**
  997. * Creates directory
  998. *
  999. * @access private
  1000. * @param string $path Path to create
  1001. * @param integer $mode Optional permissions
  1002. * @return boolean Success
  1003. */
  1004. function _mkdir($path, $mode = 0777) {
  1005. $old = umask(0);
  1006. $res = @mkdir($path, $mode);
  1007. umask($old);
  1008. return $res;
  1009. }
  1010.  
  1011. /**
  1012. * Translate error messages
  1013. *
  1014. * @access private
  1015. * @param string $str Message to translate
  1016. * @param array $tokens Optional token values
  1017. * @return string Translated string
  1018. */
  1019. function translate($str, $tokens = array()) {
  1020. if (array_key_exists($str, $this->translation)) $str = $this->translation[$str];
  1021. if (is_array($tokens) && sizeof($tokens) > 0) $str = vsprintf($str, $tokens);
  1022. return $str;
  1023. }
  1024.  
  1025. /**
  1026. * Decodes colors
  1027. *
  1028. * @access private
  1029. * @param string $color Color string
  1030. * @return array RGB colors
  1031. */
  1032. function getcolors($color) {
  1033. $r = sscanf($color, "#%2x%2x%2x");
  1034. $red = (array_key_exists(0, $r) && is_numeric($r[0]) ? $r[0] : 0);
  1035. $green = (array_key_exists(1, $r) && is_numeric($r[1]) ? $r[1] : 0);
  1036. $blue = (array_key_exists(2, $r) && is_numeric($r[2]) ? $r[2] : 0);
  1037. return array($red, $green, $blue);
  1038. }
  1039.  
  1040. /**
  1041. * Decodes sizes
  1042. *
  1043. * @access private
  1044. * @param string $size Size in bytes, or shorthand byte options
  1045. * @return integer Size in bytes
  1046. */
  1047. function getsize($size) {
  1048. $last = strtolower($size{strlen($size)-1});
  1049. switch($last) {
  1050. case 'g':
  1051. $size *= 1024;
  1052. case 'm':
  1053. $size *= 1024;
  1054. case 'k':
  1055. $size *= 1024;
  1056. }
  1057. return $size;
  1058. }
  1059.  
  1060. /**
  1061. * Decodes offsets
  1062. *
  1063. * @access private
  1064. * @param misc $offsets Offsets, as an integer, a string or an array
  1065. * @param integer $x Reference picture width
  1066. * @param integer $y Reference picture height
  1067. * @param boolean $round Round offsets before returning them
  1068. * @param boolean $negative Allow negative offsets to be returned
  1069. * @return array Array of four offsets (TRBL)
  1070. */
  1071. function getoffsets($offsets, $x, $y, $round = true, $negative = true) {
  1072. if (!is_array($offsets)) $offsets = explode(' ', $offsets);
  1073. if (sizeof($offsets) == 4) {
  1074. $ct = $offsets[0]; $cr = $offsets[1]; $cb = $offsets[2]; $cl = $offsets[3];
  1075. } else if (sizeof($offsets) == 2) {
  1076. $ct = $offsets[0]; $cr = $offsets[1]; $cb = $offsets[0]; $cl = $offsets[1];
  1077. } else {
  1078. $ct = $offsets[0]; $cr = $offsets[0]; $cb = $offsets[0]; $cl = $offsets[0];
  1079. }
  1080. if (strpos($ct, '%')>0) $ct = $y * (str_replace('%','',$ct) / 100);
  1081. if (strpos($cr, '%')>0) $cr = $x * (str_replace('%','',$cr) / 100);
  1082. if (strpos($cb, '%')>0) $cb = $y * (str_replace('%','',$cb) / 100);
  1083. if (strpos($cl, '%')>0) $cl = $x * (str_replace('%','',$cl) / 100);
  1084. if (strpos($ct, 'px')>0) $ct = str_replace('px','',$ct);
  1085. if (strpos($cr, 'px')>0) $cr = str_replace('px','',$cr);
  1086. if (strpos($cb, 'px')>0) $cb = str_replace('px','',$cb);
  1087. if (strpos($cl, 'px')>0) $cl = str_replace('px','',$cl);
  1088. $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl;
  1089. if ($round) {
  1090. $ct = round($ct);
  1091. $cr = round($cr);
  1092. $cb = round($cb);
  1093. $cl = round($cl);
  1094. }
  1095. if (!$negative) {
  1096. if ($ct < 0) $ct = 0;
  1097. if ($cr < 0) $cr = 0;
  1098. if ($cb < 0) $cb = 0;
  1099. if ($cl < 0) $cl = 0;
  1100. }
  1101. return array($ct, $cr, $cb, $cl);
  1102. }
  1103.  
  1104. /**
  1105. * Creates a container image
  1106. *
  1107. * @access private
  1108. * @param integer $x Width
  1109. * @param integer $y Height
  1110. * @param boolean $fill Optional flag to draw the background color or not
  1111. * @param boolean $trsp Optional flag to set the background to be transparent
  1112. * @return resource Container image
  1113. */
  1114. function imagecreatenew($x, $y, $fill = true, $trsp = false) {
  1115. if ($x < 1) $x = 1; if ($y < 1) $y = 1;
  1116. if ($this->gdversion() >= 2 && !$this->image_is_palette) {
  1117. // create a true color image
  1118. $dst_im = imagecreatetruecolor($x, $y);
  1119. // this preserves transparency in PNGs, in true color
  1120. if (empty($this->image_background_color) || $trsp) {
  1121. imagealphablending($dst_im, false );
  1122. imagefilledrectangle($dst_im, 0, 0, $x, $y, imagecolorallocatealpha($dst_im, 0, 0, 0, 127));
  1123. }
  1124. } else {
  1125. // creates a palette image
  1126. $dst_im = imagecreate($x, $y);
  1127. // preserves transparency for palette images, if the original image has transparency
  1128. if (($fill && $this->image_is_transparent && empty($this->image_background_color)) || $trsp) {
  1129. imagefilledrectangle($dst_im, 0, 0, $x, $y, $this->image_transparent_color);
  1130. imagecolortransparent($dst_im, $this->image_transparent_color);
  1131. }
  1132. }
  1133. // fills with background color if any is set
  1134. if ($fill && !empty($this->image_background_color) && !$trsp) {
  1135. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  1136. $background_color = imagecolorallocate($dst_im, $red, $green, $blue);
  1137. imagefilledrectangle($dst_im, 0, 0, $x, $y, $background_color);
  1138. }
  1139. return $dst_im;
  1140. }
  1141.  
  1142.  
  1143. /**
  1144. * Transfers an image from the container to the destination image
  1145. *
  1146. * @access private
  1147. * @param resource $src_im Container image
  1148. * @param resource $dst_im Destination image
  1149. * @return resource Destination image
  1150. */
  1151. function imagetransfer($src_im, $dst_im) {
  1152. if (is_resource($dst_im)) imagedestroy($dst_im);
  1153. $dst_im = & $src_im;
  1154. return $dst_im;
  1155. }
  1156.  
  1157. /**
  1158. * Merges two images
  1159. *
  1160. * If the output format is PNG, then we do it pixel per pixel to retain the alpha channel
  1161. *
  1162. * @access private
  1163. * @param resource $dst_img Destination image
  1164. * @param resource $src_img Overlay image
  1165. * @param int $dst_x x-coordinate of destination point
  1166. * @param int $dst_y y-coordinate of destination point
  1167. * @param int $src_x x-coordinate of source point
  1168. * @param int $src_y y-coordinate of source point
  1169. * @param int $src_w Source width
  1170. * @param int $src_h Source height
  1171. * @param int $pct Optional percentage of the overlay, between 0 and 100 (default: 100)
  1172. * @return resource Destination image
  1173. */
  1174. function imagecopymergealpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct = 0) {
  1175. $dst_x = (int) $dst_x;
  1176. $dst_y = (int) $dst_y;
  1177. $src_x = (int) $src_x;
  1178. $src_y = (int) $src_y;
  1179. $src_w = (int) $src_w;
  1180. $src_h = (int) $src_h;
  1181. $pct = (int) $pct;
  1182. $dst_w = imagesx($dst_im);
  1183. $dst_h = imagesy($dst_im);
  1184.  
  1185. for ($y = $src_y; $y < $src_h; $y++) {
  1186. for ($x = $src_x; $x < $src_w; $x++) {
  1187.  
  1188. if ($x + $dst_x >= 0 && $x + $dst_x < $dst_w && $x + $src_x >= 0 && $x + $src_x < $src_w
  1189. && $y + $dst_y >= 0 && $y + $dst_y < $dst_h && $y + $src_y >= 0 && $y + $src_y < $src_h) {
  1190.  
  1191. $dst_pixel = imagecolorsforindex($dst_im, imagecolorat($dst_im, $x + $dst_x, $y + $dst_y));
  1192. $src_pixel = imagecolorsforindex($src_im, imagecolorat($src_im, $x + $src_x, $y + $src_y));
  1193.  
  1194. $src_alpha = 1 - ($src_pixel['alpha'] / 127);
  1195. $dst_alpha = 1 - ($dst_pixel['alpha'] / 127);
  1196. $opacity = $src_alpha * $pct / 100;
  1197. if ($dst_alpha >= $opacity) $alpha = $dst_alpha;
  1198. if ($dst_alpha < $opacity) $alpha = $opacity;
  1199. if ($alpha > 1) $alpha = 1;
  1200.  
  1201. if ($opacity > 0) {
  1202. $dst_red = round(( ($dst_pixel['red'] * $dst_alpha * (1 - $opacity)) ) );
  1203. $dst_green = round(( ($dst_pixel['green'] * $dst_alpha * (1 - $opacity)) ) );
  1204. $dst_blue = round(( ($dst_pixel['blue'] * $dst_alpha * (1 - $opacity)) ) );
  1205. $src_red = round((($src_pixel['red'] * $opacity)) );
  1206. $src_green = round((($src_pixel['green'] * $opacity)) );
  1207. $src_blue = round((($src_pixel['blue'] * $opacity)) );
  1208. $red = round(($dst_red + $src_red ) / ($dst_alpha * (1 - $opacity) + $opacity));
  1209. $green = round(($dst_green + $src_green) / ($dst_alpha * (1 - $opacity) + $opacity));
  1210. $blue = round(($dst_blue + $src_blue ) / ($dst_alpha * (1 - $opacity) + $opacity));
  1211. if ($red > 255) $red = 255;
  1212. if ($green > 255) $green = 255;
  1213. if ($blue > 255) $blue = 255;
  1214. $alpha = round((1 - $alpha) * 127);
  1215. $color = imagecolorallocatealpha($dst_im, $red, $green, $blue, $alpha);
  1216. imagesetpixel($dst_im, $x + $dst_x, $y + $dst_y, $color);
  1217. }
  1218. }
  1219. }
  1220. }
  1221. return true;
  1222. }
  1223.  
  1224.  
  1225.  
  1226. /**
  1227. * Actually uploads the file, and act on it according to the set processing class variables
  1228. *
  1229. * This function copies the uploaded file to the given location, eventually performing actions on it.
  1230. * Typically, you can call {@link process} several times for the same file,
  1231. * for instance to create a resized image and a thumbnail of the same file.
  1232. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times.
  1233. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls.
  1234. *
  1235. * According to the processing class variables set in the calling file, the file can be renamed,
  1236. * and if it is an image, can be resized or converted.
  1237. *
  1238. * When the processing is completed, and the file copied to its new location, the
  1239. * processing class variables will be reset to their default value.
  1240. * This allows you to set new properties, and perform another {@link process} on the same uploaded file
  1241. *
  1242. * If the function is called with a null or empty argument, then it will return the content of the picture
  1243. *
  1244. * It will set {@link processed} (and {@link error} is an error occurred)
  1245. *
  1246. * @access public
  1247. * @param string $server_path Optional path location of the uploaded file, with an ending slash
  1248. * @return string Optional content of the image
  1249. */
  1250. function process($server_path = null) {
  1251. $this->error = '';
  1252. $this->processed = true;
  1253. $return_mode = false;
  1254. $return_content = null;
  1255.  
  1256. // clean up dst variables
  1257. $this->file_dst_path = '';
  1258. $this->file_dst_pathname = '';
  1259. $this->file_dst_name = '';
  1260. $this->file_dst_name_body = '';
  1261. $this->file_dst_name_ext = '';
  1262.  
  1263. // clean up some parameters
  1264. $this->file_max_size = $this->getsize($this->file_max_size);
  1265. $this->jpeg_size = $this->getsize($this->jpeg_size);
  1266. // some parameters are being deprecated, and replaced with others
  1267. if (is_null($this->image_overlay_opacity)) $this->image_overlay_opacity = $this->image_overlay_percent;
  1268. if ($this->image_text_opacity == 100) $this->image_text_opacity = $this->image_text_percent;
  1269. if ($this->image_text_background_opacity == 100) $this->image_text_background_opacity = $this->image_text_background_percent;
  1270.  
  1271. // copy some variables as we need to keep them clean
  1272. $file_src_name = $this->file_src_name;
  1273. $file_src_name_body = $this->file_src_name_body;
  1274. $file_src_name_ext = $this->file_src_name_ext;
  1275.  
  1276. if (!$this->uploaded) {
  1277. $this->error = $this->translate('file_not_uploaded');
  1278. $this->processed = false;
  1279. }
  1280.  
  1281. if ($this->processed) {
  1282. if (empty($server_path) || is_null($server_path)) {
  1283. $this->log .= '<b>process file and return the content</b><br />';
  1284. $return_mode = true;
  1285. } else {
  1286. if(strtolower(substr(PHP_OS, 0, 3)) === 'win') {
  1287. if (substr($server_path, -1, 1) != '\\') $server_path = $server_path . '\\';
  1288. } else {
  1289. if (substr($server_path, -1, 1) != '/') $server_path = $server_path . '/';
  1290. }
  1291. $this->log .= '<b>process file to ' . $server_path . '</b><br />';
  1292. }
  1293. }
  1294.  
  1295. if ($this->processed) {
  1296. // checks file max size
  1297. if ($this->file_src_size > $this->file_max_size) {
  1298. $this->processed = false;
  1299. $this->error = $this->translate('file_too_big');
  1300. } else {
  1301. $this->log .= '- file size OK<br />';
  1302. }
  1303. }
  1304.  
  1305. if ($this->processed) {
  1306. // if we have an image without extension, set it
  1307. if ($this->file_force_extension && $this->file_is_image && !$this->file_src_name_ext) $file_src_name_ext = $this->image_src_type;
  1308. // turn dangerous scripts into text files
  1309. if ($this->no_script) {
  1310. // if the file has no extension, we try to guess it from the MIME type
  1311. if ($this->file_force_extension && empty($file_src_name_ext)) {
  1312. if ($key = array_search($this->file_src_mime, $this->mime_types)) {
  1313. $file_src_name_ext = $key;
  1314. $file_src_name = $file_src_name_body . '.' . $file_src_name_ext;
  1315. $this->log .= '- file renamed as ' . $file_src_name_body . '.' . $file_src_name_ext . '!<br />';
  1316. }
  1317. }
  1318. // if the file is text based, or has a dangerous extension, we rename it as .txt
  1319. if ((((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf') || strpos($this->file_src_mime, 'javascript') !== false) && (substr($file_src_name, -4) != '.txt'))
  1320. || preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $this->file_src_name)
  1321. || $this->file_force_extension && empty($file_src_name_ext)) {
  1322. $this->file_src_mime = 'text/plain';
  1323. if ($this->file_src_name_ext) $file_src_name_body = $file_src_name_body . '.' . $this->file_src_name_ext;
  1324. $file_src_name_ext = 'txt';
  1325. $file_src_name = $file_src_name_body . '.' . $file_src_name_ext;
  1326. $this->log .= '- script renamed as ' . $file_src_name_body . '.' . $file_src_name_ext . '!<br />';
  1327. }
  1328. }
  1329.  
  1330. if ($this->mime_check && empty($this->file_src_mime)) {
  1331. $this->processed = false;
  1332. $this->error = $this->translate('no_mime');
  1333. } else if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) {
  1334. list($m1, $m2) = explode('/', $this->file_src_mime);
  1335. $allowed = false;
  1336. // check wether the mime type is allowed
  1337. if (!is_array($this->allowed)) $this->allowed = array($this->allowed);
  1338. foreach($this->allowed as $k => $v) {
  1339. list($v1, $v2) = explode('/', $v);
  1340. if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  1341. $allowed = true;
  1342. break;
  1343. }
  1344. }
  1345. // check wether the mime type is forbidden
  1346. if (!is_array($this->forbidden)) $this->forbidden = array($this->forbidden);
  1347. foreach($this->forbidden as $k => $v) {
  1348. list($v1, $v2) = explode('/', $v);
  1349. if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  1350. $allowed = false;
  1351. break;
  1352. }
  1353. }
  1354. if (!$allowed) {
  1355. $this->processed = false;
  1356. $this->error = $this->translate('incorrect_file');
  1357. } else {
  1358. $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />';
  1359. }
  1360. } else {
  1361. $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />';
  1362. }
  1363.  
  1364. // if the file is an image, we can check on its dimensions
  1365. // these checks are not available if open_basedir restrictions are in place
  1366. if ($this->file_is_image) {
  1367. if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) {
  1368. $ratio = $this->image_src_x / $this->image_src_y;
  1369. if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) {
  1370. $this->processed = false;
  1371. $this->error = $this->translate('image_too_wide');
  1372. }
  1373. if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) {
  1374. $this->processed = false;
  1375. $this->error = $this->translate('image_too_narrow');
  1376. }
  1377. if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) {
  1378. $this->processed = false;
  1379. $this->error = $this->translate('image_too_high');
  1380. }
  1381. if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) {
  1382. $this->processed = false;
  1383. $this->error = $this->translate('image_too_short');
  1384. }
  1385. if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) {
  1386. $this->processed = false;
  1387. $this->error = $this->translate('ratio_too_high');
  1388. }
  1389. if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) {
  1390. $this->processed = false;
  1391. $this->error = $this->translate('ratio_too_low');
  1392. }
  1393. if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) {
  1394. $this->processed = false;
  1395. $this->error = $this->translate('too_many_pixels');
  1396. }
  1397. if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) {
  1398. $this->processed = false;
  1399. $this->error = $this->translate('not_enough_pixels');
  1400. }
  1401. } else {
  1402. $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />';
  1403. }
  1404. }
  1405. }
  1406.  
  1407. if ($this->processed) {
  1408. $this->file_dst_path = $server_path;
  1409.  
  1410. // repopulate dst variables from src
  1411. $this->file_dst_name = $file_src_name;
  1412. $this->file_dst_name_body = $file_src_name_body;
  1413. $this->file_dst_name_ext = $file_src_name_ext;
  1414. if ($this->file_overwrite) $this->file_auto_rename = false;
  1415.  
  1416. if ($this->image_convert && $this->file_is_image) { // if we convert as an image
  1417. if ($this->file_src_name_ext) $this->file_dst_name_ext = $this->image_convert;
  1418. $this->log .= '- new file name ext : ' . $this->image_convert . '<br />';
  1419. }
  1420. if (!is_null($this->file_new_name_body)) { // rename file body
  1421. $this->file_dst_name_body = $this->file_new_name_body;
  1422. $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />';
  1423. }
  1424. if (!is_null($this->file_new_name_ext)) { // rename file ext
  1425. $this->file_dst_name_ext = $this->file_new_name_ext;
  1426. $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />';
  1427. }
  1428. if (!is_null($this->file_name_body_add)) { // append a string to the name
  1429. $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add;
  1430. $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />';
  1431. }
  1432. if (!is_null($this->file_name_body_pre)) { // prepend a string to the name
  1433. $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body;
  1434. $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />';
  1435. }
  1436. if ($this->file_safe_name) { // formats the name
  1437. $this->file_dst_name_body = strtr($this->file_dst_name_body, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
  1438. $this->file_dst_name_body = strtr($this->file_dst_name_body, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));
  1439. $this->file_dst_name_body = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $this->file_dst_name_body);
  1440. $this->log .= '- file name safe format<br />';
  1441. }
  1442.  
  1443. $this->log .= '- destination variables<br />';
  1444. if (empty($this->file_dst_path) || is_null($this->file_dst_path)) {
  1445. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path : n/a<br />';
  1446. } else {
  1447. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path : ' . $this->file_dst_path . '<br />';
  1448. }
  1449. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_body : ' . $this->file_dst_name_body . '<br />';
  1450. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />';
  1451.  
  1452. // do we do some image manipulation?
  1453. $image_manipulation = ($this->file_is_image && (
  1454. $this->image_resize
  1455. || $this->image_convert != ''
  1456. || is_numeric($this->image_brightness)
  1457. || is_numeric($this->image_contrast)
  1458. || is_numeric($this->image_opacity)
  1459. || is_numeric($this->image_threshold)
  1460. || !empty($this->image_tint_color)
  1461. || !empty($this->image_overlay_color)
  1462. || $this->image_unsharp
  1463. || !empty($this->image_text)
  1464. || $this->image_greyscale
  1465. || $this->image_negative
  1466. || !empty($this->image_watermark)
  1467. || is_numeric($this->image_rotate)
  1468. || is_numeric($this->jpeg_size)
  1469. || !empty($this->image_flip)
  1470. || !empty($this->image_crop)
  1471. || !empty($this->image_precrop)
  1472. || !empty($this->image_border)
  1473. || !empty($this->image_border_transparent)
  1474. || $this->image_frame > 0
  1475. || $this->image_bevel > 0
  1476. || $this->image_reflection_height));
  1477.  
  1478. // set the destination file name
  1479. $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  1480.  
  1481. if (!$return_mode) {
  1482. if (!$this->file_auto_rename) {
  1483. $this->log .= '- no auto_rename if same filename exists<br />';
  1484. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  1485. } else {
  1486. $this->log .= '- checking for auto_rename<br />';
  1487. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  1488. $body = $this->file_dst_name_body;
  1489. $ext = '';
  1490. // if we have changed the extension, then we add our increment before
  1491. if ($file_src_name_ext != $this->file_src_name_ext) {
  1492. if (substr($this->file_dst_name_body, -1 - strlen($this->file_src_name_ext)) == '.' . $this->file_src_name_ext) {
  1493. $body = substr($this->file_dst_name_body, 0, strlen($this->file_dst_name_body) - 1 - strlen($this->file_src_name_ext));
  1494. $ext = '.' . $this->file_src_name_ext;
  1495. }
  1496. }
  1497. $cpt = 1;
  1498. while (@file_exists($this->file_dst_pathname)) {
  1499. $this->file_dst_name_body = $body . '_' . $cpt . $ext;
  1500. $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  1501. $cpt++;
  1502. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  1503. }
  1504. if ($cpt>1) $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;auto_rename to ' . $this->file_dst_name . '<br />';
  1505. }
  1506.  
  1507. $this->log .= '- destination file details<br />';
  1508. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name : ' . $this->file_dst_name . '<br />';
  1509. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_pathname : ' . $this->file_dst_pathname . '<br />';
  1510.  
  1511. if ($this->file_overwrite) {
  1512. $this->log .= '- no overwrite checking<br />';
  1513. } else {
  1514. if (@file_exists($this->file_dst_pathname)) {
  1515. $this->processed = false;
  1516. $this->error = $this->translate('already_exists', array($this->file_dst_name));
  1517. } else {
  1518. $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />';
  1519. }
  1520. }
  1521. }
  1522. }
  1523.  
  1524. if ($this->processed) {
  1525. // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists
  1526. if (!empty($this->file_src_temp)) {
  1527. $this->log .= '- use the temp file instead of the original file since it is a second process<br />';
  1528. $this->file_src_pathname = $this->file_src_temp;
  1529. if (!file_exists($this->file_src_pathname)) {
  1530. $this->processed = false;
  1531. $this->error = $this->translate('temp_file_missing');
  1532. }
  1533. // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file()
  1534. } else if (!$this->no_upload_check) {
  1535. if (!is_uploaded_file($this->file_src_pathname)) {
  1536. $this->processed = false;
  1537. $this->error = $this->translate('source_missing');
  1538. }
  1539. // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists()
  1540. } else {
  1541. if (!file_exists($this->file_src_pathname)) {
  1542. $this->processed = false;
  1543. $this->error = $this->translate('source_missing');
  1544. }
  1545. }
  1546.  
  1547. // checks if the destination directory exists, and attempt to create it
  1548. if (!$return_mode) {
  1549. if ($this->processed && !file_exists($this->file_dst_path)) {
  1550. if ($this->dir_auto_create) {
  1551. $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:';
  1552. if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) {
  1553. $this->log .= ' failed<br />';
  1554. $this->processed = false;
  1555. $this->error = $this->translate('destination_dir');
  1556. } else {
  1557. $this->log .= ' success<br />';
  1558. }
  1559. } else {
  1560. $this->error = $this->translate('destination_dir_missing');
  1561. }
  1562. }
  1563.  
  1564. if ($this->processed && !is_dir($this->file_dst_path)) {
  1565. $this->processed = false;
  1566. $this->error = $this->translate('destination_path_not_dir');
  1567. }
  1568.  
  1569. // checks if the destination directory is writeable, and attempt to make it writeable
  1570. $hash = md5($this->file_dst_name_body . rand(1, 1000));
  1571. if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''), 'a+'))) {
  1572. if ($this->dir_auto_chmod) {
  1573. $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:';
  1574. if (!@chmod($this->file_dst_path, $this->dir_chmod)) {
  1575. $this->log .= ' failed<br />';
  1576. $this->processed = false;
  1577. $this->error = $this->translate('destination_dir_write');
  1578. } else {
  1579. $this->log .= ' success<br />';
  1580. if (!($f = @fopen($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''), 'a+'))) { // we re-check
  1581. $this->processed = false;
  1582. $this->error = $this->translate('destination_dir_write');
  1583. } else {
  1584. @fclose($f);
  1585. }
  1586. }
  1587. } else {
  1588. $this->processed = false;
  1589. $this->error = $this->translate('destination_path_write');
  1590. }
  1591. } else {
  1592. if ($this->processed) @fclose($f);
  1593. @unlink($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''));
  1594. }
  1595.  
  1596.  
  1597. // 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)
  1598. // then we create a temp file that will be used as the source file in subsequent processes
  1599. // 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)
  1600. if (!$this->no_upload_check && empty($this->file_src_temp) && !@file_exists($this->file_src_pathname)) {
  1601. $this->log .= '- attempting to use a temp file:';
  1602. $hash = md5($this->file_dst_name_body . rand(1, 1000));
  1603. if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''))) {
  1604. $this->file_src_pathname = $this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  1605. $this->file_src_temp = $this->file_src_pathname;
  1606. $this->log .= ' file created<br />';
  1607. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;temp file is: ' . $this->file_src_temp . '<br />';
  1608. } else {
  1609. $this->log .= ' failed<br />';
  1610. $this->processed = false;
  1611. $this->error = $this->translate('temp_file');
  1612. }
  1613. }
  1614. }
  1615. }
  1616.  
  1617. if ($this->processed) {
  1618.  
  1619. // we do a quick check to ensure the file is really an image
  1620. // we can do this only now, as it would have failed before in case of open_basedir
  1621. if ($image_manipulation && !@getimagesize($this->file_src_pathname)) {
  1622. $this->log .= '- the file is not an image!<br />';
  1623. $image_manipulation = false;
  1624. }
  1625.  
  1626. if ($image_manipulation) {
  1627.  
  1628. // checks if the source file is readable
  1629. if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) {
  1630. $this->processed = false;
  1631. $this->error = $this->translate('source_not_readable');
  1632. } else {
  1633. @fclose($f);
  1634. }
  1635.  
  1636. // we now do all the image manipulations
  1637. $this->log .= '- image resizing or conversion wanted<br />';
  1638. if ($this->gdversion()) {
  1639. switch($this->image_src_type) {
  1640. case 'jpg':
  1641. if (!function_exists('imagecreatefromjpeg')) {
  1642. $this->processed = false;
  1643. $this->error = $this->translate('no_create_support', array('JPEG'));
  1644. } else {
  1645. $image_src = @imagecreatefromjpeg($this->file_src_pathname);
  1646. if (!$image_src) {
  1647. $this->processed = false;
  1648. $this->error = $this->translate('create_error', array('JPEG'));
  1649. } else {
  1650. $this->log .= '- source image is JPEG<br />';
  1651. }
  1652. }
  1653. break;
  1654. case 'png':
  1655. if (!function_exists('imagecreatefrompng')) {
  1656. $this->processed = false;
  1657. $this->error = $this->translate('no_create_support', array('PNG'));
  1658. } else {
  1659. $image_src = @imagecreatefrompng($this->file_src_pathname);
  1660. if (!$image_src) {
  1661. $this->processed = false;
  1662. $this->error = $this->translate('create_error', array('PNG'));
  1663. } else {
  1664. $this->log .= '- source image is PNG<br />';
  1665. }
  1666. }
  1667. break;
  1668. case 'gif':
  1669. if (!function_exists('imagecreatefromgif')) {
  1670. $this->processed = false;
  1671. $this->error = $this->translate('no_create_support', array('GIF'));
  1672. } else {
  1673. $image_src = @imagecreatefromgif($this->file_src_pathname);
  1674. if (!$image_src) {
  1675. $this->processed = false;
  1676. $this->error = $this->translate('create_error', array('GIF'));
  1677. } else {
  1678. $this->log .= '- source image is GIF<br />';
  1679. }
  1680. }
  1681. break;
  1682. case 'bmp':
  1683. if (!method_exists($this, 'imagecreatefrombmp')) {
  1684. $this->processed = false;
  1685. $this->error = $this->translate('no_create_support', array('BMP'));
  1686. } else {
  1687. $image_src = @$this->imagecreatefrombmp($this->file_src_pathname);
  1688. if (!$image_src) {
  1689. $this->processed = false;
  1690. $this->error = $this->translate('create_error', array('BMP'));
  1691. } else {
  1692. $this->log .= '- source image is BMP<br />';
  1693. }
  1694. }
  1695. break;
  1696. default:
  1697. $this->processed = false;
  1698. $this->error = $this->translate('source_invalid');
  1699. }
  1700. } else {
  1701. $this->processed = false;
  1702. $this->error = $this->translate('gd_missing');
  1703. }
  1704.  
  1705. if ($this->processed && $image_src) {
  1706.  
  1707. // we have to set image_convert if it is not already
  1708. if (empty($this->image_convert)) {
  1709. $this->log .= '- setting destination file type to ' . $this->image_src_type . '<br />';
  1710. $this->image_convert = $this->image_src_type;
  1711. }
  1712.  
  1713. if (!in_array($this->image_convert, $this->image_supported)) {
  1714. $this->image_convert = 'jpg';
  1715. }
  1716.  
  1717. // we set the default color to be the background color if we don't output in a transparent format
  1718. 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;
  1719. if (!empty($this->image_background_color)) $this->image_default_color = $this->image_background_color;
  1720. if (empty($this->image_default_color)) $this->image_default_color = '#FFFFFF';
  1721.  
  1722. $this->image_src_x = imagesx($image_src);
  1723. $this->image_src_y = imagesy($image_src);
  1724. $gd_version = $this->gdversion();
  1725. $ratio_crop = null;
  1726.  
  1727. if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif'
  1728. $this->log .= '- image is detected as having a palette<br />';
  1729. $this->image_is_palette = true;
  1730. $this->image_transparent_color = imagecolortransparent($image_src);
  1731. if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) {
  1732. $this->image_is_transparent = true;
  1733. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;palette image is detected as transparent<br />';
  1734. }
  1735. // if the image has a palette (GIF), we convert it to true color, preserving transparency
  1736. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;convert palette image to true color<br />';
  1737. $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y);
  1738. imagealphablending($true_color, false);
  1739. imagesavealpha($true_color, true);
  1740. for ($x = 0; $x < $this->image_src_x; $x++) {
  1741. for ($y = 0; $y < $this->image_src_y; $y++) {
  1742. if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) {
  1743. imagesetpixel($true_color, $x, $y, 127 << 24);
  1744. } else {
  1745. $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y));
  1746. imagesetpixel($true_color, $x, $y, ($rgb['alpha'] << 24) | ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue']);
  1747. }
  1748. }
  1749. }
  1750. $image_src = $this->imagetransfer($true_color, $image_src);
  1751. imagealphablending($image_src, false);
  1752. imagesavealpha($image_src, true);
  1753. $this->image_is_palette = false;
  1754. }
  1755.  
  1756.  
  1757. $image_dst = & $image_src;
  1758.  
  1759. // pre-crop image, before resizing
  1760. if ((!empty($this->image_precrop))) {
  1761. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_precrop, $this->image_src_x, $this->image_src_y, true, true);
  1762. $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
  1763. $this->image_src_x = $this->image_src_x - $cl - $cr;
  1764. $this->image_src_y = $this->image_src_y - $ct - $cb;
  1765. if ($this->image_src_x < 1) $this->image_src_x = 1;
  1766. if ($this->image_src_y < 1) $this->image_src_y = 1;
  1767. $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y);
  1768.  
  1769. // we copy the image into the recieving image
  1770. imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y);
  1771.  
  1772. // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  1773. if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0 ) {
  1774. // use the background color if present
  1775. if (!empty($this->image_background_color)) {
  1776. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  1777. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  1778. } else {
  1779. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  1780. }
  1781. // fills eventual negative margins
  1782. if ($ct < 0) imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill);
  1783. if ($cr < 0) imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill);
  1784. if ($cb < 0) imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill);
  1785. if ($cl < 0) imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill);
  1786. }
  1787.  
  1788. // we transfert tmp into image_dst
  1789. $image_dst = $this->imagetransfer($tmp, $image_dst);
  1790. }
  1791.  
  1792. // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y)
  1793. if ($this->image_resize) {
  1794. $this->log .= '- resizing...<br />';
  1795.  
  1796. if ($this->image_ratio_x) {
  1797. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x size<br />';
  1798. $this->image_dst_x = round(($this->image_src_x * $this->image_y) / $this->image_src_y);
  1799. $this->image_dst_y = $this->image_y;
  1800. } else if ($this->image_ratio_y) {
  1801. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate y size<br />';
  1802. $this->image_dst_x = $this->image_x;
  1803. $this->image_dst_y = round(($this->image_src_y * $this->image_x) / $this->image_src_x);
  1804. } else if (is_numeric($this->image_ratio_pixels)) {
  1805. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x/y size to match a number of pixels<br />';
  1806. $pixels = $this->image_src_y * $this->image_src_x;
  1807. $diff = sqrt($this->image_ratio_pixels / $pixels);
  1808. $this->image_dst_x = round($this->image_src_x * $diff);
  1809. $this->image_dst_y = round($this->image_src_y * $diff);
  1810. } 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) {
  1811. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;check x/y sizes<br />';
  1812. if ((!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out)
  1813. || ($this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y))
  1814. || ($this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y)) {
  1815. $this->image_dst_x = $this->image_x;
  1816. $this->image_dst_y = $this->image_y;
  1817. if ($this->image_ratio_crop) {
  1818. if (!is_string($this->image_ratio_crop)) $this->image_ratio_crop = '';
  1819. $this->image_ratio_crop = strtolower($this->image_ratio_crop);
  1820. if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) {
  1821. $this->image_dst_y = $this->image_y;
  1822. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  1823. $ratio_crop = array();
  1824. $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
  1825. if (strpos($this->image_ratio_crop, 'l') !== false) {
  1826. $ratio_crop['l'] = 0;
  1827. $ratio_crop['r'] = $ratio_crop['x'];
  1828. } else if (strpos($this->image_ratio_crop, 'r') !== false) {
  1829. $ratio_crop['l'] = $ratio_crop['x'];
  1830. $ratio_crop['r'] = 0;
  1831. } else {
  1832. $ratio_crop['l'] = round($ratio_crop['x']/2);
  1833. $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
  1834. }
  1835. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
  1836. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  1837. } else {
  1838. $this->image_dst_x = $this->image_x;
  1839. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  1840. $ratio_crop = array();
  1841. $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
  1842. if (strpos($this->image_ratio_crop, 't') !== false) {
  1843. $ratio_crop['t'] = 0;
  1844. $ratio_crop['b'] = $ratio_crop['y'];
  1845. } else if (strpos($this->image_ratio_crop, 'b') !== false) {
  1846. $ratio_crop['t'] = $ratio_crop['y'];
  1847. $ratio_crop['b'] = 0;
  1848. } else {
  1849. $ratio_crop['t'] = round($ratio_crop['y']/2);
  1850. $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
  1851. }
  1852. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
  1853. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  1854. }
  1855. } else if ($this->image_ratio_fill) {
  1856. if (!is_string($this->image_ratio_fill)) $this->image_ratio_fill = '';
  1857. $this->image_ratio_fill = strtolower($this->image_ratio_fill);
  1858. if (($this->image_src_x/$this->image_x) < ($this->image_src_y/$this->image_y)) {
  1859. $this->image_dst_y = $this->image_y;
  1860. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  1861. $ratio_crop = array();
  1862. $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
  1863. if (strpos($this->image_ratio_fill, 'l') !== false) {
  1864. $ratio_crop['l'] = 0;
  1865. $ratio_crop['r'] = $ratio_crop['x'];
  1866. } else if (strpos($this->image_ratio_fill, 'r') !== false) {
  1867. $ratio_crop['l'] = $ratio_crop['x'];
  1868. $ratio_crop['r'] = 0;
  1869. } else {
  1870. $ratio_crop['l'] = round($ratio_crop['x']/2);
  1871. $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
  1872. }
  1873. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
  1874. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  1875. } else {
  1876. $this->image_dst_x = $this->image_x;
  1877. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  1878. $ratio_crop = array();
  1879. $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
  1880. if (strpos($this->image_ratio_fill, 't') !== false) {
  1881. $ratio_crop['t'] = 0;
  1882. $ratio_crop['b'] = $ratio_crop['y'];
  1883. } else if (strpos($this->image_ratio_fill, 'b') !== false) {
  1884. $ratio_crop['t'] = $ratio_crop['y'];
  1885. $ratio_crop['b'] = 0;
  1886. } else {
  1887. $ratio_crop['t'] = round($ratio_crop['y']/2);
  1888. $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
  1889. }
  1890. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
  1891. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  1892. }
  1893. } else {
  1894. if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) {
  1895. $this->image_dst_x = $this->image_x;
  1896. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  1897. } else {
  1898. $this->image_dst_y = $this->image_y;
  1899. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  1900. }
  1901. }
  1902. } else {
  1903. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;doesn\'t calculate x/y sizes<br />';
  1904. $this->image_dst_x = $this->image_src_x;
  1905. $this->image_dst_y = $this->image_src_y;
  1906. }
  1907. } else {
  1908. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;use plain sizes<br />';
  1909. $this->image_dst_x = $this->image_x;
  1910. $this->image_dst_y = $this->image_y;
  1911. }
  1912.  
  1913. if ($this->image_dst_x < 1) $this->image_dst_x = 1;
  1914. if ($this->image_dst_y < 1) $this->image_dst_y = 1;
  1915. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  1916.  
  1917. if ($gd_version >= 2) {
  1918. $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);
  1919. } else {
  1920. $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);
  1921. }
  1922.  
  1923. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;resized image object created<br />';
  1924. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />';
  1925. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />';
  1926. // we transfert tmp into image_dst
  1927. $image_dst = $this->imagetransfer($tmp, $image_dst);
  1928.  
  1929. } else {
  1930. $this->image_dst_x = $this->image_src_x;
  1931. $this->image_dst_y = $this->image_src_y;
  1932. }
  1933.  
  1934. // crop image (and also crops if image_ratio_crop is used)
  1935. if ((!empty($this->image_crop) || !is_null($ratio_crop))) {
  1936. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_crop, $this->image_dst_x, $this->image_dst_y, true, true);
  1937. // we adjust the cropping if we use image_ratio_crop
  1938. if (!is_null($ratio_crop)) {
  1939. if (array_key_exists('t', $ratio_crop)) $ct += $ratio_crop['t'];
  1940. if (array_key_exists('r', $ratio_crop)) $cr += $ratio_crop['r'];
  1941. if (array_key_exists('b', $ratio_crop)) $cb += $ratio_crop['b'];
  1942. if (array_key_exists('l', $ratio_crop)) $cl += $ratio_crop['l'];
  1943. }
  1944. $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
  1945. $this->image_dst_x = $this->image_dst_x - $cl - $cr;
  1946. $this->image_dst_y = $this->image_dst_y - $ct - $cb;
  1947. if ($this->image_dst_x < 1) $this->image_dst_x = 1;
  1948. if ($this->image_dst_y < 1) $this->image_dst_y = 1;
  1949. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  1950.  
  1951. // we copy the image into the recieving image
  1952. imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y);
  1953.  
  1954. // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  1955. if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0 ) {
  1956. // use the background color if present
  1957. if (!empty($this->image_background_color)) {
  1958. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  1959. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  1960. } else {
  1961. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  1962. }
  1963. // fills eventual negative margins
  1964. if ($ct < 0) imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct-1, $fill);
  1965. if ($cr < 0) imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill);
  1966. if ($cb < 0) imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill);
  1967. if ($cl < 0) imagefilledrectangle($tmp, 0, 0, -$cl-1, $this->image_dst_y, $fill);
  1968. }
  1969.  
  1970. // we transfert tmp into image_dst
  1971. $image_dst = $this->imagetransfer($tmp, $image_dst);
  1972. }
  1973.  
  1974. // flip image
  1975. if ($gd_version >= 2 && !empty($this->image_flip)) {
  1976. $this->image_flip = strtolower($this->image_flip);
  1977. $this->log .= '- flip image : ' . $this->image_flip . '<br />';
  1978. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  1979. for ($x = 0; $x < $this->image_dst_x; $x++) {
  1980. for ($y = 0; $y < $this->image_dst_y; $y++){
  1981. if (strpos($this->image_flip, 'v') !== false) {
  1982. imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1);
  1983. } else {
  1984. imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1);
  1985. }
  1986. }
  1987. }
  1988. // we transfert tmp into image_dst
  1989. $image_dst = $this->imagetransfer($tmp, $image_dst);
  1990. }
  1991.  
  1992. // rotate image
  1993. if ($gd_version >= 2 && is_numeric($this->image_rotate)) {
  1994. if (!in_array($this->image_rotate, array(0, 90, 180, 270))) $this->image_rotate = 0;
  1995. if ($this->image_rotate != 0) {
  1996. if ($this->image_rotate == 90 || $this->image_rotate == 270) {
  1997. $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x);
  1998. } else {
  1999. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  2000. }
  2001. $this->log .= '- rotate image : ' . $this->image_rotate . '<br />';
  2002. for ($x = 0; $x < $this->image_dst_x; $x++) {
  2003. for ($y = 0; $y < $this->image_dst_y; $y++){
  2004. if ($this->image_rotate == 90) {
  2005. imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1);
  2006. } else if ($this->image_rotate == 180) {
  2007. imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1);
  2008. } else if ($this->image_rotate == 270) {
  2009. imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1);
  2010. } else {
  2011. imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1);
  2012. }
  2013. }
  2014. }
  2015. if ($this->image_rotate == 90 || $this->image_rotate == 270) {
  2016. $t = $this->image_dst_y;
  2017. $this->image_dst_y = $this->image_dst_x;
  2018. $this->image_dst_x = $t;
  2019. }
  2020. // we transfert tmp into image_dst
  2021. $image_dst = $this->imagetransfer($tmp, $image_dst);
  2022. }
  2023. }
  2024.  
  2025. // unsharp mask
  2026. if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) {
  2027. // Unsharp Mask for PHP - version 2.1.1
  2028. // Unsharp mask algorithm by Torstein Hønsi 2003-07.
  2029. // Used with permission
  2030. // Modified to support alpha transparency
  2031. if ($this->image_unsharp_amount > 500) $this->image_unsharp_amount = 500;
  2032. $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016;
  2033. if ($this->image_unsharp_radius > 50) $this->image_unsharp_radius = 50;
  2034. $this->image_unsharp_radius = $this->image_unsharp_radius * 2;
  2035. if ($this->image_unsharp_threshold > 255) $this->image_unsharp_threshold = 255;
  2036. $this->image_unsharp_radius = abs(round($this->image_unsharp_radius));
  2037. if ($this->image_unsharp_radius != 0) {
  2038. $this->image_dst_x = imagesx($image_dst); $this->image_dst_y = imagesy($image_dst);
  2039. $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
  2040. $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
  2041. if (function_exists('imageconvolution')) { // PHP >= 5.1
  2042. $matrix = array(array( 1, 2, 1 ), array( 2, 4, 2 ), array( 1, 2, 1 ));
  2043. imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  2044. imageconvolution($blur, $matrix, 16, 0);
  2045. } else {
  2046. for ($i = 0; $i < $this->image_unsharp_radius; $i++) {
  2047. imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y); // left
  2048. $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // right
  2049. $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // center
  2050. imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  2051. $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333 ); // up
  2052. $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25); // down
  2053. }
  2054. }
  2055. $p_new = array();
  2056. if($this->image_unsharp_threshold>0) {
  2057. for ($x = 0; $x < $this->image_dst_x-1; $x++) {
  2058. for ($y = 0; $y < $this->image_dst_y; $y++) {
  2059. $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2060. $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
  2061. $p_new['red'] = (abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red'])) + $p_orig['red'])) : $p_orig['red'];
  2062. $p_new['green'] = (abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green'])) + $p_orig['green'])) : $p_orig['green'];
  2063. $p_new['blue'] = (abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue'])) + $p_orig['blue'])) : $p_orig['blue'];
  2064. if (($p_orig['red'] != $p_new['red']) || ($p_orig['green'] != $p_new['green']) || ($p_orig['blue'] != $p_new['blue'])) {
  2065. $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
  2066. imagesetpixel($image_dst, $x, $y, $color);
  2067. }
  2068. }
  2069. }
  2070. } else {
  2071. for ($x = 0; $x < $this->image_dst_x; $x++) {
  2072. for ($y = 0; $y < $this->image_dst_y; $y++) {
  2073. $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2074. $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
  2075. $p_new['red'] = ($this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red'])) + $p_orig['red'];
  2076. if ($p_new['red']>255) { $p_new['red']=255; } elseif ($p_new['red']<0) { $p_new['red']=0; }
  2077. $p_new['green'] = ($this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green'])) + $p_orig['green'];
  2078. if ($p_new['green']>255) { $p_new['green']=255; } elseif ($p_new['green']<0) { $p_new['green']=0; }
  2079. $p_new['blue'] = ($this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue'])) + $p_orig['blue'];
  2080. if ($p_new['blue']>255) { $p_new['blue']=255; } elseif ($p_new['blue']<0) { $p_new['blue']=0; }
  2081. $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
  2082. imagesetpixel($image_dst, $x, $y, $color);
  2083. }
  2084. }
  2085. }
  2086. imagedestroy($canvas);
  2087. imagedestroy($blur);
  2088. }
  2089. }
  2090.  
  2091. // add color overlay
  2092. if ($gd_version >= 2 && (is_numeric($this->image_overlay_opacity) && $this->image_overlay_opacity > 0 && !empty($this->image_overlay_color))) {
  2093. $this->log .= '- apply color overlay<br />';
  2094. list($red, $green, $blue) = $this->getcolors($this->image_overlay_color);
  2095. $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y);
  2096. $color = imagecolorallocate($filter, $red, $green, $blue);
  2097. imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color);
  2098. $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_opacity);
  2099. imagedestroy($filter);
  2100. }
  2101.  
  2102. // add brightness, contrast and tint, turns to greyscale and inverts colors
  2103. 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))) {
  2104. $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />';
  2105. if (!empty($this->image_tint_color)) list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color);
  2106. //imagealphablending($image_dst, true);
  2107. for($y=0; $y < $this->image_dst_y; $y++) {
  2108. for($x=0; $x < $this->image_dst_x; $x++) {
  2109. if ($this->image_greyscale) {
  2110. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2111. $r = $g = $b = round((0.2125 * $pixel['red']) + (0.7154 * $pixel['green']) + (0.0721 * $pixel['blue']));
  2112. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  2113. imagesetpixel($image_dst, $x, $y, $color);
  2114. unset($color); unset($pixel);
  2115. }
  2116. if (is_numeric($this->image_threshold)) {
  2117. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2118. $c = (round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3) - 127;
  2119. $r = $g = $b = ($c > $this->image_threshold ? 255 : 0);
  2120. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  2121. imagesetpixel($image_dst, $x, $y, $color);
  2122. unset($color); unset($pixel);
  2123. }
  2124. if (is_numeric($this->image_brightness)) {
  2125. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2126. $r = max(min(round($pixel['red'] + (($this->image_brightness * 2))), 255), 0);
  2127. $g = max(min(round($pixel['green'] + (($this->image_brightness * 2))), 255), 0);
  2128. $b = max(min(round($pixel['blue'] + (($this->image_brightness * 2))), 255), 0);
  2129. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  2130. imagesetpixel($image_dst, $x, $y, $color);
  2131. unset($color); unset($pixel);
  2132. }
  2133. if (is_numeric($this->image_contrast)) {
  2134. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2135. $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0);
  2136. $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0);
  2137. $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0);
  2138. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  2139. imagesetpixel($image_dst, $x, $y, $color);
  2140. unset($color); unset($pixel);
  2141. }
  2142. if (!empty($this->image_tint_color)) {
  2143. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2144. $r = min(round($tint_red * $pixel['red'] / 169), 255);
  2145. $g = min(round($tint_green * $pixel['green'] / 169), 255);
  2146. $b = min(round($tint_blue * $pixel['blue'] / 169), 255);
  2147. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  2148. imagesetpixel($image_dst, $x, $y, $color);
  2149. unset($color); unset($pixel);
  2150. }
  2151. if (!empty($this->image_negative)) {
  2152. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2153. $r = round(255 - $pixel['red']);
  2154. $g = round(255 - $pixel['green']);
  2155. $b = round(255 - $pixel['blue']);
  2156. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  2157. imagesetpixel($image_dst, $x, $y, $color);
  2158. unset($color); unset($pixel);
  2159. }
  2160. }
  2161. }
  2162. }
  2163.  
  2164. // adds a border
  2165. if ($gd_version >= 2 && !empty($this->image_border)) {
  2166. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_border, $this->image_dst_x, $this->image_dst_y, true, false);
  2167. $this->log .= '- add border : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . '<br />';
  2168. $this->image_dst_x = $this->image_dst_x + $cl + $cr;
  2169. $this->image_dst_y = $this->image_dst_y + $ct + $cb;
  2170. if (!empty($this->image_border_color)) list($red, $green, $blue) = $this->getcolors($this->image_border_color);
  2171. $opacity = (is_numeric($this->image_border_opacity) ? (int) (127 - $this->image_border_opacity / 100 * 127): 0);
  2172. // we now create an image, that we fill with the border color
  2173. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  2174. $background = imagecolorallocatealpha($tmp, $red, $green, $blue, $opacity);
  2175. imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background);
  2176. // we then copy the source image into the new image, without merging so that only the border is actually kept
  2177. imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct);
  2178. // we transfert tmp into image_dst
  2179. $image_dst = $this->imagetransfer($tmp, $image_dst);
  2180. }
  2181.  
  2182. // adds a fading-to-transparent border
  2183. if ($gd_version >= 2 && !empty($this->image_border_transparent)) {
  2184. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_border_transparent, $this->image_dst_x, $this->image_dst_y, true, false);
  2185. $this->log .= '- add transparent border : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . '<br />';
  2186. // we now create an image, that we fill with the border color
  2187. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  2188. // we then copy the source image into the new image, without the borders
  2189. imagecopy($tmp, $image_dst, $cl, $ct, $cl, $ct, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct);
  2190. // we now add the top border
  2191. $opacity = 100;
  2192. for ($y = $ct - 1; $y >= 0; $y--) {
  2193. $il = (int) ($ct > 0 ? ($cl * ($y / $ct)) : 0);
  2194. $ir = (int) ($ct > 0 ? ($cr * ($y / $ct)) : 0);
  2195. for ($x = $il; $x < $this->image_dst_x - $ir; $x++) {
  2196. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2197. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  2198. if ($alpha > 0) {
  2199. if ($alpha > 1) $alpha = 1;
  2200. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  2201. imagesetpixel($tmp, $x, $y, $color);
  2202. }
  2203. }
  2204. if ($opacity > 0) $opacity = $opacity - (100 / $ct);
  2205. }
  2206. // we now add the right border
  2207. $opacity = 100;
  2208. for ($x = $this->image_dst_x - $cr; $x < $this->image_dst_x; $x++) {
  2209. $it = (int) ($cr > 0 ? ($ct * (($this->image_dst_x - $x - 1) / $cr)) : 0);
  2210. $ib = (int) ($cr > 0 ? ($cb * (($this->image_dst_x - $x - 1) / $cr)) : 0);
  2211. for ($y = $it; $y < $this->image_dst_y - $ib; $y++) {
  2212. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2213. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  2214. if ($alpha > 0) {
  2215. if ($alpha > 1) $alpha = 1;
  2216. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  2217. imagesetpixel($tmp, $x, $y, $color);
  2218. }
  2219. }
  2220. if ($opacity > 0) $opacity = $opacity - (100 / $cr);
  2221. }
  2222. // we now add the bottom border
  2223. $opacity = 100;
  2224. for ($y = $this->image_dst_y - $cb; $y < $this->image_dst_y; $y++) {
  2225. $il = (int) ($cb > 0 ? ($cl * (($this->image_dst_y - $y - 1) / $cb)) : 0);
  2226. $ir = (int) ($cb > 0 ? ($cr * (($this->image_dst_y - $y - 1) / $cb)) : 0);
  2227. for ($x = $il; $x < $this->image_dst_x - $ir; $x++) {
  2228. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2229. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  2230. if ($alpha > 0) {
  2231. if ($alpha > 1) $alpha = 1;
  2232. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  2233. imagesetpixel($tmp, $x, $y, $color);
  2234. }
  2235. }
  2236. if ($opacity > 0) $opacity = $opacity - (100 / $cb);
  2237. }
  2238. // we now add the left border
  2239. $opacity = 100;
  2240. for ($x = $cl - 1; $x >= 0; $x--) {
  2241. $it = (int) ($cl > 0 ? ($ct * ($x / $cl)) : 0);
  2242. $ib = (int) ($cl > 0 ? ($cb * ($x / $cl)) : 0);
  2243. for ($y = $it; $y < $this->image_dst_y - $ib; $y++) {
  2244. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2245. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  2246. if ($alpha > 0) {
  2247. if ($alpha > 1) $alpha = 1;
  2248. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  2249. imagesetpixel($tmp, $x, $y, $color);
  2250. }
  2251. }
  2252. if ($opacity > 0) $opacity = $opacity - (100 / $cl);
  2253. }
  2254. // we transfert tmp into image_dst
  2255. $image_dst = $this->imagetransfer($tmp, $image_dst);
  2256. }
  2257.  
  2258. // add frame border
  2259. if ($gd_version >= 2 && is_numeric($this->image_frame)) {
  2260. if (is_array($this->image_frame_colors)) {
  2261. $vars = $this->image_frame_colors;
  2262. $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />';
  2263. } else {
  2264. $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />';
  2265. $vars = explode(' ', $this->image_frame_colors);
  2266. }
  2267. $nb = sizeof($vars);
  2268. $this->image_dst_x = $this->image_dst_x + ($nb * 2);
  2269. $this->image_dst_y = $this->image_dst_y + ($nb * 2);
  2270. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  2271. imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - ($nb * 2), $this->image_dst_y - ($nb * 2));
  2272. $opacity = (is_numeric($this->image_frame_opacity) ? (int) (127 - $this->image_frame_opacity / 100 * 127): 0);
  2273. for ($i=0; $i<$nb; $i++) {
  2274. list($red, $green, $blue) = $this->getcolors($vars[$i]);
  2275. $c = imagecolorallocatealpha($tmp, $red, $green, $blue, $opacity);
  2276. if ($this->image_frame == 1) {
  2277. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c);
  2278. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $this->image_dst_x - $i -1, $i, $c);
  2279. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c);
  2280. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c);
  2281. } else {
  2282. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c);
  2283. imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c);
  2284. imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c);
  2285. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c);
  2286. }
  2287. }
  2288. // we transfert tmp into image_dst
  2289. $image_dst = $this->imagetransfer($tmp, $image_dst);
  2290. }
  2291.  
  2292. // add bevel border
  2293. if ($gd_version >= 2 && $this->image_bevel > 0) {
  2294. if (empty($this->image_bevel_color1)) $this->image_bevel_color1 = '#FFFFFF';
  2295. if (empty($this->image_bevel_color2)) $this->image_bevel_color2 = '#000000';
  2296. list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1);
  2297. list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2);
  2298. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  2299. imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  2300. imagealphablending($tmp, true);
  2301. for ($i=0; $i<$this->image_bevel; $i++) {
  2302. $alpha = round(($i / $this->image_bevel) * 127);
  2303. $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha);
  2304. $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha);
  2305. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c1);
  2306. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i, $this->image_dst_x - $i -1, $i, $c2);
  2307. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c2);
  2308. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c1);
  2309. }
  2310. // we transfert tmp into image_dst
  2311. $image_dst = $this->imagetransfer($tmp, $image_dst);
  2312. }
  2313.  
  2314. // add watermark image
  2315. if ($this->image_watermark!='' && file_exists($this->image_watermark)) {
  2316. $this->log .= '- add watermark<br />';
  2317. $this->image_watermark_position = strtolower($this->image_watermark_position);
  2318. $watermark_info = getimagesize($this->image_watermark);
  2319. $watermark_type = (array_key_exists(2, $watermark_info) ? $watermark_info[2] : null); // 1 = GIF, 2 = JPG, 3 = PNG
  2320. $watermark_checked = false;
  2321. if ($watermark_type == IMAGETYPE_GIF) {
  2322. if (!function_exists('imagecreatefromgif')) {
  2323. $this->error = $this->translate('watermark_no_create_support', array('GIF'));
  2324. } else {
  2325. $filter = @imagecreatefromgif($this->image_watermark);
  2326. if (!$filter) {
  2327. $this->error = $this->translate('watermark_create_error', array('GIF'));
  2328. } else {
  2329. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is GIF<br />';
  2330. $watermark_checked = true;
  2331. }
  2332. }
  2333. } else if ($watermark_type == IMAGETYPE_JPEG) {
  2334. if (!function_exists('imagecreatefromjpeg')) {
  2335. $this->error = $this->translate('watermark_no_create_support', array('JPEG'));
  2336. } else {
  2337. $filter = @imagecreatefromjpeg($this->image_watermark);
  2338. if (!$filter) {
  2339. $this->error = $this->translate('watermark_create_error', array('JPEG'));
  2340. } else {
  2341. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is JPEG<br />';
  2342. $watermark_checked = true;
  2343. }
  2344. }
  2345. } else if ($watermark_type == IMAGETYPE_PNG) {
  2346. if (!function_exists('imagecreatefrompng')) {
  2347. $this->error = $this->translate('watermark_no_create_support', array('PNG'));
  2348. } else {
  2349. $filter = @imagecreatefrompng($this->image_watermark);
  2350. if (!$filter) {
  2351. $this->error = $this->translate('watermark_create_error', array('PNG'));
  2352. } else {
  2353. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is PNG<br />';
  2354. $watermark_checked = true;
  2355. }
  2356. }
  2357. } else if ($watermark_type == IMAGETYPE_BMP) {
  2358. if (!method_exists($this, 'imagecreatefrombmp')) {
  2359. $this->error = $this->translate('watermark_no_create_support', array('BMP'));
  2360. } else {
  2361. $filter = @$this->imagecreatefrombmp($this->image_watermark);
  2362. if (!$filter) {
  2363. $this->error = $this->translate('watermark_create_error', array('BMP'));
  2364. } else {
  2365. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is BMP<br />';
  2366. $watermark_checked = true;
  2367. }
  2368. }
  2369. } else {
  2370. $this->error = $this->translate('watermark_invalid');
  2371. }
  2372. if ($watermark_checked) {
  2373. $watermark_dst_width = $watermark_src_width = imagesx($filter);
  2374. $watermark_dst_height = $watermark_src_height = imagesy($filter);
  2375.  
  2376. // if watermark is too large/tall, resize it first
  2377. if ((!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y))
  2378. || (!$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y)) {
  2379. $canvas_width = $this->image_dst_x - abs($this->image_watermark_x);
  2380. $canvas_height = $this->image_dst_y - abs($this->image_watermark_y);
  2381. if (($watermark_src_width/$canvas_width) > ($watermark_src_height/$canvas_height)) {
  2382. $watermark_dst_width = $canvas_width;
  2383. $watermark_dst_height = intval($watermark_src_height*($canvas_width / $watermark_src_width));
  2384. } else {
  2385. $watermark_dst_height = $canvas_height;
  2386. $watermark_dst_width = intval($watermark_src_width*($canvas_height / $watermark_src_height));
  2387. }
  2388. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark resized from '.$watermark_src_width.'x'.$watermark_src_height.' to '.$watermark_dst_width.'x'.$watermark_dst_height.'<br />';
  2389.  
  2390. }
  2391. // determine watermark position
  2392. $watermark_x = 0;
  2393. $watermark_y = 0;
  2394. if (is_numeric($this->image_watermark_x)) {
  2395. if ($this->image_watermark_x < 0) {
  2396. $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x;
  2397. } else {
  2398. $watermark_x = $this->image_watermark_x;
  2399. }
  2400. } else {
  2401. if (strpos($this->image_watermark_position, 'r') !== false) {
  2402. $watermark_x = $this->image_dst_x - $watermark_dst_width;
  2403. } else if (strpos($this->image_watermark_position, 'l') !== false) {
  2404. $watermark_x = 0;
  2405. } else {
  2406. $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2;
  2407. }
  2408. }
  2409. if (is_numeric($this->image_watermark_y)) {
  2410. if ($this->image_watermark_y < 0) {
  2411. $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y;
  2412. } else {
  2413. $watermark_y = $this->image_watermark_y;
  2414. }
  2415. } else {
  2416. if (strpos($this->image_watermark_position, 'b') !== false) {
  2417. $watermark_y = $this->image_dst_y - $watermark_dst_height;
  2418. } else if (strpos($this->image_watermark_position, 't') !== false) {
  2419. $watermark_y = 0;
  2420. } else {
  2421. $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2;
  2422. }
  2423. }
  2424. imagealphablending($image_dst, true);
  2425. imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height);
  2426. } else {
  2427. $this->error = $this->translate('watermark_invalid');
  2428. }
  2429. }
  2430.  
  2431. // add text
  2432. if (!empty($this->image_text)) {
  2433. $this->log .= '- add text<br />';
  2434.  
  2435. // calculate sizes in human readable format
  2436. $src_size = $this->file_src_size / 1024;
  2437. $src_size_mb = number_format($src_size / 1024, 1, ".", " ");
  2438. $src_size_kb = number_format($src_size, 1, ".", " ");
  2439. $src_size_human = ($src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb");
  2440.  
  2441. $this->image_text = str_replace(
  2442. array('[src_name]',
  2443. '[src_name_body]',
  2444. '[src_name_ext]',
  2445. '[src_pathname]',
  2446. '[src_mime]',
  2447. '[src_size]',
  2448. '[src_size_kb]',
  2449. '[src_size_mb]',
  2450. '[src_size_human]',
  2451. '[src_x]',
  2452. '[src_y]',
  2453. '[src_pixels]',
  2454. '[src_type]',
  2455. '[src_bits]',
  2456. '[dst_path]',
  2457. '[dst_name_body]',
  2458. '[dst_name_ext]',
  2459. '[dst_name]',
  2460. '[dst_pathname]',
  2461. '[dst_x]',
  2462. '[dst_y]',
  2463. '[date]',
  2464. '[time]',
  2465. '[host]',
  2466. '[server]',
  2467. '[ip]',
  2468. '[gd_version]'),
  2469. array($this->file_src_name,
  2470. $this->file_src_name_body,
  2471. $this->file_src_name_ext,
  2472. $this->file_src_pathname,
  2473. $this->file_src_mime,
  2474. $this->file_src_size,
  2475. $src_size_kb,
  2476. $src_size_mb,
  2477. $src_size_human,
  2478. $this->image_src_x,
  2479. $this->image_src_y,
  2480. $this->image_src_pixels,
  2481. $this->image_src_type,
  2482. $this->image_src_bits,
  2483. $this->file_dst_path,
  2484. $this->file_dst_name_body,
  2485. $this->file_dst_name_ext,
  2486. $this->file_dst_name,
  2487. $this->file_dst_pathname,
  2488. $this->image_dst_x,
  2489. $this->image_dst_y,
  2490. date('Y-m-d'),
  2491. date('H:i:s'),
  2492. (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a'),
  2493. (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a'),
  2494. (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a'),
  2495. $this->gdversion(true)),
  2496. $this->image_text);
  2497.  
  2498. if (!is_numeric($this->image_text_padding)) $this->image_text_padding = 0;
  2499. if (!is_numeric($this->image_text_line_spacing)) $this->image_text_line_spacing = 0;
  2500. if (!is_numeric($this->image_text_padding_x)) $this->image_text_padding_x = $this->image_text_padding;
  2501. if (!is_numeric($this->image_text_padding_y)) $this->image_text_padding_y = $this->image_text_padding;
  2502. $this->image_text_position = strtolower($this->image_text_position);
  2503. $this->image_text_direction = strtolower($this->image_text_direction);
  2504. $this->image_text_alignment = strtolower($this->image_text_alignment);
  2505.  
  2506. // if the font is a string, we assume that we might want to load a font
  2507. if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') {
  2508. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;try to load font ' . $this->image_text_font . '... ';
  2509. if ($this->image_text_font = @imageloadfont($this->image_text_font)) {
  2510. $this->log .= 'success<br />';
  2511. } else {
  2512. $this->log .= 'error<br />';
  2513. $this->image_text_font = 5;
  2514. }
  2515. }
  2516.  
  2517. $text = explode("\n", $this->image_text);
  2518. $char_width = imagefontwidth($this->image_text_font);
  2519. $char_height = imagefontheight($this->image_text_font);
  2520. $text_height = 0;
  2521. $text_width = 0;
  2522. $line_height = 0;
  2523. $line_width = 0;
  2524.  
  2525. foreach ($text as $k => $v) {
  2526. if ($this->image_text_direction == 'v') {
  2527. $h = ($char_width * strlen($v));
  2528. if ($h > $text_height) $text_height = $h;
  2529. $line_width = $char_height;
  2530. $text_width += $line_width + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0);
  2531. } else {
  2532. $w = ($char_width * strlen($v));
  2533. if ($w > $text_width) $text_width = $w;
  2534. $line_height = $char_height;
  2535. $text_height += $line_height + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0);
  2536. }
  2537. }
  2538. $text_width += (2 * $this->image_text_padding_x);
  2539. $text_height += (2 * $this->image_text_padding_y);
  2540. $text_x = 0;
  2541. $text_y = 0;
  2542. if (is_numeric($this->image_text_x)) {
  2543. if ($this->image_text_x < 0) {
  2544. $text_x = $this->image_dst_x - $text_width + $this->image_text_x;
  2545. } else {
  2546. $text_x = $this->image_text_x;
  2547. }
  2548. } else {
  2549. if (strpos($this->image_text_position, 'r') !== false) {
  2550. $text_x = $this->image_dst_x - $text_width;
  2551. } else if (strpos($this->image_text_position, 'l') !== false) {
  2552. $text_x = 0;
  2553. } else {
  2554. $text_x = ($this->image_dst_x - $text_width) / 2;
  2555. }
  2556. }
  2557. if (is_numeric($this->image_text_y)) {
  2558. if ($this->image_text_y < 0) {
  2559. $text_y = $this->image_dst_y - $text_height + $this->image_text_y;
  2560. } else {
  2561. $text_y = $this->image_text_y;
  2562. }
  2563. } else {
  2564. if (strpos($this->image_text_position, 'b') !== false) {
  2565. $text_y = $this->image_dst_y - $text_height;
  2566. } else if (strpos($this->image_text_position, 't') !== false) {
  2567. $text_y = 0;
  2568. } else {
  2569. $text_y = ($this->image_dst_y - $text_height) / 2;
  2570. }
  2571. }
  2572.  
  2573. // add a background, maybe transparent
  2574. if (!empty($this->image_text_background)) {
  2575. list($red, $green, $blue) = $this->getcolors($this->image_text_background);
  2576. if ($gd_version >= 2 && (is_numeric($this->image_text_background_opacity)) && $this->image_text_background_opacity >= 0 && $this->image_text_background_opacity <= 100) {
  2577. $filter = imagecreatetruecolor($text_width, $text_height);
  2578. $background_color = imagecolorallocate($filter, $red, $green, $blue);
  2579. imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color);
  2580. $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_opacity);
  2581. imagedestroy($filter);
  2582. } else {
  2583. $background_color = imagecolorallocate($image_dst ,$red, $green, $blue);
  2584. imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color);
  2585. }
  2586. }
  2587.  
  2588. $text_x += $this->image_text_padding_x;
  2589. $text_y += $this->image_text_padding_y;
  2590. $t_width = $text_width - (2 * $this->image_text_padding_x);
  2591. $t_height = $text_height - (2 * $this->image_text_padding_y);
  2592. list($red, $green, $blue) = $this->getcolors($this->image_text_color);
  2593.  
  2594. // add the text, maybe transparent
  2595. if ($gd_version >= 2 && (is_numeric($this->image_text_opacity)) && $this->image_text_opacity >= 0 && $this->image_text_opacity <= 100) {
  2596. if ($t_width < 0) $t_width = 0;
  2597. if ($t_height < 0) $t_height = 0;
  2598. $filter = $this->imagecreatenew($t_width, $t_height, false, true);
  2599. $text_color = imagecolorallocate($filter ,$red, $green, $blue);
  2600.  
  2601. foreach ($text as $k => $v) {
  2602. if ($this->image_text_direction == 'v') {
  2603. imagestringup($filter,
  2604. $this->image_text_font,
  2605. $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  2606. $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))) ,
  2607. $v,
  2608. $text_color);
  2609. } else {
  2610. imagestring($filter,
  2611. $this->image_text_font,
  2612. ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))),
  2613. $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  2614. $v,
  2615. $text_color);
  2616. }
  2617. }
  2618. $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_opacity);
  2619. imagedestroy($filter);
  2620.  
  2621. } else {
  2622. $text_color = imageColorAllocate($image_dst ,$red, $green, $blue);
  2623. foreach ($text as $k => $v) {
  2624. if ($this->image_text_direction == 'v') {
  2625. imagestringup($image_dst,
  2626. $this->image_text_font,
  2627. $text_x + $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  2628. $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))),
  2629. $v,
  2630. $text_color);
  2631. } else {
  2632. imagestring($image_dst,
  2633. $this->image_text_font,
  2634. $text_x + ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))),
  2635. $text_y + $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  2636. $v,
  2637. $text_color);
  2638. }
  2639. }
  2640. }
  2641. }
  2642.  
  2643. // add a reflection
  2644. if ($this->image_reflection_height) {
  2645. $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />';
  2646. // we decode image_reflection_height, which can be a integer, a string in pixels or percentage
  2647. $image_reflection_height = $this->image_reflection_height;
  2648. if (strpos($image_reflection_height, '%')>0) $image_reflection_height = $this->image_dst_y * (str_replace('%','',$image_reflection_height / 100));
  2649. if (strpos($image_reflection_height, 'px')>0) $image_reflection_height = str_replace('px','',$image_reflection_height);
  2650. $image_reflection_height = (int) $image_reflection_height;
  2651. if ($image_reflection_height > $this->image_dst_y) $image_reflection_height = $this->image_dst_y;
  2652. if (empty($this->image_reflection_opacity)) $this->image_reflection_opacity = 60;
  2653. // create the new destination image
  2654. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true);
  2655. $transparency = $this->image_reflection_opacity;
  2656.  
  2657. // copy the original image
  2658. 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));
  2659.  
  2660. // we have to make sure the extra bit is the right color, or transparent
  2661. if ($image_reflection_height + $this->image_reflection_space > 0) {
  2662. // use the background color if present
  2663. if (!empty($this->image_background_color)) {
  2664. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  2665. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  2666. } else {
  2667. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  2668. }
  2669. // fill in from the edge of the extra bit
  2670. imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill);
  2671. }
  2672.  
  2673. // copy the reflection
  2674. for ($y = 0; $y < $image_reflection_height; $y++) {
  2675. for ($x = 0; $x < $this->image_dst_x; $x++) {
  2676. $pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space));
  2677. $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)));
  2678. $alpha_o = 1 - ($pixel_o['alpha'] / 127);
  2679. $alpha_b = 1 - ($pixel_b['alpha'] / 127);
  2680. $opacity = $alpha_o * $transparency / 100;
  2681. if ($opacity > 0) {
  2682. $red = round((($pixel_o['red'] * $opacity) + ($pixel_b['red'] ) * $alpha_b) / ($alpha_b + $opacity));
  2683. $green = round((($pixel_o['green'] * $opacity) + ($pixel_b['green']) * $alpha_b) / ($alpha_b + $opacity));
  2684. $blue = round((($pixel_o['blue'] * $opacity) + ($pixel_b['blue'] ) * $alpha_b) / ($alpha_b + $opacity));
  2685. $alpha = ($opacity + $alpha_b);
  2686. if ($alpha > 1) $alpha = 1;
  2687. $alpha = round((1 - $alpha) * 127);
  2688. $color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha);
  2689. imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color);
  2690. }
  2691. }
  2692. if ($transparency > 0) $transparency = $transparency - ($this->image_reflection_opacity / $image_reflection_height);
  2693. }
  2694.  
  2695. // copy the resulting image into the destination image
  2696. $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space;
  2697. $image_dst = $this->imagetransfer($tmp, $image_dst);
  2698. }
  2699.  
  2700. // change opacity
  2701. if ($gd_version >= 2 && is_numeric($this->image_opacity) && $this->image_opacity < 100) {
  2702. $this->log .= '- change opacity<br />';
  2703. // create the new destination image
  2704. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, true);
  2705. for($y=0; $y < $this->image_dst_y; $y++) {
  2706. for($x=0; $x < $this->image_dst_x; $x++) {
  2707. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2708. $alpha = $pixel['alpha'] + round((127 - $pixel['alpha']) * (100 - $this->image_opacity) / 100);
  2709. if ($alpha > 127) $alpha = 127;
  2710. if ($alpha > 0) {
  2711. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], $alpha);
  2712. imagesetpixel($tmp, $x, $y, $color);
  2713. }
  2714. }
  2715. }
  2716. // copy the resulting image into the destination image
  2717. $image_dst = $this->imagetransfer($tmp, $image_dst);
  2718. }
  2719. // reduce the JPEG image to a set desired size
  2720. if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) {
  2721. // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net
  2722. $this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />';
  2723. // calculate size of each image. 75%, 50%, and 25% quality
  2724. ob_start(); imagejpeg($image_dst,'',75); $buffer = ob_get_contents(); ob_end_clean();
  2725. $size75 = strlen($buffer);
  2726. ob_start(); imagejpeg($image_dst,'',50); $buffer = ob_get_contents(); ob_end_clean();
  2727. $size50 = strlen($buffer);
  2728. ob_start(); imagejpeg($image_dst,'',25); $buffer = ob_get_contents(); ob_end_clean();
  2729. $size25 = strlen($buffer);
  2730.  
  2731. // make sure we won't divide by 0
  2732. if ($size50 == $size25) $size50++;
  2733. if ($size75 == $size50 || $size75 == $size25) $size75++;
  2734.  
  2735. // calculate gradient of size reduction by quality
  2736. $mgrad1 = 25 / ($size50-$size25);
  2737. $mgrad2 = 25 / ($size75-$size50);
  2738. $mgrad3 = 50 / ($size75-$size25);
  2739. $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3;
  2740. // result of approx. quality factor for expected size
  2741. $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50);
  2742.  
  2743. if ($q_factor<1) {
  2744. $this->jpeg_quality=1;
  2745. } elseif ($q_factor>100) {
  2746. $this->jpeg_quality=100;
  2747. } else {
  2748. $this->jpeg_quality=$q_factor;
  2749. }
  2750. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG quality factor set to ' . $this->jpeg_quality . '<br />';
  2751. }
  2752.  
  2753. // converts image from true color, and fix transparency if needed
  2754. $this->log .= '- converting...<br />';
  2755. switch($this->image_convert) {
  2756. case 'gif':
  2757. // if the image is true color, we convert it to a palette
  2758. if (imageistruecolor($image_dst)) {
  2759. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;true color to palette<br />';
  2760. // creates a black and white mask
  2761. $mask = array(array());
  2762. for ($x = 0; $x < $this->image_dst_x; $x++) {
  2763. for ($y = 0; $y < $this->image_dst_y; $y++) {
  2764. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2765. $mask[$x][$y] = $pixel['alpha'];
  2766. }
  2767. }
  2768. list($red, $green, $blue) = $this->getcolors($this->image_default_color);
  2769. // first, we merge the image with the background color, so we know which colors we will have
  2770. for ($x = 0; $x < $this->image_dst_x; $x++) {
  2771. for ($y = 0; $y < $this->image_dst_y; $y++) {
  2772. if ($mask[$x][$y] > 0){
  2773. // we have some transparency. we combine the color with the default color
  2774. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2775. $alpha = ($mask[$x][$y] / 127);
  2776. $pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
  2777. $pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
  2778. $pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
  2779. $color = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
  2780. imagesetpixel($image_dst, $x, $y, $color);
  2781. }
  2782. }
  2783. }
  2784. // transforms the true color image into palette, with its merged default color
  2785. if (empty($this->image_background_color)) {
  2786. imagetruecolortopalette($image_dst, true, 255);
  2787. $transparency = imagecolorallocate($image_dst, 254, 1, 253);
  2788. imagecolortransparent($image_dst, $transparency);
  2789. // make the transparent areas transparent
  2790. for ($x = 0; $x < $this->image_dst_x; $x++) {
  2791. for ($y = 0; $y < $this->image_dst_y; $y++) {
  2792. // we test wether we have enough opacity to justify keeping the color
  2793. if ($mask[$x][$y] > 120) imagesetpixel($image_dst, $x, $y, $transparency);
  2794. }
  2795. }
  2796. }
  2797. unset($mask);
  2798. }
  2799. break;
  2800. case 'jpg':
  2801. case 'bmp':
  2802. // if the image doesn't support any transparency, then we merge it with the default color
  2803. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;fills in transparency with default color<br />';
  2804. list($red, $green, $blue) = $this->getcolors($this->image_default_color);
  2805. $transparency = imagecolorallocate($image_dst, $red, $green, $blue);
  2806. // make the transaparent areas transparent
  2807. for ($x = 0; $x < $this->image_dst_x; $x++) {
  2808. for ($y = 0; $y < $this->image_dst_y; $y++) {
  2809. // we test wether we have some transparency, in which case we will merge the colors
  2810. if (imageistruecolor($image_dst)) {
  2811. $rgba = imagecolorat($image_dst, $x, $y);
  2812. $pixel = array('red' => ($rgba >> 16) & 0xFF,
  2813. 'green' => ($rgba >> 8) & 0xFF,
  2814. 'blue' => $rgba & 0xFF,
  2815. 'alpha' => ($rgba & 0x7F000000) >> 24);
  2816. } else {
  2817. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  2818. }
  2819. if ($pixel['alpha'] == 127) {
  2820. // we have full transparency. we make the pixel transparent
  2821. imagesetpixel($image_dst, $x, $y, $transparency);
  2822. } else if ($pixel['alpha'] > 0) {
  2823. // we have some transparency. we combine the color with the default color
  2824. $alpha = ($pixel['alpha'] / 127);
  2825. $pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
  2826. $pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
  2827. $pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
  2828. $color = imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
  2829. imagesetpixel($image_dst, $x, $y, $color);
  2830. }
  2831. }
  2832. }
  2833.  
  2834. break;
  2835. default:
  2836. break;
  2837. }
  2838.  
  2839. // outputs image
  2840. $this->log .= '- saving image...<br />';
  2841. switch($this->image_convert) {
  2842. case 'jpeg':
  2843. case 'jpg':
  2844. if (!$return_mode) {
  2845. $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality);
  2846. } else {
  2847. ob_start();
  2848. $result = @imagejpeg($image_dst, '', $this->jpeg_quality);
  2849. $return_content = ob_get_contents();
  2850. ob_end_clean();
  2851. }
  2852. if (!$result) {
  2853. $this->processed = false;
  2854. $this->error = $this->translate('file_create', array('JPEG'));
  2855. } else {
  2856. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG image created<br />';
  2857. }
  2858. break;
  2859. case 'png':
  2860. imagealphablending( $image_dst, false );
  2861. imagesavealpha( $image_dst, true );
  2862. if (!$return_mode) {
  2863. $result = @imagepng($image_dst, $this->file_dst_pathname);
  2864. } else {
  2865. ob_start();
  2866. $result = @imagepng($image_dst);
  2867. $return_content = ob_get_contents();
  2868. ob_end_clean();
  2869. }
  2870. if (!$result) {
  2871. $this->processed = false;
  2872. $this->error = $this->translate('file_create', array('PNG'));
  2873. } else {
  2874. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PNG image created<br />';
  2875. }
  2876. break;
  2877. case 'gif':
  2878. if (!$return_mode) {
  2879. $result = @imagegif($image_dst, $this->file_dst_pathname);
  2880. } else {
  2881. ob_start();
  2882. $result = @imagegif($image_dst);
  2883. $return_content = ob_get_contents();
  2884. ob_end_clean();
  2885. }
  2886. if (!$result) {
  2887. $this->processed = false;
  2888. $this->error = $this->translate('file_create', array('GIF'));
  2889. } else {
  2890. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;GIF image created<br />';
  2891. }
  2892. break;
  2893. case 'bmp':
  2894. if (!$return_mode) {
  2895. $result = $this->imagebmp($image_dst, $this->file_dst_pathname);
  2896. } else {
  2897. ob_start();
  2898. $result = $this->imagebmp($image_dst);
  2899. $return_content = ob_get_contents();
  2900. ob_end_clean();
  2901. }
  2902. if (!$result) {
  2903. $this->processed = false;
  2904. $this->error = $this->translate('file_create', array('BMP'));
  2905. } else {
  2906. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;BMP image created<br />';
  2907. }
  2908. break;
  2909.  
  2910. default:
  2911. $this->processed = false;
  2912. $this->error = $this->translate('no_conversion_type');
  2913. }
  2914. if ($this->processed) {
  2915. if (is_resource($image_src)) imagedestroy($image_src);
  2916. if (is_resource($image_dst)) imagedestroy($image_dst);
  2917. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image objects destroyed<br />';
  2918. }
  2919. }
  2920.  
  2921. } else {
  2922. $this->log .= '- no image processing wanted<br />';
  2923.  
  2924. if (!$return_mode) {
  2925. // copy the file to its final destination. we don't use move_uploaded_file here
  2926. // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file
  2927. if (!copy($this->file_src_pathname, $this->file_dst_pathname)) {
  2928. $this->processed = false;
  2929. $this->error = $this->translate('copy_failed');
  2930. }
  2931. } else {
  2932. // returns the file, so that its content can be received by the caller
  2933. $return_content = @file_get_contents($this->file_src_pathname);
  2934. if ($return_content === FALSE) {
  2935. $this->processed = false;
  2936. $this->error = $this->translate('reading_failed');
  2937. }
  2938. }
  2939. }
  2940. }
  2941.  
  2942. if ($this->processed) {
  2943. $this->log .= '- <b>process OK</b><br />';
  2944. } else {
  2945. $this->log .= '- <b>error</b>: ' . $this->error . '<br />';
  2946. }
  2947.  
  2948. // we reinit all the vars
  2949. $this->init();
  2950.  
  2951. // we may return the image content
  2952. if ($return_mode) return $return_content;
  2953.  
  2954. }
  2955.  
  2956. /**
  2957. * Deletes the uploaded file from its temporary location
  2958. *
  2959. * When PHP uploads a file, it stores it in a temporary location.
  2960. * When you {@link process} the file, you actually copy the resulting file to the given location, it doesn't alter the original file.
  2961. * Once you have processed the file as many times as you wanted, you can delete the uploaded file.
  2962. * If there is open_basedir restrictions, the uploaded file is in fact a temporary file
  2963. *
  2964. * You might want not to use this function if you work on local files, as it will delete the source file
  2965. *
  2966. * @access public
  2967. */
  2968. function clean() {
  2969. $this->log .= '<b>cleanup</b><br />';
  2970. $this->log .= '- delete temp file ' . $this->file_src_pathname . '<br />';
  2971. @unlink($this->file_src_pathname);
  2972. }
  2973.  
  2974.  
  2975. /**
  2976. * Opens a BMP image
  2977. *
  2978. * This function has been written by DHKold, and is used with permission of the author
  2979. *
  2980. * @access public
  2981. */
  2982. function imagecreatefrombmp($filename) {
  2983. if (! $f1 = fopen($filename,"rb")) return false;
  2984.  
  2985. $file = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
  2986. if ($file['file_type'] != 19778) return false;
  2987.  
  2988. $bmp = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  2989. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  2990. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
  2991. $bmp['colors'] = pow(2,$bmp['bits_per_pixel']);
  2992. if ($bmp['size_bitmap'] == 0) $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
  2993. $bmp['bytes_per_pixel'] = $bmp['bits_per_pixel']/8;
  2994. $bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
  2995. $bmp['decal'] = ($bmp['width']*$bmp['bytes_per_pixel']/4);
  2996. $bmp['decal'] -= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
  2997. $bmp['decal'] = 4-(4*$bmp['decal']);
  2998. if ($bmp['decal'] == 4) $bmp['decal'] = 0;
  2999.  
  3000. $palette = array();
  3001. if ($bmp['colors'] < 16777216) {
  3002. $palette = unpack('V'.$bmp['colors'], fread($f1,$bmp['colors']*4));
  3003. }
  3004.  
  3005. $im = fread($f1,$bmp['size_bitmap']);
  3006. $vide = chr(0);
  3007.  
  3008. $res = imagecreatetruecolor($bmp['width'],$bmp['height']);
  3009. $P = 0;
  3010. $Y = $bmp['height']-1;
  3011. while ($Y >= 0) {
  3012. $X=0;
  3013. while ($X < $bmp['width']) {
  3014. if ($bmp['bits_per_pixel'] == 24)
  3015. $color = unpack("V",substr($im,$P,3).$vide);
  3016. elseif ($bmp['bits_per_pixel'] == 16) {
  3017. $color = unpack("n",substr($im,$P,2));
  3018. $color[1] = $palette[$color[1]+1];
  3019. } elseif ($bmp['bits_per_pixel'] == 8) {
  3020. $color = unpack("n",$vide.substr($im,$P,1));
  3021. $color[1] = $palette[$color[1]+1];
  3022. } elseif ($bmp['bits_per_pixel'] == 4) {
  3023. $color = unpack("n",$vide.substr($im,floor($P),1));
  3024. if (($P*2)%2 == 0) $color[1] = ($color[1] >> 4) ; else $color[1] = ($color[1] & 0x0F);
  3025. $color[1] = $palette[$color[1]+1];
  3026. } elseif ($bmp['bits_per_pixel'] == 1) {
  3027. $color = unpack("n",$vide.substr($im,floor($P),1));
  3028. if (($P*8)%8 == 0) $color[1] = $color[1] >>7;
  3029. elseif (($P*8)%8 == 1) $color[1] = ($color[1] & 0x40)>>6;
  3030. elseif (($P*8)%8 == 2) $color[1] = ($color[1] & 0x20)>>5;
  3031. elseif (($P*8)%8 == 3) $color[1] = ($color[1] & 0x10)>>4;
  3032. elseif (($P*8)%8 == 4) $color[1] = ($color[1] & 0x8)>>3;
  3033. elseif (($P*8)%8 == 5) $color[1] = ($color[1] & 0x4)>>2;
  3034. elseif (($P*8)%8 == 6) $color[1] = ($color[1] & 0x2)>>1;
  3035. elseif (($P*8)%8 == 7) $color[1] = ($color[1] & 0x1);
  3036. $color[1] = $palette[$color[1]+1];
  3037. } else
  3038. return FALSE;
  3039. imagesetpixel($res,$X,$Y,$color[1]);
  3040. $X++;
  3041. $P += $bmp['bytes_per_pixel'];
  3042. }
  3043. $Y--;
  3044. $P+=$bmp['decal'];
  3045. }
  3046. fclose($f1);
  3047. return $res;
  3048. }
  3049.  
  3050. /**
  3051. * Saves a BMP image
  3052. *
  3053. * This function has been published on the PHP website, and can be used freely
  3054. *
  3055. * @access public
  3056. */
  3057. function imagebmp(&$im, $filename = "") {
  3058.  
  3059. if (!$im) return false;
  3060. $w = imagesx($im);
  3061. $h = imagesy($im);
  3062. $result = '';
  3063.  
  3064. // if the image is not true color, we convert it first
  3065. if (!imageistruecolor($im)) {
  3066. $tmp = imagecreatetruecolor($w, $h);
  3067. imagecopy($tmp, $im, 0, 0, 0, 0, $w, $h);
  3068. imagedestroy($im);
  3069. $im = & $tmp;
  3070. }
  3071.  
  3072. $biBPLine = $w * 3;
  3073. $biStride = ($biBPLine + 3) & ~3;
  3074. $biSizeImage = $biStride * $h;
  3075. $bfOffBits = 54;
  3076. $bfSize = $bfOffBits + $biSizeImage;
  3077.  
  3078. $result .= substr('BM', 0, 2);
  3079. $result .= pack ('VvvV', $bfSize, 0, 0, $bfOffBits);
  3080. $result .= pack ('VVVvvVVVVVV', 40, $w, $h, 1, 24, 0, $biSizeImage, 0, 0, 0, 0);
  3081.  
  3082. $numpad = $biStride - $biBPLine;
  3083. for ($y = $h - 1; $y >= 0; --$y) {
  3084. for ($x = 0; $x < $w; ++$x) {
  3085. $col = imagecolorat ($im, $x, $y);
  3086. $result .= substr(pack ('V', $col), 0, 3);
  3087. }
  3088. for ($i = 0; $i < $numpad; ++$i)
  3089. $result .= pack ('C', 0);
  3090. }
  3091.  
  3092. if($filename==""){
  3093. echo $result;
  3094. } else {
  3095. $file = fopen($filename, "wb");
  3096. fwrite($file, $result);
  3097. fclose($file);
  3098. }
  3099. return true;
  3100. }
  3101. }