一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php imagecreate实例教程

时间:2009-11-19 编辑:简简单单 来源:一聚教程网

php imagecreate实例教程

描述
资源imagecreate($width,$height)
imagecreate()返回一个图像标识符代表了指定大小的空白图像。

我们建议使用imagecreatetruecolor()。

看个
header("Content-type: image/png");
$im = @imagecreate(110, 20)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

如果您可以访问gd.h(很少在一个共享的主机),您可以修改GD_RESOLUTION并给它所需的价值。

该解决方案,我发现直接操纵的JPEG头字节。
我希望此示例代码将帮助别人,因为我花了5挖掘推测离开系统I小时,没有其他选择。

header('Content-Disposition: attachment; filename="myimg.jpg"');
header('Cache-Control: private');
$dst    = imagecreatetruecolor(300, 300); /*this creates a 300x300 pixels image*/
ob_start(); /*don't send the output to the browser since we'll need to manipulate it*/
ImageJpeg($dst);
$img = ob_get_contents();
ob_end_clean();

//byte #14 in jpeg sets the resolution type: 0=none, 1=pixels per inch, 2=pixels per cm
//bytes 15-16 sets X resolution
//bytes 17-18 sets Y resolution
$img = substr_replace($img, pack('cnn',1,300,300),13,5);

echo $img;

?>

文件在其文件类型,并返回假的负载如果失败
function imagecreatefromfile($path, $user_functions = false)
{
    $info = @getimagesize($path);
   
    if(!$info)
    {
        return false;
    }
   
    $functions = array(
        IMAGETYPE_GIF => 'imagecreatefromgif',
        IMAGETYPE_JPEG => 'imagecreatefromjpeg',
        IMAGETYPE_PNG => 'imagecreatefrompng',
        IMAGETYPE_WBMP => 'imagecreatefromwbmp',
        IMAGETYPE_XBM => 'imagecreatefromwxbm',
        );
   
    if($user_functions)
    {
        $functions[IMAGETYPE_BMP] = 'imagecreatefrombmp';
    }
   
    if(!$functions[$info[2]])
    {
        return false;
    }
   
    if(!function_exists($functions[$info[2]]))
    {
        return false;
    }
   
    return $functions[$info[2]]($path);
}
?>

热门栏目