关于php:如何使用GD调整大小并将上传的图像转换为PNG?

关于php:如何使用GD调整大小并将上传的图像转换为PNG?

How do I resize and convert an uploaded image to a PNG using GD?

我想允许用户以各种格式(至少GIF,JPEG和PNG)上载化身类型的图像,但是将它们全部保存为PNG数据库BLOB。如果图像按像素大小过大,我想在插入数据库之前调整它们的大小。

使用GD进行大小调整和PNG转换的最佳方法是什么?

编辑:可悲的是,我需要使用的服务器上只有GD可用,没有ImageMagick。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php                                              
/*
Resizes an image and converts it to PNG returning the PNG data as a string
*/

function imageToPng($srcFile, $maxSize = 100) {  
    list($width_orig, $height_orig, $type) = getimagesize($srcFile);        

    // Get the aspect ratio
    $ratio_orig = $width_orig / $height_orig;

    $width  = $maxSize;
    $height = $maxSize;

    // resize to height (orig is portrait)
    if ($ratio_orig < 1) {
        $width = $height * $ratio_orig;
    }
    // resize to width (orig is landscape)
    else {
        $height = $width / $ratio_orig;
    }

    // Temporarily increase the memory limit to allow for larger images
    ini_set('memory_limit', '32M');

    switch ($type)
    {
        case IMAGETYPE_GIF:
            $image = imagecreatefromgif($srcFile);
            break;  
        case IMAGETYPE_JPEG:  
            $image = imagecreatefromjpeg($srcFile);
            break;  
        case IMAGETYPE_PNG:  
            $image = imagecreatefrompng($srcFile);
            break;
        default:
            throw new Exception('Unrecognized image type ' . $type);
    }

    // create a new blank image
    $newImage = imagecreatetruecolor($width, $height);

    // Copy the old image to the new image
    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

    // Output to a temp file
    $destFile = tempnam();
    imagepng($newImage, $destFile);  

    // Free memory                          
    imagedestroy($newImage);

    if ( is_file($destFile) ) {
        $f = fopen($destFile, 'rb');  
        $data = fread($f);      
        fclose($f);

        // Remove the tempfile
        unlink($destFile);    
        return $data;
    }

    throw new Exception('Image conversion failed.');
}

您的处理步骤应如下所示:

  • 验证文件类型
  • 使用imagecreatefrom *将图像(如果是受支持的文件类型)加载到GD中
  • 使用imagecopyresize或im??agecopyresampled调整大小
  • 使用imagepng($ handle,\\'filename.webp \\',$ quality,$ filters)保存图像
  • ImageMagick is faster, generates better images, is more configurable, and finally is (IMO) much easier to code for.

    @ceejayoz只需等待新的GD-它像MySQLi一样是面向对象的,实际上还不错:)


    类似这样的东西,也许是:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    <?php
       //Input file
       $file ="myImage.webp";
       $img = ImageCreateFromPNG($file);

       //Dimensions
       $width = imagesx($img);
       $height = imagesy($img);
       $max_width = 300;
       $max_height = 300;
       $percentage = 1;

       //Image scaling calculations
       if ( $width > $max_width ) {
          $percentage = ($height / ($width / $max_width)) > $max_height ?
               $height / $max_height :
               $width / $max_width;
       }
       elseif ( $height > $max_height) {
          $percentage = ($width / ($height / $max_height)) > $max_width ?
               $width / $max_width :
               $height / $max_height;
       }
       $new_width = $width / $percentage;
       $new_height = $height / $percentage;

       //scaled image
       $out = imagecreatetruecolor($new_width, $new_height);
       imagecopyresampled($out, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

       //output image
       imagepng($out);
    ?>

    我尚未测试代码,因此可能存在一些语法错误,但是它应该为您提供一个有关如何完成代码的公平演示。另外,我假定了一个PNG文件。您可能希望使用某种switch语句来确定文件类型。


    确定服务器上没有ImageMagick吗?

    我邀请您使用PHP(问题用PHP标记)。我使用的托管公司没有根据phpinfo()开启ImageMagick扩展。

    但是当我问他们关于它们的问题时,这里说的是可从PHP代码获得的ImageMagick程序的列表。如此简单-PHP中没有IM接口,但是我可以直接从PHP调用IM程序。

    我希望您有相同的选择。

    我非常同意-将图像存储在数据库中不是一个好主意。


    如果要使用gdlib,请使用gdlib 2或更高版本。它具有一个称为imagecopyresampled()的函数,该函数将在调整大小的同时对像素进行插值,并且外观要好得多。

    此外,我一直在网上听到有关在数据库中存储图像的错误形式的说明:

    • 访问速度比磁盘慢
    • 您的服务器将需要运行脚本来访问映像
      简单地提供文件
    • 您的脚本现在负责Web服务器使用的许多内容
      处理:

      • 设置适当的Content-Type标头
      • 设置适当的缓存/超时/ E-tag标头,以便客户端可以正确缓存图像。如果操作不正确,则会在每个请求中都击中图像服务脚本,从而进一步增加服务器上的负载。

    我能看到的唯一优点是您不需要保持数据库和图像文件同步。我仍然建议反对。


    GD是否绝对必需? ImageMagick更快,生成更好的图像,更可配置,最后(IMO)更加容易编写代码。


    phpThumb是一个高级抽象,可能值得一看。


    我认为此页面是一个很好的起点。它使用imagecreatefrom(jpeg / gif / png)调整大小并转换图像,然后输出到浏览器。除了输出浏览器外,您还可以将其输出到数据库中的BLOB,而无需花费很多代码重写时间。


    本文似乎很适合您想要的内容。您需要将保存的imagejpeg()函数更改为imagepng(),并将其保存到字符串中,而不是将其输出到页面中,但是除此之外,它应该易于复制/粘贴到现有代码中。


    推荐阅读