即时上传并最小化客户端的大小

时间:2015-09-12 02:45:18

标签: php jquery wordpress image

所以,我有以下代码用于上传图片:

<?php
if ($_FILES) {
  foreach ($_FILES as $file => $array) {
  $newupload = insert_attachment($file,$pid);
 }
}
?>
<div class="rh_image_upload">
    <input type="file" name="rh_image" id="rh_image" size="50">
</div>
<p align="right">
    <input type="submit" value="Publish" tabindex="6" id="submit" name="submit" />
</p>
<input type="hidden" name="post_type" id="post_type" value="post" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>

functions.php

//Upload items
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

$attach_id = media_handle_upload( $file_handler, $post_id );

if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
//Upload item done

问题:

因此,在单击提交按钮之前,不会发生图像上传。 如果图像尺寸很小但文件大小很大,则需要更长的时间。

  1. 那么,一旦选择了图像(但尚未提交),有没有办法开始上传过程?

  2. 另外,我如何限制&#34;图像&#34;的文件类型?仅限文件?

  3. 最后,有没有办法在提交帖子之前缩小选定的图片尺寸?

  4. 谢谢你们!

1 个答案:

答案 0 :(得分:0)

  
      
  1. 那么,一旦选择了图像(但尚未提交),有没有办法开始上传过程?
  2.   

尝试使用change事件,$.post()将用户选择的文件上传到服务器

  
      
  1. 另外,我如何限制&#34;图像&#34;的文件类型?仅限文件?
  2.   

accepts元素

处将"image/*"属性设置为input
<input type="file" accepts="image/*" name="rh_image" id="rh_image" size="50">
  
      
  1. 最后,有没有办法在提交帖子之前缩小选定的图片尺寸?
  2.   

创建新的img元素,设置widthheight属性?重新调整大小后,使用canvas元素返回图像的data URI