PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/mod_image_show_gk4/styles/gk_cherrydesign/class.image.php

https://gitlab.com/ppapadatis/Videolearn
PHP | 162 lines | 108 code | 5 blank | 49 comment | 36 complexity | 840760f4a83c68d0df7774584194d595 MD5 | raw file
  1. <?php
  2. /**
  3. * @author: GavickPro
  4. * @copyright: 2008-2009
  5. **/
  6. // no direct access
  7. defined('_JEXEC') or die('Restricted access');
  8. class GKIS_Coffe_Image {
  9. /*
  10. function to change file path to filename.
  11. For example:
  12. ./images/stories/demo.jpg
  13. will be translated to:
  14. stories.demo.jpg
  15. (in this situation mirror of ./images/ directory isn't necessary)
  16. */
  17. function translateName($name, $mod_id) {
  18. $name = GKIS_Coffe_Image::getRealPath($name);
  19. $start = strpos($name, DS.'images'.DS);
  20. $name = substr($name, $start+8);
  21. $ext = substr($name, -4);
  22. $name = substr($name, 0, -4);
  23. $name = str_replace(DS,'.',$name);
  24. $name .= $mod_id.$ext;
  25. return $name;
  26. }
  27. // function to change file path to real path.
  28. function getRealPath($path) {
  29. $start = strpos($path, 'images/');
  30. $path = './'.substr($path, $start);
  31. return realpath($path);
  32. }
  33. /*
  34. function to check cache
  35. this function checks if file exists in cache directory
  36. and checks if time of file life isn't too long
  37. */
  38. function checkCache($filename, $last_modification_time) {
  39. $cache_dir = JPATH_ROOT.DS.'modules'.DS.'mod_image_show_gk4'.DS.'cache'.DS;
  40. $file = $cache_dir.$filename;
  41. return (!is_file($file)) ? FALSE : (filemtime($file) > $last_modification_time);
  42. }
  43. // Creating thumbnails
  44. function createThumbnail($path, $config, $width, $height, $image_bg, $image_stretch, $quality) {
  45. if(GKIS_Coffe_Image::checkCache(GKIS_Coffe_Image::translateName($path,$config['module_id']), $config['last_modification'], $config['module_id'])){
  46. return TRUE;
  47. }else{
  48. // importing classes
  49. jimport('joomla.filesystem.file');
  50. jimport('joomla.filesystem.folder');
  51. jimport('joomla.filesystem.path');
  52. //script configuration - increase memory limit to 64MB
  53. ini_set('memory_limit', '64M');
  54. // cache dir
  55. $cache_dir = JPATH_ROOT.DS.'modules'.DS.'mod_image_show_gk4'.DS.'cache'.DS;
  56. // file path
  57. $file = GKIS_Coffe_Image::getRealPath($path);
  58. // filename
  59. $filename = GKIS_Coffe_Image::translateName($path,$config['module_id']);
  60. // Getting informations about image
  61. if(is_file($file)){
  62. $imageData = getimagesize($file);
  63. // loading image depends from type of image
  64. if($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg') $imageSource = @imagecreatefromjpeg($file);
  65. elseif($imageData['mime'] == 'image/gif') $imageSource = @imagecreatefromgif($file);
  66. else $imageSource = @imagecreatefrompng($file);
  67. // here can be exist an error when image is to big - then class return blank page
  68. // setting image size in variables
  69. $imageSourceWidth = imagesx($imageSource);
  70. $imageSourceHeight = imagesy($imageSource);
  71. // Creating blank canvas
  72. $imageBG = imagecreatetruecolor($width, $height);
  73. // If image is JPG or GIF
  74. if($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg' || $imageData['mime'] == 'image/gif') {
  75. // when bg is set to transparent - use black background
  76. if($image_bg == 'transparent'){
  77. $bgColorR = 0;
  78. $bgColorG = 0;
  79. $bgColorB = 0;
  80. }else{ // in other situation - translate hex to RGB
  81. $bg = $image_bg;
  82. if(strlen($bg) == 4) $bg = $bg[0].$bg[1].$bg[1].$bg[2].$bg[2].$bg[3].$bg[3];
  83. $hex_color = strtolower(trim($bg,'#;&Hh'));
  84. $bg = array_map('hexdec',explode('.',wordwrap($hex_color, ceil(strlen($hex_color)/3),'.',1)));
  85. $bgColorR = $bg[0];
  86. $bgColorG = $bg[1];
  87. $bgColorB = $bg[2];
  88. }
  89. // Creating color
  90. $rgb = imagecolorallocate($imageBG, $bgColorR, $bgColorG, $bgColorB);
  91. // filling canvas with new color
  92. imagefill($imageBG, 0, 0, $rgb);
  93. }else {// for PNG images
  94. $imageBG = imagecreatetruecolor($width, $height);
  95. // enable transparent background
  96. if($image_bg == 'transparent'){
  97. // create transparent color
  98. $rgb = imagecolorallocatealpha($imageBG, 0, 0, 0, 127);
  99. }else {// create normal color
  100. $bg = $image_bg;
  101. // translate hex to RGB
  102. $hex_color = strtolower(trim($bg,'#;&Hh'));
  103. $bg = array_map('hexdec',explode('.',wordwrap($hex_color, ceil(strlen($hex_color)/3),'.',1)));
  104. // creating color
  105. $rgb = imagecolorallocate($imageBG, $bg[0], $bg[1], $bg[2]);
  106. }
  107. // filling the canvas
  108. imagefill($imageBG, 0, 0, $rgb);
  109. // enabling transparent settings for better quality
  110. imagealphablending($imageBG, false);
  111. imagesavealpha($imageBG, true);
  112. }
  113. // when stretching is disabled
  114. if(!$image_stretch){
  115. // calculate ratio for first scaling
  116. $ratio = ($imageSourceWidth > $imageSourceHeight) ? $width/$imageSourceWidth : $height/$imageSourceHeight;
  117. // calculate new image size
  118. $imageSourceNWidth = $imageSourceWidth * $ratio;
  119. $imageSourceNHeight = $imageSourceHeight * $ratio;
  120. // calculate ratio for second scaling
  121. if($width > $height){
  122. if($imageSourceNHeight > $height){
  123. $ratio2 = $height / $imageSourceNHeight;
  124. $imageSourceNHeight *= $ratio2;
  125. $imageSourceNWidth *= $ratio2;
  126. }
  127. }else{
  128. if($imageSourceNWidth > $width){
  129. $ratio2 = $width / $imageSourceNWidth;
  130. $imageSourceNHeight *= $ratio2;
  131. $imageSourceNWidth *= $ratio2;
  132. }
  133. }
  134. // setting position of putting thumbnail on canvas
  135. $base_x = floor(($width - $imageSourceNWidth) / 2);
  136. $base_y = floor(($height - $imageSourceNHeight) / 2);
  137. }else{ // when stretching is disabled
  138. $imageSourceNWidth = $width;
  139. $imageSourceNHeight = $height;
  140. $base_x = 0;
  141. $base_y = 0;
  142. }
  143. // copy image
  144. imagecopyresampled($imageBG, $imageSource, $base_x, $base_y, 0, 0, $imageSourceNWidth, $imageSourceNHeight, $imageSourceWidth, $imageSourceHeight);
  145. // save image depends from MIME type
  146. if($imageData['mime'] == 'image/jpeg' || $imageData['mime'] == 'image/pjpeg' || $imageData['mime'] == 'image/jpg') imagejpeg($imageBG,$cache_dir.$filename, $quality);
  147. elseif($imageData['mime'] == 'image/gif') imagegif($imageBG, $cache_dir.$filename);
  148. else imagepng($imageBG, $cache_dir.$filename);
  149. return TRUE;
  150. }else{
  151. return FALSE;
  152. }
  153. }
  154. }
  155. }
  156. /* eof */