PHP将POST图像转换为PNG

时间:2015-12-23 17:12:50

标签: php png php-gd

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

我更像是一个客观的C程序员,然后是PHP,所以我在这里遇到了这个代码,我在其他问题中发现这些代码似乎对我不起作用。

以下是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。我必须遗漏一些非常明显的东西,但我会感激一些帮助。

非常感谢。

1 个答案:

答案 0 :(得分:1)

使用以下脚本将任何图像(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";
}