PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/MY_Image_lib.php

https://gitlab.com/fcupen/PHP
PHP | 177 lines | 87 code | 25 blank | 65 comment | 27 complexity | 885c993a5c574151a1e2f3251e7c4ef2 MD5 | raw file
  1. <?php
  2. /**
  3. * @name CodeIgniter Advanced Images
  4. * @author Jens Segers
  5. * @link http://www.jenssegers.be
  6. * @license MIT License Copyright (c) 2012 Jens Segers
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. if (!defined('BASEPATH'))
  27. exit('No direct script access allowed');
  28. class MY_Image_lib extends CI_Image_lib {
  29. var $user_width = 0;
  30. var $user_height = 0;
  31. var $user_x_axis = '';
  32. var $user_y_axis = '';
  33. /**
  34. * Initialize image preferences
  35. *
  36. * @access public
  37. * @param array
  38. * @return bool
  39. */
  40. function initialize($props = array()) {
  41. // save user specified dimensions and axis positions before they are modified by the CI library
  42. if (isset($props["width"])) {
  43. $this->user_width = $props["width"];
  44. }
  45. if (isset($props["height"])) {
  46. $this->user_height = $props["height"];
  47. }
  48. if (isset($props["x_axis"])) {
  49. $this->user_x_axis = $props["x_axis"];
  50. }
  51. if (isset($props["y_axis"])) {
  52. $this->user_y_axis = $props["y_axis"];
  53. }
  54. return parent::initialize($props);
  55. }
  56. /**
  57. * Initialize image properties
  58. *
  59. * Resets values in case this class is used in a loop
  60. *
  61. * @access public
  62. * @return void
  63. */
  64. function clear() {
  65. $this->user_width = 0;
  66. $this->user_height = 0;
  67. $this->user_x_axis = '';
  68. $this->user_y_axis = '';
  69. return parent::clear();
  70. }
  71. /**
  72. * Smart resize and crop function
  73. *
  74. * @access public
  75. * @return bool
  76. */
  77. function fit() {
  78. // overwrite the dimensions with the original user specified dimensions
  79. $this->width = $this->user_width;
  80. $this->height = $this->user_height;
  81. // we will calculate the sizes ourselves
  82. $this->maintain_ratio = FALSE;
  83. // ------------------------------------------------------------------------------------------
  84. // mode 1: auto-scale the image to fit 1 dimension
  85. // ------------------------------------------------------------------------------------------
  86. if ($this->user_width == 0 || $this->user_height == 0) {
  87. // calculate missing dimension
  88. if ($this->user_width == 0) {
  89. $this->width = ceil($this->user_height * $this->orig_width / $this->orig_height);
  90. } else {
  91. $this->height = ceil($this->user_width * $this->orig_height / $this->orig_width);
  92. }
  93. // no cropping is needed, just resize
  94. return $this->resize();
  95. }
  96. // ------------------------------------------------------------------------------------------
  97. // mode 2: resize and crop the image to fit both dimensions
  98. // ------------------------------------------------------------------------------------------
  99. $this->width = ceil($this->user_height * $this->orig_width / $this->orig_height);
  100. $this->height = ceil($this->user_width * $this->orig_height / $this->orig_width);
  101. if (($this->user_width != $this->width) && ($this->user_height != $this->height)) {
  102. if ($this->master_dim == 'height') {
  103. $this->width = $this->user_width;
  104. } else {
  105. $this->height = $this->user_height;
  106. }
  107. }
  108. // save dynamic output for last
  109. $dynamic_output = $this->dynamic_output;
  110. $this->dynamic_output = FALSE;
  111. // if dynamic output is requested we will use a temporary file to work on
  112. $tempfile = FALSE;
  113. if ($dynamic_output) {
  114. $temp = tmpfile();
  115. $tempfile = array_search('uri', @array_flip(stream_get_meta_data($temp)));
  116. $this->full_dst_path = $tempfile;
  117. }
  118. // resize stage
  119. if (!$this->resize()) {
  120. return FALSE;
  121. }
  122. // axis settings
  123. if (!is_numeric($this->user_x_axis)) {
  124. $this->x_axis = floor(($this->width - $this->user_width) / 2);
  125. } else {
  126. $this->x_axis = $this->user_x_axis;
  127. }
  128. if (!is_numeric($this->user_y_axis)) {
  129. $this->y_axis = floor(($this->height - $this->user_height) / 2);
  130. } else {
  131. $this->y_axis = $this->user_y_axis;
  132. }
  133. // cropping options
  134. $this->orig_width = $this->width;
  135. $this->orig_height = $this->height;
  136. $this->width = $this->user_width;
  137. $this->height = $this->user_height;
  138. // use the previous generated image for output
  139. $this->full_src_path = $this->full_dst_path;
  140. // reset dynamic output to initial value
  141. $this->dynamic_output = $dynamic_output;
  142. // cropping stage
  143. if (!$this->crop()) {
  144. return FALSE;
  145. }
  146. // close (and remove) the temporary file
  147. if ($tempfile) {
  148. fclose($temp);
  149. }
  150. return TRUE;
  151. }
  152. }