如何用程序检测GD的版本,网上找了二个,都不能用 不知道为什么。 function chkgd2(){ $testGD = get_extension_funcs("gd"); // Grab function list if (!$testGD){ echo "GD not even installed."; exit; } if (in_array ("imagegd2",$testGD)) $gd_version = "<2"; // Check if ($gd_version == "<2") return false; else return true;
}if (chkgd2()) echo "<h1>GD2+ is installed.</h1>"; // Test for GD2 else echo "<h1>GD2+ is not installed.</h1>";?>//////////////////////////////////// <?php ob_start(); phpinfo(8); $phpinfo=ob_get_contents();ob_end_clean(); $phpinfo=strip_tags($phpinfo); $phpinfo=stristr($phpinfo,"gd version"); $phpinfo=stristr($phpinfo,"version"); $end=strpos($phpinfo," "); $phpinfo=substr($phpinfo,0,$end); $phpinfo=substr($phpinfo,7); echo $phpinfo; if(version_compare("2.0", "$phpinfo")==1) echo "you have a version less then 2"; ?>
用回GD1没问题。另外,最好先判断一下高宽,否则要变形的
改进的函数:function makethumb($srcFile,$dstFile,$dstW,$dstH)
{
$dstW = intval($dstW);
$dstH = intval($dstH); if($dstW == 0 && $dstH == 0)
return; $size = GetImageSize($srcFile,&$info); $newWidth = $size[0];
$newHeight = $size[1]; if($size[0] > $dstW)
{
$newWidth = $dstW;
$newHeight = intval(($newWidth / $size[0]) * $size[1]);
}
if($newHeight > $dstH)
{
$newHeight = $dstH;
$newWidth = intval(($newHeight / $size[1]) * $size[0]);
}
if($newWidth == 0 || $newHeight == 0)
return;
switch ($size[2])
{
case 1:
return false;//$im = ImageCreateFromGIF($srcFile);
break;
case 2:
$im = @imagecreatefromjpeg($srcFile);
break;
case 3:
$im = @ImageCreateFromPNG($srcFile);
break;
}
if($im)
{
$srcW= $size[0];
$srcH= $size[1]; $ni=@ImageCreate($newWidth,$newHeight);
if($ni)
{
if(@ImageCopyResized($ni,$im,0,0,0,0,$newWidth,$newHeight,$srcW,$srcH))
{
return @ImageJpeg($ni,$dstFile);
}
}
} return false;
// 如果需要输出到浏览器,那么将上一句改为ImageJpeg($ni);
// 如果需要其它格式的图片,改动最后一句就可以了
}
好象ImageCreate生成的是256色的,你用函数
$ni=ImageCreateTrueColor($dstW,$dstH);
试试!
先谢谢了,我来用ImageCreateTrueColor试试
function chkgd2(){
$testGD = get_extension_funcs("gd"); // Grab function list
if (!$testGD){ echo "GD not even installed."; exit; }
if (in_array ("imagegd2",$testGD)) $gd_version = "<2"; // Check
if ($gd_version == "<2") return false; else return true;
}if (chkgd2()) echo "<h1>GD2+ is installed.</h1>"; // Test for GD2
else echo "<h1>GD2+ is not installed.</h1>";?>////////////////////////////////////
<?php
ob_start();
phpinfo(8);
$phpinfo=ob_get_contents();ob_end_clean();
$phpinfo=strip_tags($phpinfo);
$phpinfo=stristr($phpinfo,"gd version");
$phpinfo=stristr($phpinfo,"version");
$end=strpos($phpinfo," ");
$phpinfo=substr($phpinfo,0,$end);
$phpinfo=substr($phpinfo,7);
echo $phpinfo;
if(version_compare("2.0", "$phpinfo")==1)
echo "you have a version less then 2";
?>
怎么加点数给你
$RESIZEWIDTH=400;// 生成图片的高度
$RESIZEHEIGHT=400;
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}?><img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br><form enctype="multipart/form-data" method="post">
<br>
<input type="file" name="image" size="50" value="浏览"><p>
<input type="submit" value="上传图片">
</form> </body>
</html>
我想用
if ($gd_version>= 2) {
$image = ImageCreateTrueColor($width, $height);
} else {
$image = ImageCreate($width, $height);
}
但找不到适合所有php版本的函数来取得GD的版本号。
$ni=ImageCreate($dstW,$dstH);$ni这个图片你最好要用黑色背景!
这样就可以解决你的问题了!
http://china-sss.xicp.net:8000/
一个使用
imagecreateturecolor()代替imagecreate()
另一个使用
imagecopyresampled()代替imagecopyresized()