使用MEDIA_HANDLE_UPLOAD上载图像..但是!

时间:2010-12-20 作者:AboSami

我正在尝试使用一些函数在wordpress中上传图像。。

我找到了上传图像的方法,但有一个问题。。

当用户上传图像时,wordpress会创建多个不同大小的图像,这是一个问题,因为我只想要一个图像。。

这是wp conent/uploads/2010/10文件夹。。看图片(这是一张图片,但wordpress创建的图片相同,但大小不同)。

http://i.stack.imgur.com/fn6ab.jpg

这是我的密码

<?php /*
Template Name: Uploading Page

*/?>

<?php get_header();
?><div class="container_12">
    <div id="content">
    <form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" method="POST">
    <p id="async-upload-wrap">
    <label for="async-upload">upload</label>
    <input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload">
    </p>

    <p>
    <input type="hidden" name="post_id" id="post_id" value="<?php echo \'212\';?>" />
    <?php wp_nonce_field(\'client-file-upload\'); ?>
    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" />
    </p>

    <p>
    <input type="submit" value="Save all changes" name="save" style="display: none;">
    </p>
    </form>

<?php
if ( isset( $_POST[\'html-upload\'] ) && !empty( $_FILES ) ) {
    require_once(ABSPATH . \'wp-admin/includes/admin.php\');
    $id = media_handle_upload(\'async-upload\', 1199); //post id of Client Files page
    unset($_FILES);
    if ( is_wp_error($id) ) {
        $errors[\'upload_error\'] = $id;
        $id = false;
    }

    if ($errors) {
        echo "<p>There was an error uploading your file.</p>";
    } else {
        echo "<p>Your file has been uploaded.</p>";
    }
}

get_sidebar();
get_footer();?>
怎样才能拍出一张照片?

我希望你能理解我,因为我的语言;

4 个回复
SO网友:Otto

使用wp\\u handle\\u upload()自行处理上载,无需创建附件或调整大小。

media\\u handle\\u upload()函数实际上创建了一个附件帖子,当调用wp\\u generate\\u attachment\\u元数据时,会发生调整大小的过程。如果不调用该选项,则不会进行大小调整。

SO网友:Arvind07

HTML Markup:

<p>
  <label for="custom-upload">Upload New Image:</label>

  <input type="file" tabindex="3" name="custom-upload" id="custom-upload" />
</p>
<?php
  /*Retrieving the image*/
  $attachment = get_post_meta($postid, \'custom_image\');

  if($attachment[0]!=\'\')
  {
   echo wp_get_attachment_link($attachment[0], \'thumbnail\', false, false);
  }
?>

Uploading the image:

<?php
global $post; /*Global post object*/

$post_id = $post->ID; /*Geting current post id*/
$upload = $_FILES[\'upload\']; /*Receive the uploaded image from form*/
add_custom_image($post_id, $upload); /*Call image uploader function*/

function add_custom_image($post_id, $upload)
{
 $uploads = wp_upload_dir(); /*Get path of upload dir of wordpress*/

 if (is_writable($uploads[\'path\']))  /*Check if upload dir is writable*/
 {
  if ((!empty($upload[\'tmp_name\'])))  /*Check if uploaded image is not empty*/
  {
   if ($upload[\'tmp_name\'])   /*Check if image has been uploaded in temp directory*/
   {
    $file=handle_image_upload($upload); /*Call our custom function to ACTUALLY upload the image*/

    $attachment = array  /*Create attachment for our post*/
    (
      \'post_mime_type\' => $file[\'type\'],  /*Type of attachment*/
      \'post_parent\' => $post_id,  /*Post id*/
    );

    $aid = wp_insert_attachment($attachment, $file[\'file\'], $post_id);  /*Insert post attachment and return the attachment id*/
    $a = wp_generate_attachment_metadata($aid, $file[\'file\'] );  /*Generate metadata for new attacment*/
    $prev_img = get_post_meta($post_id, \'custom_image\');  /*Get previously uploaded image*/
    if(is_array($prev_img))
    {
     if($prev_img[0] != \'\')  /*If image exists*/
     {
      wp_delete_attachment($prev_img[0]);  /*Delete previous image*/
     }
    }
    update_post_meta($post_id, \'custom_image\', $aid);  /*Save the attachment id in meta data*/

    if ( !is_wp_error($aid) ) 
    {
     wp_update_attachment_metadata($aid, wp_generate_attachment_metadata($aid, $file[\'file\'] ) );  /*If there is no error, update the metadata of the newly uploaded image*/
    }
   }
  }
  else
  {
   echo \'Please upload the image.\';
  }
 }
}

function handle_image_upload($upload)
{
 global $post;

        if (file_is_displayable_image( $upload[\'tmp_name\'] )) /*Check if image*/
        {
    /*handle the uploaded file*/
            $overrides = array(\'test_form\' => false);
            $file=wp_handle_upload($upload, $overrides);
        }
 return $file;
}
?>
SO网友:MathSmath

如果您使用的是WordPress 3。x、 转到管理>设置>媒体。

在“图像大小”下,将所有值设置为零(0),并取消选中标记为“将缩略图裁剪为精确尺寸”的框。

单击“保存更改”,WP将不再创建缩略图。

SO网友:fad.lee

为了防止WordPress在上载时为缩略图创建多个图像,我使用以下代码:

add_filter(\'intermediate_image_sizes_advanced\', \'no_image_resizing\');
    function no_image_resizing($size) {
        $ret = array();
        return $ret;
    }

结束

相关推荐