PHP将POST图像转换为PNG


PHP Convert POST image to PNG

我正在尝试将JPG(以及任何图像)转换为PNG。我有一个HTML表单,可以很好地将图像发布到服务器。我需要重命名该文件,并将其转换为PNG。在代码的后面,在我插入相关的表数据库后,我再次重命名文件,将记录ID附加到文件名中,以确保其唯一性。

比起PHP,我更像是一个客观的C程序员,所以我在这里很难处理我在其他问题中发现的代码,这些问题似乎对我不起作用

这是print_r($_FILES);

Array ( [image] => Array ( [name] => BBnL9Ho.jpg [type] => image/jpeg [tmp_name] => /tmp/phphhqHam [error] => 0 [size] => 1636 ) )

因此,我想将其转换为PNG并重命名为BBnL9Ho.jpg to image1.png。我尝试过使用以下代码,但没有用:

$newfileName = imagepng(imagecreatefromjpeg($_FILES['image']['tmp_name']), "image1.png");

稍后,在我执行相关数据库表INSERT后,我再次更改名称并附加相关数据库记录的ID(由于一对多关系,我将文件名存储在单独的表中,然后存储表单数据的其余部分):

$fileName="$lastinsertID".$newfileName;

然后,我将该名称插入正确输入的数据库中。然后我需要将文件移动到上传目录,我尝试这样做:

move_uploaded_file("$fileName",$dir . $fileName);

这就是我的问题所在。文件没有移动。当我检查文件的属性时,它似乎并没有真正转换文件。我用这个来检查类型:

$fileType = $_FILES["image"]["type"];

它仍然表明它是一个JPG。我一定错过了一些显而易见的东西,但我希望能得到一些帮助。

非常感谢。

使用以下脚本将任何图像(JPEG、PNG和GIF)转换为PNG格式。仔细阅读下面的脚本,我在每一个关键步骤都添加了注释。

// $dir specifies the directory where you upload your image files
// get the file by it's temporary name
$tmp_file_name = $_FILES['image']['tmp_name'];
// get the file extension
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
// specify the whole path here
$actual_file_name = $dir . basename($_FILES['image']['name'], "." . $ext) . ".png";
// check whether a valid image is uploaded or not
if(getimagesize($tmp_file_name)){
    // get the mime type of the uploaded image
    $image_array = getimagesize($tmp_file_name);
    $mime_type = $image_array['mime'];
    // get the height and width of the uploaded image
    list($width_orig, $height_orig) = getimagesize($tmp_file_name);
    $width = $width_orig;
    $height = $height_orig;
    if($mime_type == "image/gif"){
        // create a new true color image
        if($image_p = imagecreatetruecolor($width, $height)){
            // create a new image from file
            if($image = imagecreatefromgif($tmp_file_name)){
                // copy and resize part of an image with resampling
                if(imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)){
                    if(imagepng($image_p, $actual_file_name, 0)){
                        // image is successfully uploaded
                        // free resources
                        imagedestroy($image_p);
                        imagedestroy($image);
                        // perform the insert operation and get the last inserted id
                        // $lastinsertID = XXXX
                        // new file name
                        $filename = $dir . $lastinsertID . basename($_FILES['image']['name'], "." . $ext) . ".png";
                        //move the file to your desired location
                        if(rename($actual_file_name, $filename)){
                            echo "success";
                        }else{
                            echo "error";
                        }
                    }else{
                        //Destroy both image resource handler
                        imagedestroy($image);
                        imagedestroy($image_p);
                        echo "Error";
                    }
                }else{
                    //Destroy both image resource handlers
                    imagedestroy($image);
                    imagedestroy($image_p);
                    echo "Error";
                }
            }else{
                //destroy $image_p image resource handler
                imagedestroy($image_p);
                echo "Error";
            }
        }else{
            echo "Error";
        }
    }elseif($mime_type == "image/png"){
        // the uploaded image is already in .png format
        if(move_uploaded_file($tmp_file_name, $actual_file_name)){
            // perform the insert operation and get the last inserted id
            // $lastinsertID = XXXX
            // new file name
            $filename = $dir . $lastinsertID . $_FILES['image']['name'];
            //move the file to your desired location
            if(rename($actual_file_name, $filename)){
                echo "success";
            }else{
                echo "error";
            }
        }else{
            echo "error";
        }
    }elseif($mime_type == "image/jpeg"){
        // create a new true color image
        if($image_p = imagecreatetruecolor($width, $height)){
            // create a new image from file
            if($image = imagecreatefromjpeg($tmp_file_name)){
                // copy and resize part of an image with resampling
                if(imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)){
                    if(imagepng($image_p, $actual_file_name, 0)){
                        // image is successfully uploaded
                        // free resources
                        imagedestroy($image_p);
                        imagedestroy($image);
                        // perform the insert operation and get the last inserted id
                        // $lastinsertID = XXXX
                        // new file name
                        $filename = $dir . $lastinsertID . basename($_FILES['image']['name'], "." . $ext) . ".png";
                        //move the file to your desired location
                        if(rename($actual_file_name, $filename)){
                            echo "success";
                        }else{
                            echo "error";
                        }
                    }else{
                        //Destroy both image resource handler
                        imagedestroy($image);
                        imagedestroy($image_p);
                        echo "Error";
                    }
                }else{
                    //Destroy both image resource handlers
                    imagedestroy($image);
                    imagedestroy($image_p);
                    echo "Error";
                }
            }else{
                //destroy $image_p image resource handler
                imagedestroy($image_p);
                echo "Error";
            }
        }else{
            echo "error_An unexpected error has been occured. Please try again later.";
        }
    }else{
        echo "Only JPEG, PNG and GIF images are allowed.";
    }
}else{
    echo "Bad image format";
}