On my site I have a front end form for users to upload multiple images, using the following code ---
if (!empty($_FILES['vidPix']['tmp_name'][0])) {
$i = 1;
$files = $_FILES['vidPix'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("sight" . $i => $file);
add_filter( 'upload_dir', 'wpse_141088_upload_dir' );
add_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
$mfile = wp_handle_upload($files, $upload_overrides );
$newvidPix = sight("sight" . $i, $v_Id);
remove_filter( 'upload_dir', 'wpse_141088_upload_dir' );
remove_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
if ($i == 1) {
update_post_meta($v_Id, '_thumbnail_id', $newvidPix);
}
add_post_meta($v_Id, 'vid_pix', $newvidPix, false);
}
$i++;
}
}
This works fine but now I want to make it so that the files are converted to PNG, then saved to the folder and getting rid of the old file. I found this question that suggests the following code -
imagepng(imagecreatefromstring(file_get_contents($file)), "output.png");
Im not sure how to get it work with my upload though. How would I go about this?
UPDATE - FOR SINGLE FILE UPLOAD
if(isset($_POST["ebc_submit"])) {
$uploadedfile2 = $_FILES ['ebc_upload'];
if (! empty ( $uploadedfile2 ['name'] )) {
$upload_overrides = array (
'test_form' => false
);
add_filter( 'upload_dir', 'wpse_141088_upload_dir' );
add_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
$endpic = wp_handle_upload($uploadedfile2, $upload_overrides );
remove_filter( 'upload_dir', 'wpse_141088_upload_dir' );
remove_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
$im = imagecreatefromstring( file_get_contents( $endpic['file'] ) );
if ( false !== $im ) {
$filename = pathinfo( $endpic['file'], PATHINFO_FILENAME );
$to = dirname( $endpic['file'] ) . '/' . $filename . '.png';
// Creates the image and saves it in `$to`.
imagepng( $im, $to );
// Frees the image from memory.
imagedestroy( $im );
// Deletes the original file.
unlink( $endpic['file'] );
}
// Convert the image to PNG and delete the old image.
attachment_to_png( $uploadedfile2 );
}
}