上传文件时将所有图像转换为PNG格式

时间:2018-07-05 作者:730wavy

在我的网站上,我有一个前端表单,供用户使用以下代码上载多个图像---

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++;
                    }
                }
这很好,但现在我想将文件转换为PNG,然后保存到文件夹中并删除旧文件。我找到了这个question 这表明以下代码-

imagepng(imagecreatefromstring(file_get_contents($file)), "output.png");
我不知道如何让它与我的上传虽然工作。我该怎么办?

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 );

}
}

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

(更新答案)

这个sight() 函数不仅上载图像,还创建一个附件(即attachment 类型);因此,我更新了代码—现在是function 已命名attachment_to_png, 它使用get_attached_file() 要获取附加到该附件的图像文件update_attached_file() 更新文件路径。

因此,请遵循以下步骤:

将此添加到主题的functions.php 文件:

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 ) . \'/\' . $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 );
    }
}
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

我不认为这是必要的。。所以你应该把它去掉。。。

$mfile =  wp_handle_upload($files, $upload_overrides );
UTC于2018年12月3日更新(针对单个文件上载更新)

您不应该使用$im 然后简单地使用attachment_to_png() 作用并按如下方式调用函数:attachment_to_png( 123 ) 哪里123 是附件ID。

使用media_handle_upload() 为上传的图像创建附件帖子,而不是wp_handle_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\' );

        // 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!\';
        }
    }
}
如果您百分之百确定不会使用唯一的文件名,请于2018年12月5日UTC更新;i、 e.当您上载与以前上载的图像同名的图像时,您可以创建一个回调函数,该函数将由wp_unique_filename(), 返回原始(但已清理)文件名,如下所示:

// Add this to the theme\'s functions.php
function no_unique_filename( $dir, $name, $ext ) {
    return $name;
}
然后,在$upload_overrides (请参阅以前的更新),添加unique_filename_callback 像这样:

$upload_overrides = array(
    \'test_form\'                => false,
    \'unique_filename_callback\' => \'no_unique_filename\',
);

结束

相关推荐

使用POST数据和Axios时,admin-ajax.php不起作用

我正在尝试创建一个带有AJAX请求的电子邮件注册表单。早些时候,我发现Wordpress会清空$_POST 默认情况下为变量。我试过使用admin_post 和admin_post_nopriv 但这两种方法似乎都不起作用。我现在正在尝试以下操作:add_action(\'wp_ajax_nopriv_newsletter\', \'subscribeToNewsletter\'); add_action(\'wp_ajax_newsletter\', \'subscribeToNewslette