SunshinePHP Developer Conference 2015

gd_info

(PHP 4 >= 4.3.0, PHP 5)

gd_info取得当前安装的 GD 库的信息

说明

array gd_info ( void )

返回一个关联数组描述了安装的 GD 库的版本和性能。

gd_info() 返回的数组的单元
属性 含义
GD Version string 值描述了安装的 libgd 的版本。
Freetype Support boolean 值。如果安装了 Freetype 支持则为 TRUE
Freetype Linkage string 值描述了 Freetype 连接的方法。取值可能为:'with freetype', 'with TTF library' 和 'with unknown library'。本单元仅在 Freetype Support 的值为 TRUE 时有定义。
T1Lib Support boolean 值。如果包含有 T1Lib 支持则为 TRUE
GIF Read Support boolean 值。如果包含有读取 GIF 图像的支持则为 TRUE
GIF Create Support boolean 值。如果包含有创建 GIF 图像的支持则为 TRUE
JPG Support boolean 值。如果包含有 JPG 支持则为 TRUE
PNG Support boolean 值。如果包含有 PNG 支持则为 TRUE
WBMP Support boolean 值。如果包含有 WBMP 支持则为 TRUE
XBM Support boolean 值。如果包含有 XBM 支持则为 TRUE

Example #1 使用 gd_info()

<?php
var_dump
(gd_info());
?>

典型输出为:

array(9) {
  ["GD Version"]=>
  string(24) "bundled (2.0 compatible)"
  ["FreeType Support"]=>
  bool(false)
  ["T1Lib Support"]=>
  bool(false)
  ["GIF Read Support"]=>
  bool(true)
  ["GIF Create Support"]=>
  bool(false)
  ["JPG Support"]=>
  bool(false)
  ["PNG Support"]=>
  bool(true)
  ["WBMP Support"]=>
  bool(true)
  ["XBM Support"]=>
  bool(false)
}

参见 imagepng()imagejpeg()imagegif()imagewbmp()imagetypes()

add a note add a note

User Contributed Notes 2 notes

up
1
Hagan Fox
9 years ago
This function safely determines the GD version, even on PHP versions earlier than 4.3 that lack the gd_info() function.  Use it to avoid having your script halt with a fatal error if you try to use a TrueColor function and the GD version isn't 2.0 or later.

You can optionally specify a version, but if you specify version 2 it might be overridden.  Once the version number is determined it's retained to speed future calls.

<?php
/**
* Get which version of GD is installed, if any.
*
* Returns the version (1 or 2) of the GD extension.
*/
function gdVersion($user_ver = 0)
{
    if (!
extension_loaded('gd')) { return; }
    static
$gd_ver = 0;
   
// Just accept the specified setting if it's 1.
   
if ($user_ver == 1) { $gd_ver = 1; return 1; }
   
// Use the static variable if function was called previously.
   
if ($user_ver !=2 && $gd_ver > 0 ) { return $gd_ver; }
   
// Use the gd_info() function if possible.
   
if (function_exists('gd_info')) {
       
$ver_info = gd_info();
       
preg_match('/\d/', $ver_info['GD Version'], $match);
       
$gd_ver = $match[0];
        return
$match[0];
    }
   
// If phpinfo() is disabled use a specified / fail-safe choice...
   
if (preg_match('/phpinfo/', ini_get('disable_functions'))) {
        if (
$user_ver == 2) {
           
$gd_ver = 2;
            return
2;
        } else {
           
$gd_ver = 1;
            return
1;
        }
    }
   
// ...otherwise use phpinfo().
   
ob_start();
   
phpinfo(8);
   
$info = ob_get_contents();
   
ob_end_clean();
   
$info = stristr($info, 'gd version');
   
preg_match('/\d/', $info, $match);
   
$gd_ver = $match[0];
    return
$match[0];
}
// End gdVersion()

// Usage:

if ($gdv = gdVersion()) {
    if (
$gdv >=2) {
        echo
'TrueColor functions may be used.';
    } else {
        echo
'GD version is 1.  Avoid the TrueColor functions.';
    }
} else {
    echo
"The GD extension isn't loaded.";
}
?>

The function only detects the GD version, but you could determine GD capabilities in a similar manner.
up
-2
yohami dot com - zerodj at hotmail dot com
10 years ago
A cool resize / cropping script for creating thumbnails using mogrify

IMAGETEST.PHP

<?php

include 'mogrify.php';

// variables from flash (my website uses flash and php)
$picture="sample.jpg";
$fixedwidth=300;
$fixedheight=240;
//

cropimage($picture,$fixedwidth,$fixedheight,$mogrify);

?>

MOGRIFY.PHP

<?php
// walking the path
$mogrify="C:/apache/Imagik/mogrify.exe";

// ---------------------------------------- crop function

function cropimage($picture,$fixedwidth,$fixedheight,$mogrify) {

   
// GET IMG
   
$img = imagecreatefromjpeg($picture);
   
$width= imagesx($img);
   
$height= imagesy($img);
   
// CROP WIDTH
   
if($width!=$fixedwidth){
       
$ratio =$fixedwidth/$width;
       
$NewHeight=round($height*$ratio);
       
$NewWidth=round($width*$ratio);
       
exec( $mogrify." -resize ".$NewWidth."x".$NewHeight."! $picture");
       
exec( $mogrify." -crop ".$fixedwidth."x".$fixedheight."+0+0 $picture");
       
// REFRESH
       
$img = imagecreatefromjpeg($picture);
       
$width= imagesx($img);
       
$height= imagesy($img);
    }
   
// CROP HEIGHT
   
if($height!=$fixedheight){
       
$ratio =$fixedheight/$height;
       
$NewHeight=round($height*$ratio);
       
$NewWidth=round($width*$ratio);
       
exec( $mogrify." -resize ".$NewWidth."x".$NewHeight."! $picture");
       
exec( $mogrify." -crop ".$fixedwidth."x".$fixedheight."+0+0 $picture");
    }
   
//
   
ImageDestroy($img);
}

?>

yeah!
To Top