Просмотр файла download/include/class_upload.php

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