Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1. //Images resize on upload handling
  2. add_action('pmxi_gallery_image', 'my_gallery_image', 10, 3);
  3.  
  4. function my_gallery_image($pid, $attid, $image_filepath) {
  5.     $attachment = get_post($attid);
  6.     $square=500;
  7.  
  8.  
  9.    // Load up the original image
  10.     //get extension
  11.     $ext = pathinfo($image_filepath, PATHINFO_EXTENSION);
  12.         if($ext == 'png'){
  13.         $src = imagecreatefrompng($image_filepath);
  14.         }
  15.         else{
  16.         $src = imagecreatefromjpeg($image_filepath);
  17.         }
  18.  
  19.    $w = imagesx($src); // image width
  20.    $h = imagesy($src); // image height
  21.  
  22.    // Create output canvas and fill with white
  23.    $final = imagecreatetruecolor($square,$square);
  24.    $bg_color = imagecolorallocate ($final, 255, 255, 255);
  25.    imagefill($final, 0, 0, $bg_color);
  26.  
  27.    // Check if portrait or landscape
  28.    if($h>=$w){
  29.       // Portrait, i.e. tall image
  30.    
  31.       $newh=$square * 0.9; // 0.9 For the padding
  32.       $neww=intval($square*$w/$h)* 0.9; // 0.9 For the padding;
  33.       // Resize and composite original image onto output canvas
  34.       imagecopyresampled(
  35.          $final, $src,
  36.          intval(($square-$neww)/2),intval(($square-$newh)/2),
  37.          0,0,
  38.          $neww, $newh,
  39.          $w, $h);
  40.    } else {
  41.       // Landscape, i.e. wide image
  42.       $neww=$square *0.9; // 0.9 for the padding
  43.       $newh=intval($square*$h/$w) *0.9;// 0.9 for the padding
  44.       imagecopyresampled(
  45.          $final, $src,
  46.          intval(($square-$neww)/2),intval(($square-$newh)/2),
  47.          0,0,
  48.          $neww, $newh,
  49.          $w, $h);
  50.    }
  51.  
  52.    // Write result
  53.     //get extension
  54.         if($ext == 'png'){
  55.            imagepng($final,$image_filepath);
  56.         }
  57.         else{
  58.            imagejpeg($final,$image_filepath);
  59.         }
  60.     // do something with $attachment image
  61. exec('wpcli --allow-root media regenerate '.$attid );
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement