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

时间:2020-03-05 18:41:59  来源:igfitidea点击:

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

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

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

解决方案

回答

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

回答

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

回答

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

回答

如果要使用gdlib,请使用gdlib 2或者更高版本。它具有一个称为imagecopyresampled()的函数,该函数将在调整大小的同时插补像素,并且看起来更好。

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

  • 访问速度比磁盘慢
  • 服务器将需要运行脚本来访问映像,而不是简单地提供文件
  • 设置适当的缓存/超时/ E-tag标头,以便客户端可以正确缓存图像。如果操作不正确,则会在每个请求中都击中图像服务脚本,从而进一步增加服务器上的负载。

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

回答

处理步骤应如下所示:

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

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

回答

我们确定服务器上没有ImageMagick吗?

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

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

希望我们有相同的选择。

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

回答

可能是这样的:

<?php
   //Input file
   $file = "myImage.png";
   $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语句来确定文件类型。

回答

<?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.');
}

回答

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