array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.imagecreatefromstring.php', 1 => 'imagecreatefromstring', ), 'up' => array ( 0 => 'ref.image.php', 1 => 'GD 和图像处理 函数', ), 'prev' => array ( 0 => 'function.imagecreatefrompng.php', 1 => 'imagecreatefrompng', ), 'next' => array ( 0 => 'function.imagecreatefromtga.php', 1 => 'imagecreatefromtga', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/image/functions/imagecreatefromstring.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
imagecreatefromstring — 从字符串的图像流中新建图像
imagecreatefromstring() 返回图像标识符,代表从指定 data
获得的图像。图像格式将自动检测,只要编译的 PHP 支持:JPEG、PNG、GIF、BMP、WBMP、GD2 和 WEBP。
data
包含图像数据的字符串。
成功时返回图像对象。如果图像类型不支持、数据格式不可识别或者图像已损坏且无法加载,则返回 false
。
如果数据格式不可识别,则 imagecreatefromstring() 引发 E_WARNING 级别错误。
示例 #1 imagecreatefromstring() 示例
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
以上示例的输出类似于: