PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/system/libraries/Image_lib.php

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