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

/lib/external/class.upload/class.upload.php

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