3

I'm tring to upload image in wordpress by using some function ..

I found the way to upload images but there are one problem ..

when the user upload his image , wordpress create more than one image diffrent sizes, this is a problem because I want one image only ..

this is wp-conent/uploads/2010/10 folder .. look at the picture (this is one picture but wordpress create same picture but diffrent sizes) .

https://i.sstatic.net/fn6ab.jpg

this is my code

<?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();?>

how can make it one picture?

I wish you understand me ^* because my language ><

3
  • What's the problem with the thumbnails? It's technically what you ask for but this might have just too many side-effects so I thought I ask first :)
    – hakre
    Commented Dec 20, 2010 at 16:47
  • The problem is when I uploaded a image (one image) the WP will creat more than 4 images with diffrent sizes .. I don't needed .. HOW I can cancel this function? thanks ^_^
    – AboSami
    Commented Dec 21, 2010 at 19:17
  • AboSami, if you happen to return here ( seen Dec 25 '11 :), please select one of the great answers folks gave to your question (tough decision!)...
    – brasofilo
    Commented Jul 7, 2012 at 7:57

4 Answers 4

6

Use wp_handle_upload() to handle uploads yourself, without creating attachments or resizing.

The media_handle_upload() function actually creates an attachment post, and the resizing process happens when wp_generate_attachment_metadata gets called. If you don't call that, then no resizing occurs.

5

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;
}
?>
0
3

If you're using WordPress 3.x, go to Admin > Settings > Media.

Under "Image Sizes", set all the values to zero (0), and uncheck the box labeled "Crop thumbnail to exact dimensions".

Click "save changes" and WP will no longer create thumbnails.

4
  • +1, was about to post the very same thing.. (spent the last 5-10 minutes testing uploads and intermediate sizes)..
    – t31os
    Commented Dec 20, 2010 at 19:04
  • the problem still exists .. this is the defulte sizes : $default_sizes = array('large', 'medium', 'thumbnail' ); I want the default sizes is 'thumbanil' , 'photo_author' , 'big_image' .. etc (photo_author = 20 * 20 ) , (big_image = 600 * 600 ) for example ..
    – AboSami
    Commented Dec 21, 2010 at 20:06
  • 1
    @AboSami This isn't what you asked for in your original question. You said "wordpress create more than one image diffrent sizes, this is a problem because I want one image only". If you now need to know how to ADD custom sizes, or to rename the existing sizes, that's a different question and should be posted separately. But while we're on the topic--why do you care what they're called internally? Just change medium to 20*20, and large to 600*600. Any of the standard sizes can be set to whatever width and height you want using the admin.
    – MathSmath
    Commented Dec 21, 2010 at 21:14
  • I'm sorry because my quesition is not clear .. I need to reduce the defualte sizes there are three type (large , medium , thumbnail) I want to remove 'large' and 'medium'.also I want to add just 'big-picture'.HOW? thanks mr MATHSMATH
    – AboSami
    Commented Dec 22, 2010 at 5:28
3

To prevent WordPress from creating multiple image for thumbnails while uploading, I'm using this code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.