title: How create image with a blur image background in php
body: <p>Im trying to create with php a 4:3 image with any image uploaded by user. No matter the image original size, i want to fill the background with a blurred copy of the same image.</p>
  
  <p>This is the code i use (from <a href="https://stackoverflow.com/questions/4478223/how-do-i-fill-white-background-while-resize-image/4478236#4478236">István Ujj-Mészáros</a>):</p>
  
  <pre><code>function resize($source_image, $destination, $tn_w, $tn_h, $quality = 90) {
  
      $info = getimagesize($source_image);
      $imgtype = image_type_to_mime_type($info[2]);
  
      #assuming the mime type is correct
      switch ($imgtype) {
          case 'image/jpeg':
              $source = imagecreatefromjpeg($source_image);
              break;
          case 'image/gif':
              $source = imagecreatefromgif($source_image);
              break;
          case 'image/png':
              $source = imagecreatefrompng($source_image);
              break;
          default:
              die('Invalid image type.');
      }
  
      #Figure out the dimensions of the image and the dimensions of the desired thumbnail
      $src_w = imagesx($source);
      $src_h = imagesy($source);
  
      #Do some math to figure out which way we'll need to crop the image
      #to get it proportional to the new size, then crop or adjust as needed
      $x_ratio = $tn_w / $src_w;
      $y_ratio = $tn_h / $src_h;
  
      if (($src_w &lt;= $tn_w) &amp;&amp; ($src_h &lt;= $tn_h)) {
          $new_w = $src_w;
          $new_h = $src_h;
      } elseif (($x_ratio * $src_h) &lt; $tn_h) {
          $new_h = ceil($x_ratio * $src_h);
          $new_w = $tn_w;
      } else {
          $new_w = ceil($y_ratio * $src_w);
          $new_h = $tn_h;
      }
  
      $newpic = imagecreatetruecolor(round($new_w), round($new_h));
      imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
      $final = imagecreatetruecolor($tn_w, $tn_h);
  
      // This code fill with green color
      //$backgroundColor = imagecolorallocate($final, 0, 255, 0);
      //imagefill($final, 0, 0, $backgroundColor);
      imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);
  
      // This code generates a blurred image
      # ****************************************************
      //for ($x=1; $x &lt;=2; $x++){
      //    imagefilter($final, IMG_FILTER_GAUSSIAN_BLUR, 999);
      //} 
      //imagefilter($final, IMG_FILTER_SMOOTH,99);
      //imagefilter($final, IMG_FILTER_BRIGHTNESS, 10);  
      # ****************************************************
  
      if (imagejpeg($final, $destination, $quality)) {
          return true;
      }
      return false;
  }
  
  // targetFilePath contains the folder an filename
  resize($targetFilePath,$targetFilePath,640,480,90);
  </code></pre>
  
  <p>The result is like this image:
  <a href="https://i.stack.imgur.com/un1mO.jpg" rel="nofollow noreferrer">my result until now</a></p>
  
  <p>What do i need?
  <a href="https://i.stack.imgur.com/wDm70.png" rel="nofollow noreferrer">The result that i hope</a></p>
  
  <p>Please any idea will be welcome.
  Thank you in advance!!!</p>