PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-ui/inc/class-imager.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 271 lines | 251 code | 6 blank | 14 comment | 1 complexity | edc64db41207b2f74a08478dec4aa0f8 MD5 | raw file
  1. <?php
  2. /**
  3. * Handle Images. Awesomely.
  4. *
  5. *
  6. * Inspired from the tutorial by Jarrod Oberto. Thanks Jarrod.
  7. * http://www.jarrodoberto.com/articles/2011/09/image-resizing-made-easy-with-php
  8. *
  9. * @since $Id$
  10. * @package wp-ui
  11. * @subpackage class-imager
  12. **/
  13. /**
  14. * Images main class
  15. */
  16. class wpuiImager
  17. {
  18. private $src, $info, $width, $height, $process, $options;
  19. function __construct( $file, $width, $height, $mode=auto, $format='png' )
  20. {
  21. $is_Windows = strtoupper( substr(php_uname('s'), 0, 3 )) == 'WIN';
  22. $slash = ( $is_Windows ) ? '\\' : '/';
  23. $this->info = getimagesize( $file );
  24. if ( ! is_array( $this->info ) ) return;
  25. $this->src = $this->open( $file );
  26. // echo '<pre>';
  27. // var_export($this->info);
  28. // echo '</pre>';
  29. $this->options = get_option( 'wpUI_options' );
  30. if ( ! isset( $this->options ) || ! isset( $this->options['enable_cache']))
  31. return;
  32. $this->width = imagesx( $this->src );
  33. $this->height = imagesy( $this->src );
  34. if ( ( $this->width / $this->height ) != 1 ) {
  35. $mode = 'crop';
  36. }
  37. $filestr = md5(str_replace( $slash, '', strrchr( $file, $slash ) ) . '_' . $width . '_' . $height . '_' . $mode );
  38. $cachedir = wpui_adjust_path( WP_CONTENT_DIR . '/uploads/wp-ui/cache/' );
  39. is_dir( $cachedir ) || @mkdir( $cachedir, 0755, true );
  40. $storestr = $cachedir . $filestr . '.' . $format;
  41. if ( file_exists( $storestr ) ) {
  42. $this->output( $storestr, $format );
  43. } else {
  44. $this->resize( $width, $height, $mode );
  45. $this->save( $storestr, 100, $format );
  46. $this->output( $storestr, $format );
  47. }
  48. }
  49. private function open( $file ) {
  50. $filext = $this->info[ 2 ];
  51. switch( $filext ) {
  52. case IMAGETYPE_JPEG:
  53. $image = @imagecreatefromjpeg( $file );
  54. break;
  55. case IMAGETYPE_GIF:
  56. $image = @imagecreatefromgif( $file );
  57. break;
  58. case IMAGETYPE_PNG:
  59. $image = @imagecreatefrompng( $file );
  60. break;
  61. default:
  62. $image = false;
  63. break;
  64. }
  65. return $image;
  66. }
  67. public function resize( $nWidth, $nHeight, $option='auto' ) {
  68. $options = $this->getSize( $nHeight, $nWidth, $option );
  69. $this->process = imagecreatetruecolor( $options[ 0 ], $options[ 1 ]);
  70. $this->process = $this->alpha( $this->process );
  71. imagecopyresampled( $this->process, $this->src, 0, 0, 0, 0, $options[ 0 ], $options[ 1 ], $this->width, $this->height );
  72. if ( $option == 'crop' )
  73. $this->getCrop( $options[ 0 ], $options[ 1 ], $nWidth, $nHeight );
  74. }
  75. public function getSize( $nHeight, $nWidth, $option ) {
  76. switch( $option ) {
  77. case 'exact':
  78. $width = $nWidth;
  79. $height = $nHeight;
  80. break;
  81. case 'portrait':
  82. $width = $this->getSizeFixedHeight( $nHeight );
  83. $height = $nHeight;
  84. break;
  85. case 'landscape':
  86. $width = $nWidth;
  87. $height = $this->getSizeFixedWidth( $nWidth );
  88. break;
  89. case 'auto':
  90. $autoD = $this->getSizeAuto( $nWidth, $nHeight );
  91. $width = $autoD[0];
  92. $height = $autoD[1];
  93. break;
  94. case 'crop':
  95. $opts = $this->getCropSize( $nWidth, $nHeight );
  96. $width = $opts[0];
  97. $height = $opts[1];
  98. break;
  99. }
  100. return array( $width, $height );
  101. }
  102. private function getCropSize( $nWidth, $nHeight ) {
  103. $hRatio = $this->height / $nHeight;
  104. $wRatio = $this->width / $nWidth;
  105. $ratio = ( $hRatio < $wRatio ) ? $hRatio : $wRatio;
  106. $width = $this->width / $ratio;
  107. $height = $this->height / $ratio;
  108. return array( $width, $height );
  109. }
  110. private function getSizeFixedHeight( $nHeight ) {
  111. return $nHeight * ( $this->width / $this->height );
  112. }
  113. private function getSizeFixedWidth( $nWidth ) {
  114. return $nWidth * ( $this->height / $this->width );
  115. }
  116. private function getSizeAuto( $nWidth, $nHeight ) {
  117. if ( $this->height < $this->width ) {
  118. $oWidth = $nWidth;
  119. $oHeight = $this->getSizeFixedWidth( $nWidth );
  120. } elseif ( $this->height > $this->width ) {
  121. $oWidth = $this->getSizeFixedHeight( $nHeight );
  122. $oHeight = $nHeight;
  123. } else {
  124. if ( $nHeight < $nWidth ) {
  125. $oWidth = $nWidth;
  126. $oHeight = $this->getSizeFixedWidth( $nWidth );
  127. } elseif( $nHeight > $nWidth ) {
  128. $oHeight = $nHeight;
  129. $oWidth = $this->getSizeFixedHeight( $nHeight );
  130. } else {
  131. $oWidth = $nWidth;
  132. $oHeight = $nHeight;
  133. }
  134. }
  135. return array( $oWidth, $oHeight );
  136. }
  137. private function getCrop( $width, $height, $nWidth, $nHeight ) {
  138. $cX = ( $width / 2 ) - ( $nWidth / 2 );
  139. $cY = ( $height / 2 ) - ( $nHeight / 2 );
  140. $crop = $this->process;
  141. $this->process = imagecreatetruecolor( $nWidth, $nHeight );
  142. // $this->process = $this->alpha( $this->process );
  143. imagecopyresampled( $this->process, $crop, 0, 0 , $cX, $cY, $nWidth, $nHeight, $nWidth, $nHeight );
  144. }
  145. public function alpha( $temp ) {
  146. imagealphablending( $temp, false );
  147. $alpha = imagecolorallocatealpha( $temp, 255, 0, 255, 127 );
  148. // imagecolortransparent( $temp, 0, 0, 0, 127 );
  149. imagefill( $temp, 0, 0, $alpha );
  150. imagesavealpha( $temp, true );
  151. return $temp;
  152. }
  153. public function save( $path, $quality="100", $type='png' ) {
  154. if ( $path ) {
  155. $filext = strrchr( $path, '.' );
  156. $filext = strtolower( $filext );
  157. } else {
  158. $filext = $type;
  159. }
  160. switch( $filext ) {
  161. case '.jpg':
  162. case '.jpeg':
  163. if ( imagetypes() && IMG_JPG ) {
  164. imagejpeg( $this->process, $path, $quality );
  165. }
  166. break;
  167. case '.gif':
  168. if ( imagetypes() && IMG_GIF ) {
  169. imagegif( $this->process, $path );
  170. }
  171. break;
  172. case '.png':
  173. $scaleQuality = round(( $quality/100) * 9 );
  174. $invert = 9 - $scaleQuality;
  175. // $this->process = $this->alpha( $this->process );
  176. if( imagetypes() & IMG_PNG ) {
  177. imagepng( $this->process, $path, $invert );
  178. }
  179. break;
  180. }
  181. }
  182. public function output( $file, $type ) {
  183. // $pwdir = dirname( __FILE__ ) . '/cache';
  184. // if ( ! is_dir( $pwdir ) ) {
  185. // mkdir( $pwdir, 0755, true );
  186. // }
  187. header( "Content-type: image/$type" );
  188. header( "Content-length: " . filesize($file) );
  189. readfile($file);
  190. // $this->save( false, '100', '.' . $type );
  191. exit;
  192. }
  193. }
  194. add_action( 'template_redirect', 'wpui_output_the_image' );
  195. define( 'WPUI_IMAGE_ALLOW_CROSS_DOMAIN', FALSE );
  196. function wpui_output_the_image() {
  197. $src = get_query_var( 'wpui-image' );
  198. if ( !empty( $src ) ) {
  199. if ( ( stristr( $src, site_url() ) === FALSE ) || WPUI_IMAGE_ALLOW_CROSS_DOMAIN ) {
  200. return;
  201. } else {
  202. if ( ( stripos( $src, 'http:') === 0 ) || ( stripos( $src, 'https:') === 0 ) ) {
  203. $src = addslashes( $src );
  204. $path = wpui_adjust_path( str_ireplace( site_url() . '/wp-content', WP_CONTENT_DIR, $src ) );
  205. }
  206. }
  207. $width = ( isset($_GET[ 'width' ]) ) ? $_GET[ 'width' ] : '100';
  208. $height = ( isset($_GET[ 'height' ]) ) ? $_GET[ 'height' ] : '100';
  209. $mode = ( isset($_GET[ 'mode' ]) ) ? $_GET[ 'mode' ] : 'auto';
  210. $newImg = new wpuiImager( $src, $width, $height, $mode );
  211. exit();
  212. }
  213. } // END function wpui_add_queries
  214. ?>