(Updated answer)
The sight()
function not only uploads the image, but also creates an attachment (i.e. a post of the attachment
type); hence, I updated the code — it’s now in a function
named attachment_to_png
, and that it uses the get_attached_file()
to get the image file attached to that attachment, and the update_attached_file()
to update the file path.
So please follow these steps:
-
Add this to the theme’s
functions.php
file:function attachment_to_png( $att_id ) { $file = get_attached_file( $att_id ); if ( ! $file ) { return false; } // Check MIME and make sure it's not already a PNG image. $mime = strtolower( mime_content_type( $file ) ); if ( ! preg_match( '#^image/([a-z]{3,})$#', $mime, $m ) || 'png' === $m[1] ) { return false; } $im = imagecreatefromstring( file_get_contents( $file ) ); if ( false !== $im ) { $filename = pathinfo( $file, PATHINFO_FILENAME ); $to = dirname( $file ) . "https://wordpress.stackexchange.com/" . $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( $file ); // Update the attached file. update_attached_file( $att_id, $to ); } }
-
Add this after the
remove_filter('intermediate_image_sizes_advanced', 'no_image_resizing');
:// Convert the image to PNG and delete the old image. attachment_to_png( $newvidPix );
Additional Note
I don’t think this is needed.. so you should just remove it…
$mfile = wp_handle_upload($files, $upload_overrides );
UPDATED Dec 03 2018 UTC
(Updated for the single file upload)
-
You shouldn’t use the
$im
stuff and simply use theattachment_to_png()
function. And call the function like so:attachment_to_png( 123 )
where123
is the attachment ID. -
Use
media_handle_upload()
which creates an attachment post for the uploaded image, and notwp_handle_upload()
.
So try this:
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' );
// Load media_handle_upload() and other media/image/file functions.
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
// $v_Id is the ID of the post where the image should be attached to.
$endpic = media_handle_upload( 'ebc_upload', $v_Id, array(), $upload_overrides );
remove_filter( 'upload_dir', 'wpse_141088_upload_dir' );
remove_filter( 'intermediate_image_sizes_advanced', 'no_image_resizing' );
// Convert the image to PNG and delete the old image.
if ( $endpic && ! is_wp_error( $endpic ) ) {
attachment_to_png( $endpic );
//echo 'Success!';
}
}
}
UPDATED Dec 05 2018 UTC
If you’re 100% sure about not going to be using unique file names; i.e. when you upload an image with the same name as a previously uploaded image, then you can create a callback function which will be used by wp_unique_filename()
, where you return the original (but sanitized) file name, like so:
// Add this to the theme's functions.php
function no_unique_filename( $dir, $name, $ext ) {
return $name;
}
And then, in the $upload_overrides
(see previous update), add unique_filename_callback
like so:
$upload_overrides = array(
'test_form' => false,
'unique_filename_callback' => 'no_unique_filename',
);