2008-08-22 49 views
9

我想允许用户以各种格式(GIF,JPEG和PNG至少)上传头像型图像,但将它们全部保存为PNG数据库BLOBs。如果图像过大,像素方式,我想在DB插入之前调整它们的大小。如何使用GD调整上传的图像并将其转换为PNG?

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

编辑:很遗憾,只有GD可用我需要使用的服务器,没有ImageMagick

+0

不要向或向数据库提供或存储图像。 – dlamblin 2009-10-30 07:11:12

回答

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

This article看起来像它会适合你想要的。您需要将保存imagejpeg()函数更改为imagepng(),并将其保存为字符串而不是将其输出到页面,但除此之外,它应该很容易复制/粘贴到现有代码中。

0

绝对需要GD吗? ImageMagick更快,生成更好的图像,更易于配置,最后(IMO)更易于编码。

+0

声音,但一个简单的商业。任何证明? – Green 2012-08-19 22:56:25

+0

当然。制作缩略图:`$ image = new Imagick($ image_path); $ image-> cropThumbnailImage(100,100);`尝试在GD代码中执行两行代码。 ImageMagick是一个专门用于处理图像的工具包,所以它通常会比随机引入的PHP部分更好。 – ceejayoz 2012-08-20 01:05:46

0

我认为this page是一个很好的起点。它使用imagecreatefrom(jpeg/gif/png)并调整大小并转换图像,然后输出到浏览器。不用输出浏览器,您可以输出到数据库中的BLOB,而无需执行许多代码重写。

3

如果您想使用gdlib,请使用gdlib 2或更高版本。它有一个名为imagecopyresampled()的函数,它将在调整大小时插入像素,并且看起来好多了。

而且,我一直听说周围的网,在数据库中存储的图像是坏的形式指出:

  • 这是慢比磁盘访问
  • 您的服务器需要运行一个脚本来得到的图像,而不是简单地 服务的文件
  • 你的脚本现在是负责很多东西用 web服务器来处理:
    • 硒设置正确的Content-Type标头
    • 设置适当的缓存/超时/电子标签标头,以便客户端可以正确缓存图像。如果不这样做,图像服务脚本将在每个请求中被击中,从而增加服务器上的负载。

我能看到的唯一的好处是,你不需要让你的数据库和图像文件同步。我仍然会推荐它。

+0

与将它们卸载到自己的静态域或cdn上的静态服务器相比,它绝对不希望为数据库提供图像。 – dlamblin 2009-10-30 07:10:07

3

你确定你有没有ImageMagick的服务器上?

我来宾您使用PHP(问题标记为PHP)。我使用的托管公司没有根据phpinfo()打开ImageMagick扩展。

但是,当我问他们有关他们说的这里是从PHP代码可用的ImageMagick程序列表。简而言之 - PHP中没有IM界面,但我可以直接从PHP调用IM程序。

我希望你有同样的选择。

而我强烈同意 - 在数据库中存储图像不是好主意。

3

事情是这样的,也许:


<?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语句来确定文件类型。

相关问题