Просмотр файла engine/classes/class_upload.php

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