Просмотр файла vendor/verot/class.upload.php/src/class.upload.php

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