使用media_handle_sideload上传时调整Wordpress图片的大小

时间:2019-01-14 20:12:01

标签: wordpress custom-wordpress-pages

我正在尝试使用media_handle_sideload函数在wordpress中上传图像。我想在媒体上载时更改图像的高度和宽度。这是下面的代码。

<?php 

  global $post;

  $all_person_results = $wpdb->get_results( "SELECT post_title,post_name FROM {$wpdb->prefix}posts", ARRAY_A );

  function _uploadImageToMediaLibrary($postID, $url, $alt = "blabla") {

   // require_once("../sites/$this->_wpFolder/wp-load.php");
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    $tmp = download_url( $url );
    $desc = $alt;
    $file_array = array();

    // Set variables for storage
    // fix file filename for query strings
    preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches);
    $file_array['name'] = basename($matches[0]);
    $file_array['tmp_name'] = $tmp;

    // If error storing temporarily, unlink
    if ( is_wp_error( $tmp ) ) {
        @unlink($file_array['tmp_name']);
        $file_array['tmp_name'] = '';
    }

    // do the validation and storage stuff
    $id = media_handle_sideload( $file_array, $postID, $desc);

    // If error storing permanently, unlink
    if ( is_wp_error($id) ) {
        @unlink($file_array['tmp_name']);
        return $id;
    }

    return $id;
}

uploadImageToMediaLibrary(12,'http://techjahid.com/img/logo.png');

 ?>

0 个答案:

没有答案