Created
July 17, 2013 22:29
-
-
Save RadGH/6025129 to your computer and use it in GitHub Desktop.
Upload and process an image in Wordpress
This file contains 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
<?php | |
$image = abjj_upload_image('profile_picture', $existing_image, 'abjj_process_profile_picture' ); | |
echo '<pre style="font-size: 11px;">RESULT: ---------------------------', "\n", | |
htmlspecialchars( print_r( $image, true)), | |
'</pre>'; | |
exit; | |
// Other functions | |
// Either process and upload a new image, or retrieve the old image | |
// $process is a callback that allows the image to be processed once uploaded via Wordpress' image editor | |
// $process( $img_editor, $the_upload ) | |
// Note: Be sure to use $the_upload by reference to affect the uploaded image. Update any parameters that change during the processing! | |
function abjj_upload_image( $FILES_name, $old_image = false, $process = false ) { | |
$image = false; | |
// Start with the old image if available: | |
if (isset($old_image)) $image = $old_image; | |
// Check if old image should have been deleted: | |
// Check if the delete box was checked | |
if ( | |
isset($_POST[$FILES_name . '-delete']) | |
&& isset($_POST[$FILES_name . '-delete']['checkbox']) | |
) { | |
// Return nothing, as we don't want to store an image anymore | |
return false; | |
} | |
// Check if new image was uploaded: | |
if ( | |
isset($_FILES) | |
&& isset($_FILES[$FILES_name]) | |
&& isset($_FILES[$FILES_name]['error']) | |
&& $_FILES[$FILES_name]['error'] != 4 // File was not uploaded | |
): | |
// File was uploaded, begin processing | |
$the_upload = wp_handle_upload( $_FILES[$FILES_name], array('test_form' => false) ); | |
if ( // Ignore this unknown error that can occur sometimes | |
!isset($the_upload['error']) | |
|| $the_upload['error'] != 'Specified file failed upload test.' | |
) { | |
// Check if the upload contains a file, which means success! | |
if ( isset($the_upload['file']) ) { | |
// Add some more wonderful options to the uploaded file | |
$the_upload['filename'] = basename($the_upload['file']); | |
$the_upload['filesize'] = filesize($the_upload['file']); | |
$the_upload['is_image'] = true; | |
$the_upload['width'] = false; | |
$the_upload['height'] = false; | |
$img_editor = wp_get_image_editor( $the_upload['file'] ); | |
if ( $img_editor ) { | |
$image_size = $img_editor->get_size(); | |
if ( isset($image_size['width']) ) | |
$the_upload['width'] = $image_size['width']; | |
if ( isset($image_size['height']) ) | |
$the_upload['height'] = $image_size['height']; | |
} | |
if ( $process !== false && function_exists($process) ) { | |
// Before we start processing, store our original image in an index called "original" | |
$the_upload['original'] = $the_upload; | |
$process( $img_editor, $the_upload ); | |
} | |
// The upload went OK, save our results | |
$image = $the_upload; | |
} | |
} | |
endif; | |
// End of handling upload | |
return $image; | |
} | |
// Resize image to a maximum of 225x200, Preserve aspect ratio, Do not scale up | |
// Save as jpg 80% | |
function abjj_process_profile_picture( &$editor, &$upload ) { | |
$w = $upload['width']; | |
$h = $upload['height']; | |
$max_w = 225; | |
$max_h = 200; | |
if ($w > 225 || $h > 200) { | |
$is_landscape = ($max_w / $w) < ($max_h / $h); | |
$ratio = $is_landscape ? $max_w / $w : $max_h / $h; | |
$w = round($w * $ratio); | |
$h = round($h * $ratio); | |
$editor->resize( $w, $h, true ); | |
} | |
// Strip the trailing extension from the name | |
/* | |
//$upload_path = str_replace($upload['filename'], '', $upload['file']); | |
$new_name = preg_replace('/\.[^\.]+$/', '', $upload['filename']); | |
$new_name = $new_name . '-' . uniqid() . '.jpg'; | |
*/ | |
$upload_path = dirname( $upload['file'] ); | |
$new_name = $editor->generate_filename( uniqid(), $upload_path, 'jpg' ); | |
$editor->set_quality( 80 ); | |
$result = $editor->save( $new_name, 'image/jpeg' ); | |
if ($result) { | |
$upload_dir = wp_upload_dir(); | |
$upload['width'] = $w; | |
$upload['height'] = $h; | |
$upload['file'] = $new_name; | |
$upload['filename'] = basename($new_name); | |
$upload['filesize'] = filesize($new_name); | |
$upload['type'] = 'image/jpeg'; | |
$upload['url'] = $upload_dir['url'] . '/' . basename($new_name); | |
$upload['processed'] = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment