PageRenderTime 32ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Quản lý website bán mỹ phẩm PHP/modules/mod_cgrocket_vmproduct/common.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 182 lines | 153 code | 15 blank | 14 comment | 14 complexity | a132624f3a55ffe81e798a51e3326690 MD5 | raw file
  1. <?php
  2. /*------------------------------------------------------------------------
  3. # CGrocket Virtuemart Product Show Module
  4. # ------------------------------------------------------------------------
  5. # Author CGrocket http://www.cgrocket.com
  6. # Copyright (C) 2011 - 2012 CGrocket.com. All Rights Reserved.
  7. # @license - GNU/GPL V2 for PHP files. CSS / JS are Copyrighted Commercial
  8. # Websites: http://www.cgrocket.com
  9. -------------------------------------------------------------------------*/
  10. // no direct access
  11. defined('_JEXEC') or die('Restricted access');
  12. jimport('joomla.filter.output');
  13. jimport('joomla.filesystem.file');
  14. jimport('joomla.filesystem.folder');
  15. class modnsrocketCommonHelper {
  16. function cText($text, $limit, $type=0) {//function to cut text
  17. $text = preg_replace('/<img[^>]+\>/i', "", $text);
  18. if ($limit==0) {//no limit
  19. $allowed_tags = '<b><i><a><small><h1><h2><h3><h4><h5><h6><sup><sub><em><strong><u><br>';
  20. $text = strip_tags( $text, $allowed_tags );
  21. $text = $text;
  22. } else {
  23. if ($type==1) {//character lmit
  24. $text = JFilterOutput::cleanText($text);
  25. $sep = (strlen($text)>$limit) ? '...' : '';
  26. $text = utf8_substr($text,0,$limit) . $sep;
  27. } else {//word limit
  28. $text = JFilterOutput::cleanText($text);
  29. $text = explode(' ',$text);
  30. $sep = (count($text)>$limit) ? '...' : '';
  31. $text = implode(' ', array_slice($text,0,$limit)) . $sep;
  32. }
  33. }
  34. return $text;
  35. }
  36. function thumb($image, $width, $height, $ratio=false, $uniqid) {
  37. // remove any / that begins the path
  38. if (substr($image, 0 , 1) == '/') $image = substr($image, 1);
  39. // create a thumb filename
  40. $file_dir = dirname($image);
  41. $thumb_dir = $file_dir . DS . "nsrocket_thumbs";
  42. if (!JFolder::exists($thumb_dir)) {
  43. JFolder::create($thumb_dir);
  44. }
  45. $file_name = JFile::stripExt(basename($image));
  46. $file_ext = JFile::getExt($image);
  47. $thumb_path = $thumb_dir . DS. $file_name . '_' . $uniqid . "_thumb." . $file_ext;
  48. // check to see if this file exists, if so we don't need to create it
  49. if (function_exists("gd_info")) {
  50. //Check existing thumbnails dimensions
  51. if (file_exists($thumb_path)) {
  52. $size = GetImageSize( $thumb_path );
  53. $currentWidth=$size[0];
  54. $currentHeight=$size[1];
  55. }
  56. //Creating thumbnails
  57. if (!file_exists($thumb_path) || $currentWidth!=$width || $currentHeight!=$height ) {
  58. modnsrocketCommonHelper::crop($image, $width, $height, $ratio, $thumb_path);
  59. }
  60. }
  61. return str_replace("\\","/",$thumb_path);
  62. }
  63. protected function crop($image_to_resize, $new_width, $new_height, $ratio, $path)
  64. {
  65. if( !file_exists( $image_to_resize ) )
  66. {
  67. exit( "File " . $image_to_resize . " does not exist." );
  68. }
  69. $info = GetImageSize( $image_to_resize );
  70. if( empty( $info ) )
  71. {
  72. exit( "The file " . $image_to_resize . " doesn't seem to be an image." );
  73. }
  74. $width = $info[ 0 ];
  75. $height = $info[ 1 ];
  76. $mime = $info[ 'mime' ];/* Keep Aspect Ratio? */
  77. if( $ratio )
  78. {
  79. $thumb = ( $new_width < $width && $new_height < $height ) ? true : false;// Thumbnail
  80. $bigger_image = ( $new_width > $width || $new_height > $height ) ? true : false;// Bigger Image
  81. if( $thumb )
  82. {
  83. if( $new_width >= $new_height )
  84. {
  85. $x = ( $width / $new_width );
  86. $new_height = ( $height / $x );
  87. }
  88. elseif( $new_height >= $new_width )
  89. {
  90. $x = ( $height / $new_height );
  91. $new_width = ( $width / $x );
  92. }
  93. }
  94. elseif( $bigger_image )
  95. {
  96. if( $new_width >= $width )
  97. {
  98. $x = ( $new_width / $width );
  99. $new_height = ( $height * $x );
  100. }
  101. elseif( $new_height >= $height )
  102. {
  103. $x = ( $new_height / $height );
  104. $new_width = ( $width * $x );
  105. }
  106. }
  107. }// What sort of image?
  108. $type = substr( strrchr( $mime, '/' ), 1 );
  109. switch( $type )
  110. {
  111. case 'jpeg':
  112. $image_create_func = 'ImageCreateFromJPEG';
  113. $image_save_func = 'ImageJPEG';
  114. $new_image_ext = 'jpg';
  115. break;
  116. case 'png':
  117. $image_create_func = 'ImageCreateFromPNG';
  118. $image_save_func = 'ImagePNG';
  119. $new_image_ext = 'png';
  120. break;
  121. case 'bmp':
  122. $image_create_func = 'ImageCreateFromBMP';
  123. $image_save_func = 'ImageBMP';
  124. $new_image_ext = 'bmp';
  125. break;
  126. case 'gif':
  127. $image_create_func = 'ImageCreateFromGIF';
  128. $image_save_func = 'ImageGIF';
  129. $new_image_ext = 'gif';
  130. break;
  131. case 'vnd.wap.wbmp':
  132. $image_create_func = 'ImageCreateFromWBMP';
  133. $image_save_func = 'ImageWBMP';
  134. $new_image_ext = 'bmp';
  135. break;
  136. case 'xbm':
  137. $image_create_func = 'ImageCreateFromXBM';
  138. $image_save_func = 'ImageXBM';
  139. $new_image_ext = 'xbm';
  140. break;
  141. default: $image_create_func = 'ImageCreateFromJPEG';
  142. $image_save_func = 'ImageJPEG';
  143. $new_image_ext = 'jpg';
  144. }// New Image
  145. $image_c = ImageCreateTrueColor( $new_width, $new_height );
  146. if ($type=='png') {
  147. imagealphablending($image_c, false);
  148. imagesavealpha($image_c, true);
  149. }
  150. $new_image = $image_create_func( $image_to_resize );
  151. if ($type=='png') {
  152. imagealphablending($new_image, true);
  153. }
  154. ImageCopyResampled( $image_c, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
  155. $process = $image_save_func( $image_c, $path );
  156. return array( 'result' => $process, 'new_file_path' => $path );
  157. }
  158. }
  159. ?>