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

/wp-content/plugins/post-thumbnail-editor/php/class-pte-image-editor-gd.php

https://github.com/pica-design/nextstepmaine
PHP | 81 lines | 49 code | 7 blank | 25 comment | 8 complexity | fc56fdda1a8a9fef9d393abf39e9ef73 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, GPL-3.0
  1. <?php
  2. class PTE_Image_Editor_GD extends WP_Image_Editor_GD {
  3. /**
  4. * Crops Image.
  5. *
  6. * @since 3.5.0
  7. * @access public
  8. *
  9. * @param string|int $src The source file or Attachment ID.
  10. * @param int $src_x The start x position to crop from.
  11. * @param int $src_y The start y position to crop from.
  12. * @param int $src_w The width to crop.
  13. * @param int $src_h The height to crop.
  14. * @param int $dst_w Optional. The destination width.
  15. * @param int $dst_h Optional. The destination height.
  16. * @param boolean $src_abs Optional. If the source crop points are absolute.
  17. * @return boolean|WP_Error
  18. */
  19. public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
  20. $ar = $src_w / $src_h;
  21. $dst_ar = $dst_w / $dst_h;
  22. if ( isset( $_GET['pte-fit-crop-color'] ) && abs( $ar - $dst_ar ) > 0.01 ){
  23. PteLogger::debug( sprintf( "AR: '%f'\tOAR: '%f'", $ar, $dst_ar ) );
  24. // Crop the image to the correct aspect ratio...
  25. if ( $dst_ar > $ar ) { // constrain to the dst_h
  26. $tmp_dst_h = $dst_h;
  27. $tmp_dst_w = $dst_h * $ar;
  28. $tmp_dst_y = 0;
  29. $tmp_dst_x = ($dst_w / 2) - ($tmp_dst_w / 2);
  30. }
  31. else {
  32. $tmp_dst_w = $dst_w;
  33. $tmp_dst_h = $dst_w / $ar;
  34. $tmp_dst_x = 0;
  35. $tmp_dst_y = ($dst_h / 2) - ($tmp_dst_h / 2);
  36. }
  37. // copy $this->image unto a new image with the right width/height.
  38. $img = wp_imagecreatetruecolor( $dst_w, $dst_h );
  39. if ( function_exists( 'imageantialias' ) )
  40. imageantialias( $img, true );
  41. if ( preg_match( "/^#[a-fA-F0-9]{6}$/", $_GET['pte-fit-crop-color'] ) ) {
  42. $c = self::getRgbFromHex( $_GET['pte-fit-crop-color'] );
  43. $color = imagecolorallocate( $img, $c[0], $c[1], $c[2] );
  44. }
  45. else {
  46. PteLogger::debug( "setting transparent/white" );
  47. //$color = imagecolorallocate( $img, 100, 100, 100 );
  48. $color = imagecolorallocatealpha( $img, 255, 255, 255, 127 );
  49. }
  50. imagefilledrectangle( $img, 0, 0, $dst_w, $dst_h, $color );
  51. imagecopyresampled( $img, $this->image,
  52. $tmp_dst_x, $tmp_dst_y, $src_x, $src_y,
  53. $tmp_dst_w, $tmp_dst_h, $src_w, $src_h
  54. );
  55. if ( is_resource( $img ) ) {
  56. imagedestroy( $this->image );
  57. $this->image = $img;
  58. $this->update_size();
  59. return true;
  60. }
  61. return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file );
  62. }
  63. return parent::crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
  64. }
  65. /**
  66. * Pulled from: http://php.net/manual/en/ref.image.php
  67. *
  68. * print_r( getRgbFromHex( "#FFFFFF" ) )
  69. * --> // Output: Array ( [0] => 255 [1] => 255 [2] => 255 )
  70. */
  71. public static function getRgbFromHex($color_hex) {
  72. return array_map( 'hexdec', explode( '|', wordwrap( substr( $color_hex, 1 ), 2, '|', 1 ) ) );
  73. }
  74. }