我怎么调整,并转换成图像上传到PNG使用GD?
题
我想让用户上的头像类型图像在各种各样的格式(GIF,JPEG,并PNG至少),而是要拯救他们所为 PNG数据库的斑点.如果图像是超大,pixelwise,我想调整他们之前DB-插入。
什么是最好的方式来使用GD做的调整和最新的转换?
编辑:可悲的是,只有 GD 可用的服务器上,我需要使用的,没有 ImageMagick.
解决方案
<?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.');
}
其他提示
你过程中的步骤应该是这样的:
- 验证 的 filetype
- 负载的图像,如果这是一个支持filetype入GD使用 imagecreatefrom*
- 调整使用 imagecopyresize 或 imagecopyresampled
- 保存的图像的使用 imagepng($handle,'filename.png',$质量,$过滤器)
ImageMagick速度更快、产生更好的图像,更多的配置,最后是(海事组织)更容易代码。
@ceejayoz只是等待新的GD-这是面向对象等)的预,并实际上,它不坏:)
如果你想使用gdlib,使用gdlib2或更高。它有一个功能称为imagecopyresampled(),其中将插入素而调整和看起来好多了。
此外,我总是听到注意周围的网的图像存储在数据库是糟糕的形式:
- 它的速度较慢,访问比磁盘
- 你的服务器将需要运行一个脚本来获得的图像,而不是 简单地提供服务的文件
- 你的脚现在负责一个很大的东西的网服务器使用
到处理:
- 设置适当的Content-Type header
- 设置适当的缓存/timeout/电子标签的标题,使客户能够适当地缓存的图像。如果不这样做正确的,本图像服务脚本将在每个请求,增加上载服务器,甚至更多。
唯一的优点我可以看到的是你不需要保持数据库和图像的文件同步。我仍然建议反对它。
你确定你没有ImageMagick在服务器?
我的客人,您使用PHP(问题是,标记PHP)。托管公司我用没有ImageMagick扩根据phpinfo().
但当我问他们关于他们所说的 这里是ImageMagick程序可从PHP码.这么简单-有没有我的接口在PHP,但我可以打电话给IM程序直接从PHP.
我希望你也有同样的选择。
我 强烈 同意--储存的图像数据库是不好的想法。
像这样的东西,也许是:
<?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);
?>
我还没有测试的代码,因此可能有某些语法错误,但是它应该给你一个公平的介绍它如何可以做到的。此外,我假定一个新的文件。你可能需要有某种关声明确定文件的类型。
这篇文章 好像它会适合你想要什么。你会需要改变节省imagejpeg()功能以imagepng()并将文件保存到一串而不是输出它的一页,但其他比,应该可以很容易地复制、粘贴到现有的代码。
是GD绝对必需的? ImageMagick 速度更快、产生更好的图像,更多的配置,最后是(海事组织)更容易代码。
我认为 这页 是一个很好的起点。它使用imagecreatefrom(jpeg/gif/png)和调整,并转换成图像,然后输出的浏览器。而不是输出的浏览器可以输出到BLOB在一个数据库没有多minuttes的代码-重写。
phpThumb 是一个高层次的抽象,可能是值得一看。