PHP 将 POST 图像转换为 PNG
PHP Convert POST image to PNG
我正在尝试将 JPG(以及任何图像)转换为 PNG。我有一个 HTML 表单,可以将图像很好地发布到服务器。我需要重命名该文件并将其转换为 PNG。稍后在我的代码中,在执行相关的 table 数据库插入后,我再次重命名文件以将记录 ID 附加到文件名以确保其唯一性。
我更像是一个 objective 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");
之后我做了一个相关的数据库 table INSERT,我再次更改名称并附加相关数据库记录的 ID(我将文件名存储在单独的 table 然后是表格数据的其余部分由于一对多关系):
$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";
}
我正在尝试将 JPG(以及任何图像)转换为 PNG。我有一个 HTML 表单,可以将图像很好地发布到服务器。我需要重命名该文件并将其转换为 PNG。稍后在我的代码中,在执行相关的 table 数据库插入后,我再次重命名文件以将记录 ID 附加到文件名以确保其唯一性。
我更像是一个 objective 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");
之后我做了一个相关的数据库 table INSERT,我再次更改名称并附加相关数据库记录的 ID(我将文件名存储在单独的 table 然后是表格数据的其余部分由于一对多关系):
$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";
}