Guest User

Untitled

a guest
Jul 1st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. <?php
  2. class ModelToolImage extends Model {
  3. public function resize($filename, $width, $height) {
  4. if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) {
  5. return;
  6. }
  7.  
  8. $extension = pathinfo($filename, PATHINFO_EXTENSION);
  9.  
  10. $image_old = $filename;
  11. $image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;
  12.  
  13. if (!is_file(DIR_IMAGE . $image_new) || (filectime(DIR_IMAGE . $image_old) > filectime(DIR_IMAGE . $image_new))) {
  14. list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);
  15.  
  16. if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) {
  17. return DIR_IMAGE . $image_old;
  18. }
  19.  
  20. $path = '';
  21.  
  22. $directories = explode('/', dirname($image_new));
  23.  
  24. foreach ($directories as $directory) {
  25. $path = $path . '/' . $directory;
  26.  
  27. if (!is_dir(DIR_IMAGE . $path)) {
  28. @mkdir(DIR_IMAGE . $path, 0777);
  29. }
  30. }
  31.  
  32. if ($width_orig != $width || $height_orig != $height) {
  33. $image = new Image(DIR_IMAGE . $image_old);
  34. $image->resize($width, $height);
  35. $image->save(DIR_IMAGE . $image_new);
  36. } else {
  37. copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
  38. }
  39. }
  40.  
  41. $image_new = str_replace(' ', '%20', $image_new); // fix bug when attach image on email (gmail.com). it is automatic changing space " " to +
  42.  
  43. if ($this->request->server['HTTPS']) {
  44. return $this->config->get('config_ssl') . 'image/' . $image_new;
  45. } else {
  46. return $this->config->get('config_url') . 'image/' . $image_new;
  47. }
  48. }
  49.  
  50.  
  51. // Function to crop an image with given dimensions. What doesn/t fit will be cut off.
  52. function cropsize($filename, $width, $height) {
  53.  
  54. if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) {
  55. return;
  56. }
  57.  
  58.  
  59. $info = pathinfo($filename);
  60. $extension = $info['extension'];
  61.  
  62. $old_image = $filename;
  63. $image_new = 'cache/' . substr($filename, 0, strrpos($filename, '.')) . '-cr-' . $width . 'x' . $height . '.' . $extension;
  64.  
  65. if (!file_exists(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $image_new))) {
  66. $path = '';
  67.  
  68. $directories = explode('/', dirname(str_replace('../', '', $image_new)));
  69.  
  70. foreach ($directories as $directory) {
  71. $path = $path . '/' . $directory;
  72.  
  73. if (!file_exists(DIR_IMAGE . $path)) {
  74. @mkdir(DIR_IMAGE . $path, 0777);
  75. }
  76. }
  77.  
  78. $image = new Image(DIR_IMAGE . $old_image);
  79. $image->cropsize($width, $height);
  80. $image->save(DIR_IMAGE . $image_new);
  81. }
  82.  
  83. $image_new = str_replace(' ', '%20', $image_new); // fix bug when attach image on email (gmail.com). it is automatic changing space " " to +
  84.  
  85. if ($this->request->server['HTTPS']) {
  86. return $this->config->get('config_ssl') . 'image/' . $image_new;
  87. } else {
  88. return $this->config->get('config_url') . 'image/' . $image_new;
  89. }
  90.  
  91. }
  92.  
  93. // Function to resize image with one given max size.
  94. function onesize($filename, $maxsize) {
  95.  
  96. if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) {
  97. return;
  98. }
  99.  
  100. $info = pathinfo($filename);
  101. $extension = $info['extension'];
  102.  
  103. $old_image = $filename;
  104. $image_new = 'cache/' . substr($filename, 0, strrpos($filename, '.')) . '-max-' . $maxsize . '.' . $extension;
  105.  
  106. if (!file_exists(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $image_new))) {
  107. $path = '';
  108.  
  109. $directories = explode('/', dirname(str_replace('../', '', $image_new)));
  110.  
  111. foreach ($directories as $directory) {
  112. $path = $path . '/' . $directory;
  113.  
  114. if (!file_exists(DIR_IMAGE . $path)) {
  115. @mkdir(DIR_IMAGE . $path, 0777);
  116. }
  117. }
  118.  
  119. $image = new Image(DIR_IMAGE . $old_image);
  120. $image->onesize($maxsize);
  121. $image->save(DIR_IMAGE . $image_new);
  122. }
  123.  
  124. $image_new = str_replace(' ', '%20', $image_new); // fix bug when attach image on email (gmail.com). it is automatic changing space " " to +
  125.  
  126. if ($this->request->server['HTTPS']) {
  127. return $this->config->get('config_ssl') . 'image/' . $image_new;
  128. } else {
  129. return $this->config->get('config_url') . 'image/' . $image_new;
  130. }
  131.  
  132. }
  133. }
Add Comment
Please, Sign In to add comment