/omg2/application/controllers/thumb2.php

https://github.com/CMDcentral/CoManD · PHP · 192 lines · 109 code · 46 blank · 37 comment · 22 complexity · 8884c0291f91d910295c1b84092b6d4f MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Thumb2 extends CI_Controller {
  3. protected $_ciDir;
  4. protected $_cacheDir;
  5. protected $_cacheKey;
  6. protected $_cacheFile;
  7. protected $_file;
  8. protected $_width;
  9. protected $_height;
  10. protected $_crop;
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->config->load('thumb');
  15. $config = $this->config->item('thumb');
  16. $this->_ciDir = $config['ciDir'];
  17. $this->_cacheDir = $_SERVER['DOCUMENT_ROOT'] . $config['cacheDir'];
  18. $this->_width = $config['width'];
  19. $this->_height = $config['height'];
  20. $this->_crop = $config['crop'];
  21. }
  22. public function view()
  23. {
  24. $uri = $this->uri->segment_array();
  25. $params = $this->uri->uri_to_assoc();
  26. if (!isset($params['src'])) $this->_error('No image supplied');
  27. $params = array_slice($params, 0, array_search('src', array_keys($params)), true); //Just get the params we're interested in
  28. if (!isset($params['w'])) $params['w'] = $this->_width;
  29. if (!isset($params['h'])) $params['h'] = $this->_height;
  30. if (!isset($params['c'])) $params['c'] = $this->_crop;
  31. $params['src'] = str_replace('../', '', $this->_ciDir . implode('/', array_slice($uri, array_search('src', $uri))));
  32. $this->_cacheKey = md5(implode('_', $params)); //Create the filename for the cached image
  33. $this->_cacheFile = $this->_cacheDir . '/' . $this->_cacheKey; //The path to the cached image
  34. $this->_file = $_SERVER['DOCUMENT_ROOT'] . $params['src']; //The path to the source image
  35. if (!file_exists($this->_file)) $this->_error('Source image does not exist');
  36. if (!is_dir($this->_cacheDir)) mkdir($this->_cacheDir, 0755); //Check for cache directory
  37. if (!file_exists($this->_cacheFile) || filemtime($this->_file) > filemtime($this->_cacheFile))
  38. {
  39. //Create cached image
  40. $cacheFile = $this->_create_image($params['w'], $params['h'], (boolean) $params['c']);
  41. }
  42. //Get some image properties
  43. $img = getimagesize($this->_cacheFile);
  44. //Serve image
  45. header ('Content-Type: ' . $img['mime']);
  46. header ('Accept-Ranges: bytes');
  47. header ('Last-Modified: ' . filemtime($this->_cacheFile));
  48. header ('Content-Length: ' . filesize($this->_cacheFile));
  49. header ('Cache-Control: max-age=9999, must-revalidate');
  50. readfile ($this->_cacheFile);
  51. exit();
  52. }
  53. /**
  54. * Check for a valid image mime type
  55. *
  56. * @param string $mime The image mime type
  57. * @return bool
  58. */
  59. protected function _valid_src_mime_type($mime)
  60. {
  61. if (preg_match("/jpg|jpeg|gif|png/i", $mime)) return true;
  62. return false;
  63. }
  64. /**
  65. * Display an error message and quit the script
  66. *
  67. * @param string $error
  68. */
  69. protected function _error($error)
  70. {
  71. header('HTTP/1.1 400 Bad Request');
  72. die($error);
  73. }
  74. /**
  75. * Resize and crop or scale an image
  76. *
  77. * @param int $srcWidth The desired width of the new image
  78. * @param int $srcHeight The desired height of the new image
  79. * @param bool $crop Whether to crop or scale the image
  80. * @return bool
  81. */
  82. protected function _create_image($newWidth, $newHeight, $crop)
  83. {
  84. //Get some image properties
  85. $src = getimagesize($this->_file);
  86. $srcWidth = $src[0];
  87. $srcHeight = $src[1];
  88. //Prevent silly dimensions
  89. if ($newWidth > $srcWidth) $newWidth = $srcWidth;
  90. if ($newHeight > $srcHeight) $newHeight = $srcHeight;
  91. if (!$this->_valid_src_mime_type($src['mime'])) $this->_error('Source is not an image');
  92. //We'll be using the CI image library
  93. $this->load->library('image_lib');
  94. ini_set('memory_limit', '128M');
  95. //If new dimensions are larger than current, do nothing
  96. if ($newWidth >= $srcWidth && $newHeight >= $srcHeight) return;
  97. $srcRatio = $srcWidth / $srcHeight;
  98. $newRatio = $newWidth / $newHeight;
  99. //Create a copy
  100. copy($this->_file, $this->_cacheFile);
  101. //Do we need to crop the image?
  102. if ($srcRatio != $newRatio && $crop == true)
  103. {
  104. //Work out which way to crop
  105. $cmpX = $srcWidth / $newWidth;
  106. $cmpY = $srcHeight / $newHeight;
  107. if ($cmpX > $cmpY)
  108. {
  109. $divider = $srcRatio / $newRatio;
  110. //Maintain height
  111. $config['width'] = floor($srcWidth / $divider);
  112. $config['x_axis'] = floor(($srcWidth - $config['width']) / 2);
  113. $config['height'] = $srcHeight;
  114. $config['y_axis'] = 0;
  115. }
  116. else
  117. {
  118. $divider = $newRatio / $srcRatio;
  119. //Maintain width
  120. $config['height'] = floor($srcHeight / $divider);
  121. $config['y_axis'] = floor(($srcHeight - $config['height']) / 2);
  122. $config['width'] = $srcWidth;
  123. $config['x_axis'] = 0;
  124. }
  125. $config['source_image'] = $this->_cacheFile;
  126. $config['maintain_ratio'] = false;
  127. $this->image_lib->initialize($config);
  128. //Crop the image
  129. if (!$this->image_lib->crop()) $this->_error($this->image_lib->display_errors());
  130. $this->image_lib->clear();
  131. }
  132. $config = array();
  133. //Set up the new image properties
  134. $config['source_image'] = $this->_cacheFile;
  135. $config['width'] = $newWidth;
  136. $config['height'] = $newHeight;
  137. $this->image_lib->initialize($config);
  138. //Resize the image
  139. if (!$this->image_lib->resize()) $this->_error($this->image_lib->display_errors());
  140. $this->image_lib->clear();
  141. //The image is ready!
  142. return true;
  143. }
  144. }
  145. /* End of file thumb.php */
  146. /* Location: ./application/controllers/thumb.php */