Last active
December 12, 2015 08:18
-
-
Save scarstens/4742952 to your computer and use it in GitHub Desktop.
Process WordPress image uploads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // now upload the new images | |
| $postvals = cp_process_new_image($_POST['ad_id']); | |
| // associate the already uploaded images to the ad and create multiple image sizes | |
| $attach_id = cp_associate_images($_POST['ad_id'], $postvals['attachment']); | |
| //process each image thats being uploaded | |
| function cp_process_new_image() { | |
| global $wpdb; | |
| $postvals = ''; | |
| for ( $i=0; $i < count( $_FILES['image']['tmp_name'] ); $i++ ) { | |
| if ( !empty($_FILES['image']['tmp_name'][$i]) ) { | |
| // rename the image to a random number to prevent junk image names from coming in | |
| $renamed = mt_rand( 1000,1000000 ).".".appthemes_find_ext( $_FILES['image']['name'][$i] ); | |
| //Hack since WP cant handle multiple uploads as of 2.8.5 | |
| $upload = array( 'name' => $renamed,'type' => $_FILES['image']['type'][$i],'tmp_name' => $_FILES['image']['tmp_name'][$i],'error' => $_FILES['image']['error'][$i],'size' => $_FILES['image']['size'][$i] ); | |
| // need to set this in order to send to WP media | |
| $overrides = array( 'test_form' => false ); | |
| // check and make sure the image has a valid extension and then upload it | |
| $file = cp_image_upload( $upload ); | |
| if ( $file ) // put all these keys into an array and session so we can associate the image to the post after generating the post id | |
| $postvals['attachment'][$i] = array( 'post_title' => $renamed,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] ); | |
| } | |
| } | |
| return $postvals; | |
| } | |
| // make sure its an image file and then upload it | |
| function cp_image_upload($upload) { | |
| if ( cp_file_is_image( $upload['tmp_name'] ) ) { | |
| $overrides = array( 'test_form' => false ); | |
| // move image to the WP defined upload directory and set correct permissions | |
| $file = wp_handle_upload( $upload, $overrides ); | |
| } | |
| return $file; | |
| } | |
| function cp_file_is_image($path) { | |
| $info = @getimagesize($path); | |
| if (empty($info)) | |
| $result = false; | |
| elseif (!in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) | |
| $result = false; | |
| else | |
| $result = true; | |
| return apply_filters('cp_file_is_image', $result, $path); | |
| } | |
| // this ties the uploaded files to the correct ad post and creates the multiple image sizes. | |
| function cp_associate_images($post_id,$file,$print = false) { | |
| $image_count = count($file); | |
| if($image_count > 0 && $print) echo __('Your ad images are now being processed...','appthemes').'<br />'; | |
| for ($i=0; $i < count($file);$i++ ) { | |
| $post_title = esc_attr( get_the_title( $post_id ) ); | |
| $attachment = array( 'post_title' => $post_title, 'post_content' => $file[$i]['post_content'], 'post_excerpt' => $file[$i]['post_excerpt'], 'post_mime_type' => $file[$i]['post_mime_type'], 'guid' => $file[$i]['guid'] ); | |
| $attach_id = wp_insert_attachment( $attachment, $file[$i]['file'], $post_id ); | |
| // create multiple sizes of the uploaded image via WP controls | |
| wp_update_attachment_metadata( $attach_id, wp_generate_attachment_metadata($attach_id, $file[$i]['file']) ); | |
| if($print) echo sprintf(__('Image number %1$d of %2$s has been processed.','appthemes'), $i+1, $image_count).'<br />'; | |
| // this only does a specific resize. | |
| // image_make_intermediate_size($file, $width, $height, $crop=false) | |
| // $crop Optional, default is false. Whether to crop image to specified height and width or resize. | |
| //wp_update_attachment_metadata($attach_id, image_make_intermediate_size($file[$i]['file'], 50, 50, true)); | |
| //wp_update_attachment_metadata($attach_id, image_make_intermediate_size($file[$i]['file'], 25, 25, true)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment