$result = array(
“error” => “”,
“success” => “”,
“cover_name” => “”,
);
$allowed_types = array(‘image/jpeg’,’image/jpg’,’image/png’,’image/gif’);
if(empty($_FILES[‘upload_cover’]))
$result[‘error’] = “Please upload your image.”;
elseif(!empty($_FILES[‘upload_cover’][0])){
$result[‘error’] = “Seems that file you are uploading is currupted.”;
}
elseif(!in_array($_FILES[‘upload_cover’][‘type’], $allowed_types)){
$result[‘error’] = “Sorry! only JPEG/JPG/PNG files are allowed.”;
}
else{
/*start uploading cover image*/
$upload_dir = wp_upload_dir();

$cover_name = time().”-“.str_replace(” “, “_”, $_FILES[‘upload_cover’][‘name’]);

$final_path = $upload_dir[‘basedir’].’/custom_cover/’.$cover_name;

$cover_image = file_get_contents($_FILES[‘upload_cover’][‘tmp_name’]);

$handle = fopen($final_path, ‘w’);
fwrite($handle, $cover_image);
fclose($handle);

$image_cover = wp_get_image_editor( $final_path );
if ( ! is_wp_error( $image ) ) {
$image_cover->resize( 100, 100, false );
$thumb_path = $upload_dir[‘basedir’].’/custom_cover/thumb-‘.$cover_name;
$image_cover->save( $thumb_path );
}
$result[‘success’] = $upload_dir[‘baseurl’].’/custom_cover/’.$cover_name;
$result[‘cover_name’] = $cover_name;
}
die(json_encode($result));

Please login or Register to submit your answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.