在PHP中将JPG/GIF图像转换为PNG ?

时间:2020-12-30 21:21:31

Possible Duplicate of
Convert jpg image to gif, png & bmp format using PHP

可能的重复转换jpg图像到gif, png和bmp格式使用PHP

I have a PHP form that allows image uploads and checks exif_imagetype(); to make sure an image is valid.

我有一个PHP表单,允许图像上传和检查exif_imagetype();确保图像是有效的。

However, I want all formats, PNG, JPG, JPEG, and GIF, to end up being PNG once submitted.

但是,我希望所有的格式,PNG, JPG, JPEG和GIF,一旦被提交,最终都是PNG。

How can I go about doing this?

我该怎么做呢?

4 个解决方案

#1


83  

You just need imagepng() then. In fact it almost becomes a one-liner:

您只需要imagepng()。事实上,它几乎成了一句俏皮话:

 imagepng(imagecreatefromstring(file_get_contents($filename)), "output.png");

You would use $_FILES["id"]["tmp_name"] for the filename, and a different output filename obviously. But the image format probing itself would become redundant.

您将使用$_FILES["id"]["tmp_name"]作为文件名,显然使用不同的输出文件名。但是图像格式探测本身将变得多余。

#2


8  

Based on what kind of image it is you could select the correct function to open the file:

根据是哪种图像,您可以选择正确的函数打开文件:

$extension = pathinfo($filename, PATHINFO_EXTENSION); 
switch ($extension) {
    case 'jpg':
    case 'jpeg':
       $image = imagecreatefromjpeg($filename);
    break;
    case 'gif':
       $image = imagecreatefromgif($filename);
    break;
    case 'png':
       $image = imagecreatefrompng($filename);
    break;
}

Then you just save the file using:

然后使用以下方法保存文件:

imagepng($image, $new_filename);

#3


2  

<form method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
if(isset($_POST['submit']))
{
    if(exif_imagetype($_FILES['image']['tmp_name']) ==  IMAGETYPE_GIF) 
    {
        $newpng = 'image.png';
        $png = imagepng(imagecreatefromgif($_FILES['image']['tmp_name']), $newpng);
    }
    elseif(exif_imagetype($_FILES['image']['tmp_name']) ==  IMAGETYPE_JPEG) 
    {
        $newpng = 'image.png';
        $png = imagepng(imagecreatefromjpeg($_FILES['image']['tmp_name']), $newpng);
    }
    else //already png
    {
        $newpng = 'image.png';
    }       
}
?>

#4


1  

Very simple using the gd functions:

使用gd函数非常简单:

switch (exif_imagetype($image)) {
    case IMAGETYPE_GIF :
        $img = imagecreatefromgif($image);
        break;
    case IMAGETYPE_JPEG :
        $img = imagecreatefromjpeg($image);
        break;
    default :
        throw new InvalidArgumentException('Invalid image type');
}

imagepng($img, $filename);

For conciseness this obviously doesn't handle the case if the image is already a PNG.

对于简洁性而言,如果图像已经是PNG,则显然不能处理这个问题。

#1


83  

You just need imagepng() then. In fact it almost becomes a one-liner:

您只需要imagepng()。事实上,它几乎成了一句俏皮话:

 imagepng(imagecreatefromstring(file_get_contents($filename)), "output.png");

You would use $_FILES["id"]["tmp_name"] for the filename, and a different output filename obviously. But the image format probing itself would become redundant.

您将使用$_FILES["id"]["tmp_name"]作为文件名,显然使用不同的输出文件名。但是图像格式探测本身将变得多余。

#2


8  

Based on what kind of image it is you could select the correct function to open the file:

根据是哪种图像,您可以选择正确的函数打开文件:

$extension = pathinfo($filename, PATHINFO_EXTENSION); 
switch ($extension) {
    case 'jpg':
    case 'jpeg':
       $image = imagecreatefromjpeg($filename);
    break;
    case 'gif':
       $image = imagecreatefromgif($filename);
    break;
    case 'png':
       $image = imagecreatefrompng($filename);
    break;
}

Then you just save the file using:

然后使用以下方法保存文件:

imagepng($image, $new_filename);

#3


2  

<form method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
if(isset($_POST['submit']))
{
    if(exif_imagetype($_FILES['image']['tmp_name']) ==  IMAGETYPE_GIF) 
    {
        $newpng = 'image.png';
        $png = imagepng(imagecreatefromgif($_FILES['image']['tmp_name']), $newpng);
    }
    elseif(exif_imagetype($_FILES['image']['tmp_name']) ==  IMAGETYPE_JPEG) 
    {
        $newpng = 'image.png';
        $png = imagepng(imagecreatefromjpeg($_FILES['image']['tmp_name']), $newpng);
    }
    else //already png
    {
        $newpng = 'image.png';
    }       
}
?>

#4


1  

Very simple using the gd functions:

使用gd函数非常简单:

switch (exif_imagetype($image)) {
    case IMAGETYPE_GIF :
        $img = imagecreatefromgif($image);
        break;
    case IMAGETYPE_JPEG :
        $img = imagecreatefromjpeg($image);
        break;
    default :
        throw new InvalidArgumentException('Invalid image type');
}

imagepng($img, $filename);

For conciseness this obviously doesn't handle the case if the image is already a PNG.

对于简洁性而言,如果图像已经是PNG,则显然不能处理这个问题。