PHP将POST图像转换为PNG

PHP将POST图像转换为PNG,php,png,php-gd,Php,Png,Php Gd,我正在尝试将JPG(任何图像)转换为PNG。我有一个HTML表单,可以将图像发布到服务器上。我需要重命名该文件,并将其转换为PNG。稍后在代码中,在执行相关表数据库插入之后,我再次重命名该文件,以将记录ID附加到文件名中,以确保其唯一性 我更像是一个目标C程序员,而不是PHP程序员,所以我在这里努力编写我在其他问题中发现的代码,这些代码似乎不适合我 这里是print\r($\u文件) Array([image]=>Array([name]=>BBnL9Ho.jpg[type]=>image/jp

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

我更像是一个目标C程序员,而不是PHP程序员,所以我在这里努力编写我在其他问题中发现的代码,这些代码似乎不适合我

这里是
print\r($\u文件)

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

因此,我想将其转换为PNG,并将
BBnL9Ho.jpg重命名为image1.PNG
。我已尝试使用以下代码,但无效:

$newfileName = imagepng(imagecreatefromjpeg($_FILES['image']['tmp_name']), "image1.png");
稍后,在插入相关数据库表之后,我再次更改名称并附加相关数据库记录的ID(由于一对多关系,我将文件名存储在单独的表中,然后存储表单数据的其余部分):

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

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";
}   

哇,太棒了!非常感谢。我将致力于实施,并会回复您。非常感谢你!我可能要花几天时间才能恢复原状。@Eynol先生,不客气。让我知道进展情况。好吧,我试了一下,它走得更远了。它将图像转换为PNG!!:)。它还将新图像名称存储在数据库中,但它已将我希望将其移动到的目录名附加到数据库中的文件名。此外,它没有将文件移动到我的$dir位置。最后,我希望生成的图像是(lastinsertID)image1.png(例如12345image1.png)。@mreynol您可以这样做,
$lastinsertID。basename($actual_file_name)
获取更新的文件名。@mreynol如果我是你,我会创建一个生成随机数的函数。当用户上传多张图片时,我会为每张图片加上一个随机数,并加上一个简单的文件名,
random\u number。“image.png”
并在数据库中插入新文件名(
随机数。“image.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";
}