【发布时间】:2010-09-06 12:58:49
【问题描述】:
我希望允许用户上传各种格式的头像类型图像(至少是 GIF、JPEG 和 PNG),但要将它们全部保存为 PNG 数据库 BLOB。如果图像过大,按像素计算,我想在插入 DB 之前调整它们的大小。
使用 GD 进行大小调整和 PNG 转换的最佳方法是什么?
编辑:遗憾的是,我需要使用的服务器上只有GD 可用,没有ImageMagick。
【问题讨论】:
-
不要在数据库中提供或存储图像。
我希望允许用户上传各种格式的头像类型图像(至少是 GIF、JPEG 和 PNG),但要将它们全部保存为 PNG 数据库 BLOB。如果图像过大,按像素计算,我想在插入 DB 之前调整它们的大小。
使用 GD 进行大小调整和 PNG 转换的最佳方法是什么?
编辑:遗憾的是,我需要使用的服务器上只有GD 可用,没有ImageMagick。
【问题讨论】:
phpThumb 是一个可能值得一看的高级抽象。
【讨论】:
<?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.');
}
【讨论】:
您的流程步骤应如下所示:
ImageMagick 速度更快,生成更好的图像,更易于配置,并且(IMO)更容易编写代码。
@ceejayoz 等待新的 GD - 它像 MySQLi 一样是 OOP,实际上还不错 :)
【讨论】:
可能是这样的:
<?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 语句来确定文件类型。
【讨论】:
你确定服务器上没有 ImageMagick 吗?
我来宾您使用 PHP(问题用 PHP 标记)。我使用的托管公司没有根据 phpinfo() 打开 ImageMagick 扩展。
但当我询问他们时,他们说这里是 PHP 代码中可用的 ImageMagick 程序列表。很简单——PHP没有IM接口,但是我可以直接从PHP调用IM程序。
希望你也有同样的选择。
我强烈同意——将图像存储在数据库中并不是一个好主意。
【讨论】:
如果您想使用 gdlib,请使用 gdlib 2 或更高版本。它有一个名为 imagecopyresampled() 的函数,它会在调整大小的同时插入像素并看起来更好。
另外,我一直在网上听说,将图像存储在数据库中是一种不好的形式:
我能看到的唯一优点是您不需要保持数据库和图像文件同步。不过,我仍然建议不要这样做。
【讨论】:
我认为this page 是一个很好的起点。它使用 imagecreatefrom(jpeg/gif/png) 并调整大小并转换图像,然后输出到浏览器。除了输出浏览器,您还可以输出到数据库中的 BLOB,而无需大量代码重写。
【讨论】:
GD 是绝对必要的吗? ImageMagick 更快,生成更好的图像,更易于配置,并且(IMO)更容易编写代码。
【讨论】:
$image = new Imagick($image_path); $image->cropThumbnailImage(100,100); 尝试在 GD 中用两行代码来实现。 ImageMagick 是一个专门为处理图像而设计的工具包,因此它通常会比 PHP 的随机插入部分更好。
This article 似乎符合您的要求。您需要将保存 imagejpeg() 函数更改为 imagepng() 并将文件保存为字符串而不是将其输出到页面,但除此之外它应该很容易复制/粘贴到您现有的代码中。
【讨论】: