Modify image while uploading

this is how I’ve done it

  preg_match('/\.[^\.]+$/i',$file['file']['name'],$ext);
  $name = md5(time().$user_id.rand( 5, 97));
  $name_one = $name.$ext[0];
  $name_blur = $name.'_blur'.$ext[0];
  $file['file']['name'] = $name_one;
  $upload = wp_upload_dir();
  $uploaded_file = wp_handle_upload( $file['file'], array( 'action'=> 'bp_upload_profile_bg' ) );

    //if file was not uploaded correctly
  if ( !empty($uploaded_file['error'] ) ) {
        bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $uploaded_file['error'] ), 'error' );
        return false;
    }
  $destination = $upload['path']."https://wordpress.stackexchange.com/".$name_blur;
  @copy($uploaded_file['file'], $destination);

  if ( !empty( $file['file']['type'] ) ) {
    $type = $file['file']['type'];
  } elseif( !preg_match('/(jpe?g|gif|png)$/i', $file['file']['type'] )) {
    $type = preg_match('/(jpe?g|gif|png)$/i', $file['file']['type']);
  } else {
    $type = preg_match( '/(jpe?g|gif|png)$/i', $file['file']['name'] );
  }
  if($type == 'image/jpg' || $type == 'image/jpeg') {
    $img = imagecreatefromjpeg($destination);
    imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
    imagefilter($img, IMG_FILTER_SMOOTH, -4);
    imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
    imagejpeg($img, $destination);
  } elseif($type == 'image/png') {
    $img = imagecreatefrompng($destination);
    imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
    imagefilter($img, IMG_FILTER_SMOOTH, -4);
    imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
    imagepng($img, $destination);
  } elseif($type == 'image/gif') {
    $img = imagecreatefromgif($destination);
    imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
    imagefilter($img, IMG_FILTER_SMOOTH, -4);
    imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
    imagegif($img, $destination);
  }

what I do here is I upload image normally, then copy that file with _blur added to the name and then modify that image

Hope this helps anyone.

Leave a Comment

tech