当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP imagecreatefromstring()用法及代码示例


imagecreatefromstring()函数是PHP中的内置函数,用于从字符串文件或URL创建新图像。该图像可以在程序中进一步处理。当您从字符串加载图像后要编辑图像时,通常使用此函数。

用法:

resource imagecreatefromstring( string $filename )

参数:该函数接受单个参数$filename,该参数保存图像的名称。


返回值:成功时此函数返回图像资源标识符,错误时返回FALSE。

下面给出的程序说明了PHP中的imagecreatefromstring()函数:

程序1(从字符串查看图像):

<?php 
  
// Image data in the form of string 
$data = 'iVBORw0KGgoAAAANSUhEUgAAAHgAAAAUAQMAAAB'
      . '8nGuwAAAABlBMVEX///8AAP94wDzzAAAACXBIWX'
      . 'MAAA7EAAAOxAGVKw4bAAAAgUlEQVQYlWNgIBHYM'
      . 'TDwMDB8gDEYkkEU4wwog4HhAIx/AMqX4+c5/rDh'
      . '4x4w4wHDAWPJ3h7DxhnPwAwDhuOJG87zsD/mOQB'
      . 'mMDAcrt9/nv1hM88BMOMBw+EEA94GQxAfxDBgSD'
      . 'acceYMUP8BMMOAwU6evyf9YcOHA2DGA1K9QykAA'
      . 'NIrNwD/nKH3AAAAAElFTkSuQmCC'; 
   
// Decode the data 
$data = base64_decode($data); 
   
// Create an image from data 
$im = imagecreatefromstring($data); 
   
// Output the image 
header('Content-Type:image/png'); 
imagepng($im); 
imagedestroy($im); 
?>

输出:

程序2(处理加载的字符串图像):

<?php 
  
// Image data in the form of string 
$data = 'iVBORw0KGgoAAAANSUhEUgAAAHgAAAAUAQMAAAB'
      . '8nGuwAAAABlBMVEX///8AAP94wDzzAAAACXBIWX'
      . 'MAAA7EAAAOxAGVKw4bAAAAgUlEQVQYlWNgIBHYM'
      . 'TDwMDB8gDEYkkEU4wwog4HhAIx/AMqX4+c5/rDh'
      . '4x4w4wHDAWPJ3h7DxhnPwAwDhuOJG87zsD/mOQB'
      . 'mMDAcrt9/nv1hM88BMOMBw+EEA94GQxAfxDBgSD'
      . 'acceYMUP8BMMOAwU6evyf9YcOHA2DGA1K9QykAA'
      . 'NIrNwD/nKH3AAAAAElFTkSuQmCC'; 
   
// Decode the data 
$data = base64_decode($data); 
   
// Create an image from data 
$im = imagecreatefromstring($data); 
   
// Negate the image 
imagefilter($im, IMG_FILTER_NEGATE); 
   
// Output the image 
header('Content-Type:image/png'); 
imagepng($im); 
imagedestroy($im); 
?>

输出:

参考: https://www.php.net/manual/en/function.imagecreatefromstring.php



相关用法


注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | imagecreatefromstring() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。