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
相關用法
- CSS var()用法及代碼示例
- p5.js day()用法及代碼示例
- CSS url()用法及代碼示例
- p5.js pow()用法及代碼示例
- PHP each()用法及代碼示例
- PHP next()用法及代碼示例
- p5.js second()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP pow( )用法及代碼示例
- d3.js d3.set.has()用法及代碼示例
- p5.js hue()用法及代碼示例
- p5.js value()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | imagecreatefromstring() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。