PHP Velho Oeste 2024

imagexbm

(PHP 5, PHP 7, PHP 8)

imagexbm输出 XBM 图像到浏览器或文件

说明

imagexbm(GdImage $image, ?string $filename, ?int $foreground_color = null): bool

输出或保存 imageXBM 版本。

注意: imagexbm() 不应用任何填充,因此图片宽度必须是 8 的倍数。从 PHP 7.0.9 起此限制不再适用。

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

filename

string 格式,给出保存到文件的路径。如果为 null,将直接输出原始图像流。

The filename (without the .xbm extension) is also used for the C identifiers of the XBM, whereby non alphanumeric characters of the current locale are substituted by underscores. If filename is set to null, image is used to build the C identifiers.

foreground_color

通过设置从 imagecolorallocate() 获得的标识符来使用此参数设置前景色。默认前景色是黑色。所有的其它颜色都视为背景。

返回值

成功时返回 true, 或者在失败时返回 false

警告

如果 libgd 输出图像失败,函数会返回 true

更新日志

版本 说明
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource
8.0.0 foreground_color 现在允许为 null。
8.0.0 第四个参数未使用,已移除。

示例

示例 #1 保存 XBM 文件

<?php
// 创建空白图像并添加文字
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);

// 保存图像
imagexbm($im, 'simpletext.xbm');

// 释放内存
imagedestroy($im);
?>

示例 #2 以不同前景色保存一个 XBM 文件

<?php
// 创建空白图像并添加文字
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);

// 设置替换的前景色
$foreground_color = imagecolorallocate($im, 255, 0, 0);

// 保存图像
imagexbm($im, NULL, $foreground_color);

// 释放内存
imagedestroy($im);
?>

注释

add a note add a note

User Contributed Notes 1 note

up
0
Anonymous
12 years ago
FlagCreation with some random text inside.

<?php
class Logo{
    private
$colors;
        private
$imgWidth
    private
$imgHeight;  
    private
$img;
    private
$text;

    public function
__construct($width = 100, $height = 60){
       
$this->imgWidth = $width;
       
$this->imgHeight = $height;
       
$this->text = "RND TEXT";
       
$this->createImage();
    }
   
    public function
getText(){
        return
$this->text;
    }
   
    public function
createImage(){
       
$this->img = imagecreatetruecolor($this->imgWidth,$this->imgHeight);
           
$farbe = array(200,200,200);
           
$this->colors[0] = $this->makeColor($farbe);
           
$farbe = array(100,100,200);
           
$this->colors[1] = $this->makeColor($farbe);
       
       
imagefill($this->img,0,0,$this->colors[0]);
               
       
$streifenhoehe = intval($this->imgHeight / 6);       
       
$textgroesse = intval($streifenhoehe *2);
       
$y = 0;
       
$x = 0;
       
       
imagefilledrectangle($this->img,0,0,$this->imgWidth,$streifenhoehe,$this->colors[1]);
       
$y = $this->imgHeight - $streifenhoehe;
       
imagefilledrectangle($this->img,0,$y,$this->imgWidth,$this->imgHeight,$this->colors[1]);
       
       
       
$textma = imagettfbbox ( $textgroesse ,0 , "ARIAL.TTF", $this->text);
       
$textanfang = ($this->imgWidth - ($textma[2] - $textma[0]))/2;
       
$textanfang_hoehe = intval(($this->imgHeight-($textma[7]-$textma[1]))/2);
       
        
imagettftext($this->img, $textgroesse,0,$textanfang, $textanfang_hoehe, $this->colors[1],"ARIAL.TTF", $this->text);
       
       
    }
   
    public function
makeColor($color){
        if (
count($color)%3 != 0)
            return
false;
        else
            return
imagecolorallocate($this->img,$color[0],$color[1],$color[2]);
    }
   
    public function
getImage(){
        
header('Content-Type: image/gif', true);
       
imagejpeg($this->img);
    }   
}

$logo = new Logo(300,180);
$logo->getImage();
?>
To Top