當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。