arie_cristianD

crop image on top center

Sep 29th, 2025
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.72 KB | None | 0 0
  1.  
  2. add_filter(
  3.     'image_resize_dimensions',
  4.     function ( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
  5.         if ( ! $crop || $dest_w <= 0 || $dest_h <= 0 ) {
  6.             return $payload;
  7.         }
  8.         $dest_ratio = $dest_w / $dest_h;
  9.         $orig_ratio = $orig_w / $orig_h;
  10.         if ( $orig_ratio > $dest_ratio ) {
  11.             $src_h = $orig_h;
  12.             $src_w = (int) round( $dest_ratio * $src_h );
  13.             $src_x = (int) floor( ( $orig_w - $src_w ) / 2 );
  14.             $src_y = 0;
  15.         } else {
  16.             $src_w = $orig_w;
  17.             $src_h = (int) round( $src_w / $dest_ratio );
  18.             $src_x = (int) floor( ( $orig_w - $src_w ) / 2 );
  19.             $src_y = 0;
  20.         }
  21.         $dst_x = 0;
  22.         $dst_y = 0;
  23.         return array( $dst_x, $dst_y, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h );
  24.     },
  25.     10,
  26.     6
  27. );
Advertisement
Add Comment
Please, Sign In to add comment