Modify image while uploading

时间:2013-12-09 作者:CBeTJlu4ok

我有一个插件,可以使用wp_upload_handler

以下是一个函数:

//handles upload, a modified version of bp_core_avatar_handle_upload(from bp-core/bp-core-avatars.php)
function handle_upload( ) {
    global $bp;

    //include core files
    require_once( ABSPATH . \'/wp-admin/includes/file.php\' );
        $max_upload_size=$this->get_max_upload_size();
        $max_upload_size=$max_upload_size*1024;//convert kb to bytes
    $file=$_FILES;

        //I am not changing the domain of erro messages as these are same as bp, so you should have a translation for this
        $uploadErrors = array(
        0 => __(\'There is no error, the file uploaded with success\', \'buddypress\'),
        1 => __(\'Your image was bigger than the maximum allowed file size of: \', \'buddypress\') . size_format($max_upload_size),
        2 => __(\'Your image was bigger than the maximum allowed file size of: \', \'buddypress\') . size_format($max_upload_size),
        3 => __(\'The uploaded file was only partially uploaded\', \'buddypress\'),
        4 => __(\'No file was uploaded\', \'buddypress\'),
        6 => __(\'Missing a temporary folder\', \'buddypress\')
    );

    if ( $file[\'error\'] ) {
        bp_core_add_message( sprintf( __( \'Your upload failed, please try again. Error was: %s\', \'buddypress\' ), $uploadErrors[$file[\'file\'][\'error\']] ), \'error\' );
        return false;
    }

    if ( ! ($file[\'file\'][\'size\']<$max_upload_size) ) {
        bp_core_add_message( sprintf( __( \'The file you uploaded is too big. Please upload a file under %s\', \'buddypress\'), size_format($max_upload_size) ), \'error\' );
        return false;
    }

    if ( ( !empty( $file[\'file\'][\'type\'] ) && !preg_match(\'/(jpe?g|gif|png)$/i\', $file[\'file\'][\'type\'] ) ) || !preg_match( \'/(jpe?g|gif|png)$/i\', $file[\'file\'][\'name\'] ) )
     {
        bp_core_add_message( __( \'Please upload only JPG, GIF or PNG photos.\', \'buddypress\' ), \'error\' );
        return false;
    }


    $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;
    }

        //assume that the file uploaded succesfully
        //delete any previous uploaded image
        self::delete_bg_for_user();
        //save in usermeta
        update_user_meta(bp_loggedin_user_id(),\'profile_bg\',$uploaded_file[\'url\']);
        update_user_meta(bp_loggedin_user_id(),\'profile_bg_file_path\',$uploaded_file[\'file\']);
        update_user_meta(bp_loggedin_user_id(),\'profile_bg_pos\',0);

        do_action(\'bppg_background_uploaded\',$uploaded_file[\'url\']);//allow to do some other actions when a new background is uploaded
    return true;
}
在上载之前,我想修改此文件。通常我想给图像添加“模糊”效果。这种结构有可能吗?

1 个回复
最合适的回答,由SO网友:CBeTJlu4ok 整理而成

我就是这样做的

  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\'].\'/\'.$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);
  }
我在这里做的是正常上传图像,然后用_blur 添加到名称,然后修改该图像

希望这对任何人都有帮助。

结束

相关推荐

子主题函数.php如何与父主题函数.php一起工作?它像css一样吗?

我正在寻求帮助,了解儿童主题和家长主题functions.php 工作我知道使用CSS时,级联会覆盖样式表中前面声明的CSS。据我所知functions.php 操作方式不同。我理解这两个functions.php 已加载。如果您想让子主题中的函数覆盖父主题中的函数,并且父主题未使用codex建议的if语句,会发生什么情况?