You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1839 lines
42 KiB

7 years ago
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Image Manipulation class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Image_lib
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/libraries/image_lib.html
  47. */
  48. class CI_Image_lib {
  49. /**
  50. * PHP extension/library to use for image manipulation
  51. * Can be: imagemagick, netpbm, gd, gd2
  52. *
  53. * @var string
  54. */
  55. public $image_library = 'gd2';
  56. /**
  57. * Path to the graphic library (if applicable)
  58. *
  59. * @var string
  60. */
  61. public $library_path = '';
  62. /**
  63. * Whether to send to browser or write to disk
  64. *
  65. * @var bool
  66. */
  67. public $dynamic_output = FALSE;
  68. /**
  69. * Path to original image
  70. *
  71. * @var string
  72. */
  73. public $source_image = '';
  74. /**
  75. * Path to the modified image
  76. *
  77. * @var string
  78. */
  79. public $new_image = '';
  80. /**
  81. * Image width
  82. *
  83. * @var int
  84. */
  85. public $width = '';
  86. /**
  87. * Image height
  88. *
  89. * @var int
  90. */
  91. public $height = '';
  92. /**
  93. * Quality percentage of new image
  94. *
  95. * @var int
  96. */
  97. public $quality = 90;
  98. /**
  99. * Whether to create a thumbnail
  100. *
  101. * @var bool
  102. */
  103. public $create_thumb = FALSE;
  104. /**
  105. * String to add to thumbnail version of image
  106. *
  107. * @var string
  108. */
  109. public $thumb_marker = '_thumb';
  110. /**
  111. * Whether to maintain aspect ratio when resizing or use hard values
  112. *
  113. * @var bool
  114. */
  115. public $maintain_ratio = TRUE;
  116. /**
  117. * auto, height, or width. Determines what to use as the master dimension
  118. *
  119. * @var string
  120. */
  121. public $master_dim = 'auto';
  122. /**
  123. * Angle at to rotate image
  124. *
  125. * @var string
  126. */
  127. public $rotation_angle = '';
  128. /**
  129. * X Coordinate for manipulation of the current image
  130. *
  131. * @var int
  132. */
  133. public $x_axis = '';
  134. /**
  135. * Y Coordinate for manipulation of the current image
  136. *
  137. * @var int
  138. */
  139. public $y_axis = '';
  140. // --------------------------------------------------------------------------
  141. // Watermark Vars
  142. // --------------------------------------------------------------------------
  143. /**
  144. * Watermark text if graphic is not used
  145. *
  146. * @var string
  147. */
  148. public $wm_text = '';
  149. /**
  150. * Type of watermarking. Options: text/overlay
  151. *
  152. * @var string
  153. */
  154. public $wm_type = 'text';
  155. /**
  156. * Default transparency for watermark
  157. *
  158. * @var int
  159. */
  160. public $wm_x_transp = 4;
  161. /**
  162. * Default transparency for watermark
  163. *
  164. * @var int
  165. */
  166. public $wm_y_transp = 4;
  167. /**
  168. * Watermark image path
  169. *
  170. * @var string
  171. */
  172. public $wm_overlay_path = '';
  173. /**
  174. * TT font
  175. *
  176. * @var string
  177. */
  178. public $wm_font_path = '';
  179. /**
  180. * Font size (different versions of GD will either use points or pixels)
  181. *
  182. * @var int
  183. */
  184. public $wm_font_size = 17;
  185. /**
  186. * Vertical alignment: T M B
  187. *
  188. * @var string
  189. */
  190. public $wm_vrt_alignment = 'B';
  191. /**
  192. * Horizontal alignment: L R C
  193. *
  194. * @var string
  195. */
  196. public $wm_hor_alignment = 'C';
  197. /**
  198. * Padding around text
  199. *
  200. * @var int
  201. */
  202. public $wm_padding = 0;
  203. /**
  204. * Lets you push text to the right
  205. *
  206. * @var int
  207. */
  208. public $wm_hor_offset = 0;
  209. /**
  210. * Lets you push text down
  211. *
  212. * @var int
  213. */
  214. public $wm_vrt_offset = 0;
  215. /**
  216. * Text color
  217. *
  218. * @var string
  219. */
  220. protected $wm_font_color = '#ffffff';
  221. /**
  222. * Dropshadow color
  223. *
  224. * @var string
  225. */
  226. protected $wm_shadow_color = '';
  227. /**
  228. * Dropshadow distance
  229. *
  230. * @var int
  231. */
  232. public $wm_shadow_distance = 2;
  233. /**
  234. * Image opacity: 1 - 100 Only works with image
  235. *
  236. * @var int
  237. */
  238. public $wm_opacity = 50;
  239. // --------------------------------------------------------------------------
  240. // Private Vars
  241. // --------------------------------------------------------------------------
  242. /**
  243. * Source image folder
  244. *
  245. * @var string
  246. */
  247. public $source_folder = '';
  248. /**
  249. * Destination image folder
  250. *
  251. * @var string
  252. */
  253. public $dest_folder = '';
  254. /**
  255. * Image mime-type
  256. *
  257. * @var string
  258. */
  259. public $mime_type = '';
  260. /**
  261. * Original image width
  262. *
  263. * @var int
  264. */
  265. public $orig_width = '';
  266. /**
  267. * Original image height
  268. *
  269. * @var int
  270. */
  271. public $orig_height = '';
  272. /**
  273. * Image format
  274. *
  275. * @var string
  276. */
  277. public $image_type = '';
  278. /**
  279. * Size of current image
  280. *
  281. * @var string
  282. */
  283. public $size_str = '';
  284. /**
  285. * Full path to source image
  286. *
  287. * @var string
  288. */
  289. public $full_src_path = '';
  290. /**
  291. * Full path to destination image
  292. *
  293. * @var string
  294. */
  295. public $full_dst_path = '';
  296. /**
  297. * File permissions
  298. *
  299. * @var int
  300. */
  301. public $file_permissions = 0644;
  302. /**
  303. * Name of function to create image
  304. *
  305. * @var string
  306. */
  307. public $create_fnc = 'imagecreatetruecolor';
  308. /**
  309. * Name of function to copy image
  310. *
  311. * @var string
  312. */
  313. public $copy_fnc = 'imagecopyresampled';
  314. /**
  315. * Error messages
  316. *
  317. * @var array
  318. */
  319. public $error_msg = array();
  320. /**
  321. * Whether to have a drop shadow on watermark
  322. *
  323. * @var bool
  324. */
  325. protected $wm_use_drop_shadow = FALSE;
  326. /**
  327. * Whether to use truetype fonts
  328. *
  329. * @var bool
  330. */
  331. public $wm_use_truetype = FALSE;
  332. /**
  333. * Initialize Image Library
  334. *
  335. * @param array $props
  336. * @return void
  337. */
  338. public function __construct($props = array())
  339. {
  340. if (count($props) > 0)
  341. {
  342. $this->initialize($props);
  343. }
  344. /**
  345. * A work-around for some improperly formatted, but
  346. * usable JPEGs; known to be produced by Samsung
  347. * smartphones' front-facing cameras.
  348. *
  349. * @see https://github.com/bcit-ci/CodeIgniter/issues/4967
  350. * @see https://bugs.php.net/bug.php?id=72404
  351. */
  352. ini_set('gd.jpeg_ignore_warning', 1);
  353. log_message('info', 'Image Lib Class Initialized');
  354. }
  355. // --------------------------------------------------------------------
  356. /**
  357. * Initialize image properties
  358. *
  359. * Resets values in case this class is used in a loop
  360. *
  361. * @return void
  362. */
  363. public function clear()
  364. {
  365. $props = array('thumb_marker', 'library_path', 'source_image', 'new_image', 'width', 'height', 'rotation_angle', 'x_axis', 'y_axis', 'wm_text', 'wm_overlay_path', 'wm_font_path', 'wm_shadow_color', 'source_folder', 'dest_folder', 'mime_type', 'orig_width', 'orig_height', 'image_type', 'size_str', 'full_src_path', 'full_dst_path');
  366. foreach ($props as $val)
  367. {
  368. $this->$val = '';
  369. }
  370. $this->image_library = 'gd2';
  371. $this->dynamic_output = FALSE;
  372. $this->quality = 90;
  373. $this->create_thumb = FALSE;
  374. $this->thumb_marker = '_thumb';
  375. $this->maintain_ratio = TRUE;
  376. $this->master_dim = 'auto';
  377. $this->wm_type = 'text';
  378. $this->wm_x_transp = 4;
  379. $this->wm_y_transp = 4;
  380. $this->wm_font_size = 17;
  381. $this->wm_vrt_alignment = 'B';
  382. $this->wm_hor_alignment = 'C';
  383. $this->wm_padding = 0;
  384. $this->wm_hor_offset = 0;
  385. $this->wm_vrt_offset = 0;
  386. $this->wm_font_color = '#ffffff';
  387. $this->wm_shadow_distance = 2;
  388. $this->wm_opacity = 50;
  389. $this->create_fnc = 'imagecreatetruecolor';
  390. $this->copy_fnc = 'imagecopyresampled';
  391. $this->error_msg = array();
  392. $this->wm_use_drop_shadow = FALSE;
  393. $this->wm_use_truetype = FALSE;
  394. }
  395. // --------------------------------------------------------------------
  396. /**
  397. * initialize image preferences
  398. *
  399. * @param array
  400. * @return bool
  401. */
  402. public function initialize($props = array())
  403. {
  404. // Convert array elements into class variables
  405. if (count($props) > 0)
  406. {
  407. foreach ($props as $key => $val)
  408. {
  409. if (property_exists($this, $key))
  410. {
  411. if (in_array($key, array('wm_font_color', 'wm_shadow_color'), TRUE))
  412. {
  413. if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
  414. {
  415. /* $matches[1] contains our hex color value, but it might be
  416. * both in the full 6-length format or the shortened 3-length
  417. * value.
  418. * We'll later need the full version, so we keep it if it's
  419. * already there and if not - we'll convert to it. We can
  420. * access string characters by their index as in an array,
  421. * so we'll do that and use concatenation to form the final
  422. * value:
  423. */
  424. $val = (strlen($matches[1]) === 6)
  425. ? '#'.$matches[1]
  426. : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2];
  427. }
  428. else
  429. {
  430. continue;
  431. }
  432. }
  433. elseif (in_array($key, array('width', 'height'), TRUE) && ! ctype_digit((string) $val))
  434. {
  435. continue;
  436. }
  437. $this->$key = $val;
  438. }
  439. }
  440. }
  441. // Is there a source image? If not, there's no reason to continue
  442. if ($this->source_image === '')
  443. {
  444. $this->set_error('imglib_source_image_required');
  445. return FALSE;
  446. }
  447. /* Is getimagesize() available?
  448. *
  449. * We use it to determine the image properties (width/height).
  450. * Note: We need to figure out how to determine image
  451. * properties using ImageMagick and NetPBM
  452. */
  453. if ( ! function_exists('getimagesize'))
  454. {
  455. $this->set_error('imglib_gd_required_for_props');
  456. return FALSE;
  457. }
  458. $this->image_library = strtolower($this->image_library);
  459. /* Set the full server path
  460. *
  461. * The source image may or may not contain a path.
  462. * Either way, we'll try use realpath to generate the
  463. * full server path in order to more reliably read it.
  464. */
  465. if (($full_source_path = realpath($this->source_image)) !== FALSE)
  466. {
  467. $full_source_path = str_replace('\\', '/', $full_source_path);
  468. }
  469. else
  470. {
  471. $full_source_path = $this->source_image;
  472. }
  473. $x = explode('/', $full_source_path);
  474. $this->source_image = end($x);
  475. $this->source_folder = str_replace($this->source_image, '', $full_source_path);
  476. // Set the Image Properties
  477. if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
  478. {
  479. return FALSE;
  480. }
  481. /*
  482. * Assign the "new" image name/path
  483. *
  484. * If the user has set a "new_image" name it means
  485. * we are making a copy of the source image. If not
  486. * it means we are altering the original. We'll
  487. * set the destination filename and path accordingly.
  488. */
  489. if ($this->new_image === '')
  490. {
  491. $this->dest_image = $this->source_image;
  492. $this->dest_folder = $this->source_folder;
  493. }
  494. elseif (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE)
  495. {
  496. $this->dest_image = $this->new_image;
  497. $this->dest_folder = $this->source_folder;
  498. }
  499. else
  500. {
  501. // Is there a file name?
  502. if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $this->new_image))
  503. {
  504. $this->dest_image = $this->source_image;
  505. $this->dest_folder = $this->new_image;
  506. }
  507. else
  508. {
  509. $x = explode('/', str_replace('\\', '/', $this->new_image));
  510. $this->dest_image = end($x);
  511. $this->dest_folder = str_replace($this->dest_image, '', $this->new_image);
  512. }
  513. $this->dest_folder = realpath($this->dest_folder).'/';
  514. }
  515. /* Compile the finalized filenames/paths
  516. *
  517. * We'll create two master strings containing the
  518. * full server path to the source image and the
  519. * full server path to the destination image.
  520. * We'll also split the destination image name
  521. * so we can insert the thumbnail marker if needed.
  522. */
  523. if ($this->create_thumb === FALSE OR $this->thumb_marker === '')
  524. {
  525. $this->thumb_marker = '';
  526. }
  527. $xp = $this->explode_name($this->dest_image);
  528. $filename = $xp['name'];
  529. $file_ext = $xp['ext'];
  530. $this->full_src_path = $this->source_folder.$this->source_image;
  531. $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
  532. /* Should we maintain image proportions?
  533. *
  534. * When creating thumbs or copies, the target width/height
  535. * might not be in correct proportion with the source
  536. * image's width/height. We'll recalculate it here.
  537. */
  538. if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0))
  539. {
  540. $this->image_reproportion();
  541. }
  542. /* Was a width and height specified?
  543. *
  544. * If the destination width/height was not submitted we
  545. * will use the values from the actual file
  546. */
  547. if ($this->width === '')
  548. {
  549. $this->width = $this->orig_width;
  550. }
  551. if ($this->height === '')
  552. {
  553. $this->height = $this->orig_height;
  554. }
  555. // Set the quality
  556. $this->quality = trim(str_replace('%', '', $this->quality));
  557. if ($this->quality === '' OR $this->quality === 0 OR ! ctype_digit($this->quality))
  558. {
  559. $this->quality = 90;
  560. }
  561. // Set the x/y coordinates
  562. is_numeric($this->x_axis) OR $this->x_axis = 0;
  563. is_numeric($this->y_axis) OR $this->y_axis = 0;
  564. // Watermark-related Stuff...
  565. if ($this->wm_overlay_path !== '')
  566. {
  567. $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
  568. }
  569. if ($this->wm_shadow_color !== '')
  570. {
  571. $this->wm_use_drop_shadow = TRUE;
  572. }
  573. elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '')
  574. {
  575. $this->wm_use_drop_shadow = FALSE;
  576. }
  577. if ($this->wm_font_path !== '')
  578. {
  579. $this->wm_use_truetype = TRUE;
  580. }
  581. return TRUE;
  582. }
  583. // --------------------------------------------------------------------
  584. /**
  585. * Image Resize
  586. *
  587. * This is a wrapper function that chooses the proper
  588. * resize function based on the protocol specified
  589. *
  590. * @return bool
  591. */
  592. public function resize()
  593. {
  594. $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
  595. return $this->$protocol('resize');
  596. }
  597. // --------------------------------------------------------------------
  598. /**
  599. * Image Crop
  600. *
  601. * This is a wrapper function that chooses the proper
  602. * cropping function based on the protocol specified
  603. *
  604. * @return bool
  605. */
  606. public function crop()
  607. {
  608. $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
  609. return $this->$protocol('crop');
  610. }
  611. // --------------------------------------------------------------------
  612. /**
  613. * Image Rotate
  614. *
  615. * This is a wrapper function that chooses the proper
  616. * rotation function based on the protocol specified
  617. *
  618. * @return bool
  619. */
  620. public function rotate()
  621. {
  622. // Allowed rotation values
  623. $degs = array(90, 180, 270, 'vrt', 'hor');
  624. if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs))
  625. {
  626. $this->set_error('imglib_rotation_angle_required');
  627. return FALSE;
  628. }
  629. // Reassign the width and height
  630. if ($this->rotation_angle === 90 OR $this->rotation_angle === 270)
  631. {
  632. $this->width = $this->orig_height;
  633. $this->height = $this->orig_width;
  634. }
  635. else
  636. {
  637. $this->width = $this->orig_width;
  638. $this->height = $this->orig_height;
  639. }
  640. // Choose resizing function
  641. if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm')
  642. {
  643. $protocol = 'image_process_'.$this->image_library;
  644. return $this->$protocol('rotate');
  645. }
  646. return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
  647. ? $this->image_mirror_gd()
  648. : $this->image_rotate_gd();
  649. }
  650. // --------------------------------------------------------------------
  651. /**
  652. * Image Process Using GD/GD2
  653. *
  654. * This function will resize or crop
  655. *
  656. * @param string
  657. * @return bool
  658. */
  659. public function image_process_gd($action = 'resize')
  660. {
  661. $v2_override = FALSE;
  662. // If the target width/height match the source, AND if the new file name is not equal to the old file name
  663. // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
  664. if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
  665. {
  666. if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
  667. {
  668. chmod($this->full_dst_path, $this->file_permissions);
  669. }
  670. return TRUE;
  671. }
  672. // Let's set up our values based on the action
  673. if ($action === 'crop')
  674. {
  675. // Reassign the source width/height if cropping
  676. $this->orig_width = $this->width;
  677. $this->orig_height = $this->height;
  678. // GD 2.0 has a cropping bug so we'll test for it
  679. if ($this->gd_version() !== FALSE)
  680. {
  681. $gd_version = str_replace('0', '', $this->gd_version());
  682. $v2_override = ($gd_version == 2);
  683. }
  684. }
  685. else
  686. {
  687. // If resizing the x/y axis must be zero
  688. $this->x_axis = 0;
  689. $this->y_axis = 0;
  690. }
  691. // Create the image handle
  692. if ( ! ($src_img = $this->image_create_gd()))
  693. {
  694. return FALSE;
  695. }
  696. /* Create the image
  697. *
  698. * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
  699. * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
  700. * below should that ever prove inaccurate.
  701. *
  702. * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE)
  703. */
  704. if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
  705. {
  706. $create = 'imagecreatetruecolor';
  707. $copy = 'imagecopyresampled';
  708. }
  709. else
  710. {
  711. $create = 'imagecreate';
  712. $copy = 'imagecopyresized';
  713. }
  714. $dst_img = $create($this->width, $this->height);
  715. if ($this->image_type === 3) // png we can actually preserve transparency
  716. {
  717. imagealphablending($dst_img, FALSE);
  718. imagesavealpha($dst_img, TRUE);
  719. }
  720. $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
  721. // Show the image
  722. if ($this->dynamic_output === TRUE)
  723. {
  724. $this->image_display_gd($dst_img);
  725. }
  726. elseif ( ! $this->image_save_gd($dst_img)) // Or save it
  727. {
  728. return FALSE;
  729. }
  730. // Kill the file handles
  731. imagedestroy($dst_img);
  732. imagedestroy($src_img);
  733. chmod($this->full_dst_path, $this->file_permissions);
  734. return TRUE;
  735. }
  736. // --------------------------------------------------------------------
  737. /**
  738. * Image Process Using ImageMagick
  739. *
  740. * This function will resize, crop or rotate
  741. *
  742. * @param string
  743. * @return bool
  744. */
  745. public function image_process_imagemagick($action = 'resize')
  746. {
  747. // Do we have a vaild library path?
  748. if ($this->library_path === '')
  749. {
  750. $this->set_error('imglib_libpath_invalid');
  751. return FALSE;
  752. }
  753. if ( ! preg_match('/convert$/i', $this->library_path))
  754. {
  755. $this->library_path = rtrim($this->library_path, '/').'/convert';
  756. }
  757. // Execute the command
  758. $cmd = $this->library_path.' -quality '.$this->quality;
  759. if ($action === 'crop')
  760. {
  761. $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
  762. }
  763. elseif ($action === 'rotate')
  764. {
  765. $cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
  766. ? ' -flop'
  767. : ' -rotate '.$this->rotation_angle;
  768. }
  769. else // Resize
  770. {
  771. if($this->maintain_ratio === TRUE)
  772. {
  773. $cmd .= ' -resize '.$this->width.'x'.$this->height;
  774. }
  775. else
  776. {
  777. $cmd .= ' -resize '.$this->width.'x'.$this->height.'\!';
  778. }
  779. }
  780. $cmd .= ' '.escapeshellarg($this->full_src_path).' '.escapeshellarg($this->full_dst_path).' 2>&1';
  781. $retval = 1;
  782. // exec() might be disabled
  783. if (function_usable('exec'))
  784. {
  785. @exec($cmd, $output, $retval);
  786. }
  787. // Did it work?
  788. if ($retval > 0)
  789. {
  790. $this->set_error('imglib_image_process_failed');
  791. return FALSE;
  792. }
  793. chmod($this->full_dst_path, $this->file_permissions);
  794. return TRUE;
  795. }
  796. // --------------------------------------------------------------------
  797. /**
  798. * Image Process Using NetPBM
  799. *
  800. * This function will resize, crop or rotate
  801. *
  802. * @param string
  803. * @return bool
  804. */
  805. public function image_process_netpbm($action = 'resize')
  806. {
  807. if ($this->library_path === '')
  808. {
  809. $this->set_error('imglib_libpath_invalid');
  810. return FALSE;
  811. }
  812. // Build the resizing command
  813. switch ($this->image_type)
  814. {
  815. case 1 :
  816. $cmd_in = 'giftopnm';
  817. $cmd_out = 'ppmtogif';
  818. break;
  819. case 2 :
  820. $cmd_in = 'jpegtopnm';
  821. $cmd_out = 'ppmtojpeg';
  822. break;
  823. case 3 :
  824. $cmd_in = 'pngtopnm';
  825. $cmd_out = 'ppmtopng';
  826. break;
  827. }
  828. if ($action === 'crop')
  829. {
  830. $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
  831. }
  832. elseif ($action === 'rotate')
  833. {
  834. switch ($this->rotation_angle)
  835. {
  836. case 90: $angle = 'r270';
  837. break;
  838. case 180: $angle = 'r180';
  839. break;
  840. case 270: $angle = 'r90';
  841. break;
  842. case 'vrt': $angle = 'tb';
  843. break;
  844. case 'hor': $angle = 'lr';
  845. break;
  846. }
  847. $cmd_inner = 'pnmflip -'.$angle.' ';
  848. }
  849. else // Resize
  850. {
  851. $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
  852. }
  853. $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
  854. $retval = 1;
  855. // exec() might be disabled
  856. if (function_usable('exec'))
  857. {
  858. @exec($cmd, $output, $retval);
  859. }
  860. // Did it work?
  861. if ($retval > 0)
  862. {
  863. $this->set_error('imglib_image_process_failed');
  864. return FALSE;
  865. }
  866. // With NetPBM we have to create a temporary image.
  867. // If you try manipulating the original it fails so
  868. // we have to rename the temp file.
  869. copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
  870. unlink($this->dest_folder.'netpbm.tmp');
  871. chmod($this->full_dst_path, $this->file_permissions);
  872. return TRUE;
  873. }
  874. // --------------------------------------------------------------------
  875. /**
  876. * Image Rotate Using GD
  877. *
  878. * @return bool
  879. */
  880. public function image_rotate_gd()
  881. {
  882. // Create the image handle
  883. if ( ! ($src_img = $this->image_create_gd()))
  884. {
  885. return FALSE;
  886. }
  887. // Set the background color
  888. // This won't work with transparent PNG files so we are
  889. // going to have to figure out how to determine the color
  890. // of the alpha channel in a future release.
  891. $white = imagecolorallocate($src_img, 255, 255, 255);
  892. // Rotate it!
  893. $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
  894. // Show the image
  895. if ($this->dynamic_output === TRUE)
  896. {
  897. $this->image_display_gd($dst_img);
  898. }
  899. elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
  900. {
  901. return FALSE;
  902. }
  903. // Kill the file handles
  904. imagedestroy($dst_img);
  905. imagedestroy($src_img);
  906. chmod($this->full_dst_path, $this->file_permissions);
  907. return TRUE;
  908. }
  909. // --------------------------------------------------------------------
  910. /**
  911. * Create Mirror Image using GD
  912. *
  913. * This function will flip horizontal or vertical
  914. *
  915. * @return bool
  916. */
  917. public function image_mirror_gd()
  918. {
  919. if ( ! $src_img = $this->image_create_gd())
  920. {
  921. return FALSE;
  922. }
  923. $width = $this->orig_width;
  924. $height = $this->orig_height;
  925. if ($this->rotation_angle === 'hor')
  926. {
  927. for ($i = 0; $i < $height; $i++)
  928. {
  929. $left = 0;
  930. $right = $width - 1;
  931. while ($left < $right)
  932. {
  933. $cl = imagecolorat($src_img, $left, $i);
  934. $cr = imagecolorat($src_img, $right, $i);
  935. imagesetpixel($src_img, $left, $i, $cr);
  936. imagesetpixel($src_img, $right, $i, $cl);
  937. $left++;
  938. $right--;
  939. }
  940. }
  941. }
  942. else
  943. {
  944. for ($i = 0; $i < $width; $i++)
  945. {
  946. $top = 0;
  947. $bottom = $height - 1;
  948. while ($top < $bottom)
  949. {
  950. $ct = imagecolorat($src_img, $i, $top);
  951. $cb = imagecolorat($src_img, $i, $bottom);
  952. imagesetpixel($src_img, $i, $top, $cb);
  953. imagesetpixel($src_img, $i, $bottom, $ct);
  954. $top++;
  955. $bottom--;
  956. }
  957. }
  958. }
  959. // Show the image
  960. if ($this->dynamic_output === TRUE)
  961. {
  962. $this->image_display_gd($src_img);
  963. }
  964. elseif ( ! $this->image_save_gd($src_img)) // ... or save it
  965. {
  966. return FALSE;
  967. }
  968. // Kill the file handles
  969. imagedestroy($src_img);
  970. chmod($this->full_dst_path, $this->file_permissions);
  971. return TRUE;
  972. }
  973. // --------------------------------------------------------------------
  974. /**
  975. * Image Watermark
  976. *
  977. * This is a wrapper function that chooses the type
  978. * of watermarking based on the specified preference.
  979. *
  980. * @return bool
  981. */
  982. public function watermark()
  983. {
  984. return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
  985. }
  986. // --------------------------------------------------------------------
  987. /**
  988. * Watermark - Graphic Version
  989. *
  990. * @return bool
  991. */
  992. public function overlay_watermark()
  993. {
  994. if ( ! function_exists('imagecolortransparent'))
  995. {
  996. $this->set_error('imglib_gd_required');
  997. return FALSE;
  998. }
  999. // Fetch source image properties
  1000. $this->get_image_properties();
  1001. // Fetch watermark image properties
  1002. $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
  1003. $wm_img_type = $props['image_type'];
  1004. $wm_width = $props['width'];
  1005. $wm_height = $props['height'];
  1006. // Create two image resources
  1007. $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
  1008. $src_img = $this->image_create_gd($this->full_src_path);
  1009. // Reverse the offset if necessary
  1010. // When the image is positioned at the bottom
  1011. // we don't want the vertical offset to push it
  1012. // further down. We want the reverse, so we'll
  1013. // invert the offset. Same with the horizontal
  1014. // offset when the image is at the right
  1015. $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
  1016. $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
  1017. if ($this->wm_vrt_alignment === 'B')
  1018. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  1019. if ($this->wm_hor_alignment === 'R')
  1020. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  1021. // Set the base x and y axis values
  1022. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  1023. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  1024. // Set the vertical position
  1025. if ($this->wm_vrt_alignment === 'M')
  1026. {
  1027. $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
  1028. }
  1029. elseif ($this->wm_vrt_alignment === 'B')
  1030. {
  1031. $y_axis += $this->orig_height - $wm_height;
  1032. }
  1033. // Set the horizontal position
  1034. if ($this->wm_hor_alignment === 'C')
  1035. {
  1036. $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
  1037. }
  1038. elseif ($this->wm_hor_alignment === 'R')
  1039. {
  1040. $x_axis += $this->orig_width - $wm_width;
  1041. }
  1042. // Build the finalized image
  1043. if ($wm_img_type === 3 && function_exists('imagealphablending'))
  1044. {
  1045. @imagealphablending($src_img, TRUE);
  1046. }
  1047. // Set RGB values for text and shadow
  1048. $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
  1049. $alpha = ($rgba & 0x7F000000) >> 24;
  1050. // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
  1051. if ($alpha > 0)
  1052. {
  1053. // copy the image directly, the image's alpha transparency being the sole determinant of blending
  1054. imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
  1055. }
  1056. else
  1057. {
  1058. // set our RGB value from above to be transparent and merge the images with the specified opacity
  1059. imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
  1060. imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
  1061. }
  1062. // We can preserve transparency for PNG images
  1063. if ($this->image_type === 3)
  1064. {
  1065. imagealphablending($src_img, FALSE);
  1066. imagesavealpha($src_img, TRUE);
  1067. }
  1068. // Output the image
  1069. if ($this->dynamic_output === TRUE)
  1070. {
  1071. $this->image_display_gd($src_img);
  1072. }
  1073. elseif ( ! $this->image_save_gd($src_img)) // ... or save it
  1074. {
  1075. return FALSE;
  1076. }
  1077. imagedestroy($src_img);
  1078. imagedestroy($wm_img);
  1079. return TRUE;
  1080. }
  1081. // --------------------------------------------------------------------
  1082. /**
  1083. * Watermark - Text Version
  1084. *
  1085. * @return bool
  1086. */
  1087. public function text_watermark()
  1088. {
  1089. if ( ! ($src_img = $this->image_create_gd()))
  1090. {
  1091. return FALSE;
  1092. }
  1093. if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path))
  1094. {
  1095. $this->set_error('imglib_missing_font');
  1096. return FALSE;
  1097. }
  1098. // Fetch source image properties
  1099. $this->get_image_properties();
  1100. // Reverse the vertical offset
  1101. // When the image is positioned at the bottom
  1102. // we don't want the vertical offset to push it
  1103. // further down. We want the reverse, so we'll
  1104. // invert the offset. Note: The horizontal
  1105. // offset flips itself automatically
  1106. if ($this->wm_vrt_alignment === 'B')
  1107. {
  1108. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  1109. }
  1110. if ($this->wm_hor_alignment === 'R')
  1111. {
  1112. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  1113. }
  1114. // Set font width and height
  1115. // These are calculated differently depending on
  1116. // whether we are using the true type font or not
  1117. if ($this->wm_use_truetype === TRUE)
  1118. {
  1119. if (empty($this->wm_font_size))
  1120. {
  1121. $this->wm_font_size = 17;
  1122. }
  1123. if (function_exists('imagettfbbox'))
  1124. {
  1125. $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text);
  1126. $temp = $temp[2] - $temp[0];
  1127. $fontwidth = $temp / strlen($this->wm_text);
  1128. }
  1129. else
  1130. {
  1131. $fontwidth = $this->wm_font_size - ($this->wm_font_size / 4);
  1132. }
  1133. $fontheight = $this->wm_font_size;
  1134. $this->wm_vrt_offset += $this->wm_font_size;
  1135. }
  1136. else
  1137. {
  1138. $fontwidth = imagefontwidth($this->wm_font_size);
  1139. $fontheight = imagefontheight($this->wm_font_size);
  1140. }
  1141. // Set base X and Y axis values
  1142. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  1143. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  1144. if ($this->wm_use_drop_shadow === FALSE)
  1145. {
  1146. $this->wm_shadow_distance = 0;
  1147. }
  1148. $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
  1149. $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
  1150. // Set vertical alignment
  1151. if ($this->wm_vrt_alignment === 'M')
  1152. {
  1153. $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
  1154. }
  1155. elseif ($this->wm_vrt_alignment === 'B')
  1156. {
  1157. $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
  1158. }
  1159. // Set horizontal alignment
  1160. if ($this->wm_hor_alignment === 'R')
  1161. {
  1162. $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text)) - $this->wm_shadow_distance;
  1163. }
  1164. elseif ($this->wm_hor_alignment === 'C')
  1165. {
  1166. $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
  1167. }
  1168. if ($this->wm_use_drop_shadow)
  1169. {
  1170. // Offset from text
  1171. $x_shad = $x_axis + $this->wm_shadow_distance;
  1172. $y_shad = $y_axis + $this->wm_shadow_distance;
  1173. /* Set RGB values for shadow
  1174. *
  1175. * First character is #, so we don't really need it.
  1176. * Get the rest of the string and split it into 2-length
  1177. * hex values:
  1178. */
  1179. $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
  1180. $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
  1181. // Add the shadow to the source image
  1182. if ($this->wm_use_truetype)
  1183. {
  1184. imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
  1185. }
  1186. else
  1187. {
  1188. imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
  1189. }
  1190. }
  1191. /* Set RGB values for text
  1192. *
  1193. * First character is #, so we don't really need it.
  1194. * Get the rest of the string and split it into 2-length
  1195. * hex values:
  1196. */
  1197. $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
  1198. $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
  1199. // Add the text to the source image
  1200. if ($this->wm_use_truetype)
  1201. {
  1202. imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
  1203. }
  1204. else
  1205. {
  1206. imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
  1207. }
  1208. // We can preserve transparency for PNG images
  1209. if ($this->image_type === 3)
  1210. {
  1211. imagealphablending($src_img, FALSE);
  1212. imagesavealpha($src_img, TRUE);
  1213. }
  1214. // Output the final image
  1215. if ($this->dynamic_output === TRUE)
  1216. {
  1217. $this->image_display_gd($src_img);
  1218. }
  1219. else
  1220. {
  1221. $this->image_save_gd($src_img);
  1222. }
  1223. imagedestroy($src_img);
  1224. return TRUE;
  1225. }
  1226. // --------------------------------------------------------------------
  1227. /**
  1228. * Create Image - GD
  1229. *
  1230. * This simply creates an image resource handle
  1231. * based on the type of image being processed
  1232. *
  1233. * @param string
  1234. * @param string
  1235. * @return resource
  1236. */
  1237. public function image_create_gd($path = '', $image_type = '')
  1238. {
  1239. if ($path === '')
  1240. {
  1241. $path = $this->full_src_path;
  1242. }
  1243. if ($image_type === '')
  1244. {
  1245. $image_type = $this->image_type;
  1246. }
  1247. switch ($image_type)
  1248. {
  1249. case 1:
  1250. if ( ! function_exists('imagecreatefromgif'))
  1251. {
  1252. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  1253. return FALSE;
  1254. }
  1255. return imagecreatefromgif($path);
  1256. case 2:
  1257. if ( ! function_exists('imagecreatefromjpeg'))
  1258. {
  1259. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  1260. return FALSE;
  1261. }
  1262. return imagecreatefromjpeg($path);
  1263. case 3:
  1264. if ( ! function_exists('imagecreatefrompng'))
  1265. {
  1266. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  1267. return FALSE;
  1268. }
  1269. return imagecreatefrompng($path);
  1270. default:
  1271. $this->set_error(array('imglib_unsupported_imagecreate'));
  1272. return FALSE;
  1273. }
  1274. }
  1275. // --------------------------------------------------------------------
  1276. /**
  1277. * Write image file to disk - GD
  1278. *
  1279. * Takes an image resource as input and writes the file
  1280. * to the specified destination
  1281. *
  1282. * @param resource
  1283. * @return bool
  1284. */
  1285. public function image_save_gd($resource)
  1286. {
  1287. switch ($this->image_type)
  1288. {
  1289. case 1:
  1290. if ( ! function_exists('imagegif'))
  1291. {
  1292. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  1293. return FALSE;
  1294. }
  1295. if ( ! @imagegif($resource, $this->full_dst_path))
  1296. {
  1297. $this->set_error('imglib_save_failed');
  1298. return FALSE;
  1299. }
  1300. break;
  1301. case 2:
  1302. if ( ! function_exists('imagejpeg'))
  1303. {
  1304. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  1305. return FALSE;
  1306. }
  1307. if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
  1308. {
  1309. $this->set_error('imglib_save_failed');
  1310. return FALSE;
  1311. }
  1312. break;
  1313. case 3:
  1314. if ( ! function_exists('imagepng'))
  1315. {
  1316. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  1317. return FALSE;
  1318. }
  1319. if ( ! @imagepng($resource, $this->full_dst_path))
  1320. {
  1321. $this->set_error('imglib_save_failed');
  1322. return FALSE;
  1323. }
  1324. break;
  1325. default:
  1326. $this->set_error(array('imglib_unsupported_imagecreate'));
  1327. return FALSE;
  1328. break;
  1329. }
  1330. return TRUE;
  1331. }
  1332. // --------------------------------------------------------------------
  1333. /**
  1334. * Dynamically outputs an image
  1335. *
  1336. * @param resource
  1337. * @return void
  1338. */
  1339. public function image_display_gd($resource)
  1340. {
  1341. header('Content-Disposition: filename='.$this->source_image.';');
  1342. header('Content-Type: '.$this->mime_type);
  1343. header('Content-Transfer-Encoding: binary');
  1344. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  1345. switch ($this->image_type)
  1346. {
  1347. case 1 : imagegif($resource);
  1348. break;
  1349. case 2 : imagejpeg($resource, NULL, $this->quality);
  1350. break;
  1351. case 3 : imagepng($resource);
  1352. break;
  1353. default: echo 'Unable to display the image';
  1354. break;
  1355. }
  1356. }
  1357. // --------------------------------------------------------------------
  1358. /**
  1359. * Re-proportion Image Width/Height
  1360. *
  1361. * When creating thumbs, the desired width/height
  1362. * can end up warping the image due to an incorrect
  1363. * ratio between the full-sized image and the thumb.
  1364. *
  1365. * This function lets us re-proportion the width/height
  1366. * if users choose to maintain the aspect ratio when resizing.
  1367. *
  1368. * @return void
  1369. */
  1370. public function image_reproportion()
  1371. {
  1372. if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
  1373. OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height))
  1374. OR ! ctype_digit((string) $this->orig_width) OR ! ctype_digit((string) $this->orig_height))
  1375. {
  1376. return;
  1377. }
  1378. // Sanitize
  1379. $this->width = (int) $this->width;
  1380. $this->height = (int) $this->height;
  1381. if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
  1382. {
  1383. if ($this->width > 0 && $this->height > 0)
  1384. {
  1385. $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
  1386. ? 'width' : 'height';
  1387. }
  1388. else
  1389. {
  1390. $this->master_dim = ($this->height === 0) ? 'width' : 'height';
  1391. }
  1392. }
  1393. elseif (($this->master_dim === 'width' && $this->width === 0)
  1394. OR ($this->master_dim === 'height' && $this->height === 0))
  1395. {
  1396. return;
  1397. }
  1398. if ($this->master_dim === 'width')
  1399. {
  1400. $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
  1401. }
  1402. else
  1403. {
  1404. $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
  1405. }
  1406. }
  1407. // --------------------------------------------------------------------
  1408. /**
  1409. * Get image properties
  1410. *
  1411. * A helper function that gets info about the file
  1412. *
  1413. * @param string
  1414. * @param bool
  1415. * @return mixed
  1416. */
  1417. public function get_image_properties($path = '', $return = FALSE)
  1418. {
  1419. // For now we require GD but we should
  1420. // find a way to determine this using IM or NetPBM
  1421. if ($path === '')
  1422. {
  1423. $path = $this->full_src_path;
  1424. }
  1425. if ( ! file_exists($path))
  1426. {
  1427. $this->set_error('imglib_invalid_path');
  1428. return FALSE;
  1429. }
  1430. $vals = getimagesize($path);
  1431. if ($vals === FALSE)
  1432. {
  1433. $this->set_error('imglib_invalid_image');
  1434. return FALSE;
  1435. }
  1436. $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
  1437. $mime = isset($types[$vals[2]]) ? 'image/'.$types[$vals[2]] : 'image/jpg';
  1438. if ($return === TRUE)
  1439. {
  1440. return array(
  1441. 'width' => $vals[0],
  1442. 'height' => $vals[1],
  1443. 'image_type' => $vals[2],
  1444. 'size_str' => $vals[3],
  1445. 'mime_type' => $mime
  1446. );
  1447. }
  1448. $this->orig_width = $vals[0];
  1449. $this->orig_height = $vals[1];
  1450. $this->image_type = $vals[2];
  1451. $this->size_str = $vals[3];
  1452. $this->mime_type = $mime;
  1453. return TRUE;
  1454. }
  1455. // --------------------------------------------------------------------
  1456. /**
  1457. * Size calculator
  1458. *
  1459. * This function takes a known width x height and
  1460. * recalculates it to a new size. Only one
  1461. * new variable needs to be known
  1462. *
  1463. * $props = array(
  1464. * 'width' => $width,
  1465. * 'height' => $height,
  1466. * 'new_width' => 40,
  1467. * 'new_height' => ''
  1468. * );
  1469. *
  1470. * @param array
  1471. * @return array
  1472. */
  1473. public function size_calculator($vals)
  1474. {
  1475. if ( ! is_array($vals))
  1476. {
  1477. return;
  1478. }
  1479. $allowed = array('new_width', 'new_height', 'width', 'height');
  1480. foreach ($allowed as $item)
  1481. {
  1482. if (empty($vals[$item]))
  1483. {
  1484. $vals[$item] = 0;
  1485. }
  1486. }
  1487. if ($vals['width'] === 0 OR $vals['height'] === 0)
  1488. {
  1489. return $vals;
  1490. }
  1491. if ($vals['new_width'] === 0)
  1492. {
  1493. $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
  1494. }
  1495. elseif ($vals['new_height'] === 0)
  1496. {
  1497. $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
  1498. }
  1499. return $vals;
  1500. }
  1501. // --------------------------------------------------------------------
  1502. /**
  1503. * Explode source_image
  1504. *
  1505. * This is a helper function that extracts the extension
  1506. * from the source_image. This function lets us deal with
  1507. * source_images with multiple periods, like: my.cool.jpg
  1508. * It returns an associative array with two elements:
  1509. * $array['ext'] = '.jpg';
  1510. * $array['name'] = 'my.cool';
  1511. *
  1512. * @param array
  1513. * @return array
  1514. */
  1515. public function explode_name($source_image)
  1516. {
  1517. $ext = strrchr($source_image, '.');
  1518. $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
  1519. return array('ext' => $ext, 'name' => $name);
  1520. }
  1521. // --------------------------------------------------------------------
  1522. /**
  1523. * Is GD Installed?
  1524. *
  1525. * @return bool
  1526. */
  1527. public function gd_loaded()
  1528. {
  1529. if ( ! extension_loaded('gd'))
  1530. {
  1531. /* As it is stated in the PHP manual, dl() is not always available
  1532. * and even if so - it could generate an E_WARNING message on failure
  1533. */
  1534. return (function_exists('dl') && @dl('gd.so'));
  1535. }
  1536. return TRUE;
  1537. }
  1538. // --------------------------------------------------------------------
  1539. /**
  1540. * Get GD version
  1541. *
  1542. * @return mixed
  1543. */
  1544. public function gd_version()
  1545. {
  1546. if (function_exists('gd_info'))
  1547. {
  1548. $gd_version = @gd_info();
  1549. return preg_replace('/\D/', '', $gd_version['GD Version']);
  1550. }
  1551. return FALSE;
  1552. }
  1553. // --------------------------------------------------------------------
  1554. /**
  1555. * Set error message
  1556. *
  1557. * @param string
  1558. * @return void
  1559. */
  1560. public function set_error($msg)
  1561. {
  1562. $CI =& get_instance();
  1563. $CI->lang->load('imglib');
  1564. if (is_array($msg))
  1565. {
  1566. foreach ($msg as $val)
  1567. {
  1568. $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
  1569. $this->error_msg[] = $msg;
  1570. log_message('error', $msg);
  1571. }
  1572. }
  1573. else
  1574. {
  1575. $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
  1576. $this->error_msg[] = $msg;
  1577. log_message('error', $msg);
  1578. }
  1579. }
  1580. // --------------------------------------------------------------------
  1581. /**
  1582. * Show error messages
  1583. *
  1584. * @param string
  1585. * @param string
  1586. * @return string
  1587. */
  1588. public function display_errors($open = '<p>', $close = '</p>')
  1589. {
  1590. return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
  1591. }
  1592. }