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


PHP ImagickDraw clear()用法及代碼示例


ImagickDraw::clear()函數是PHP中的內置函數,用於清除ImagickDraw對象的所有累積命令,並將其中包含的設置重置為默認值。

用法:

bool ImagickDraw::clear( void )

參數:此函數不接受任何參數。


返回值:成功時此函數返回TRUE。

異常:該函數在錯誤時引發ImagickException。

下麵給出的程序說明了PHP中的ImagickDraw::clear()函數:
示例1:

<?php 
  
//Create a new Imagick object 
$imagick = new Imagick(); 
  
// Create a image on imagick object 
$imagick->newImage(800, 250, 'black'); 
  
// Create a new ImagickDraw object 
$draw = new ImagickDraw(); 
  
// Set the text properties 
$draw->setFontSize(110); 
$draw->setFillColor('green'); 
  
// Apply the annotation() function 
$draw->annotation(20, 120, 'Random Content'); 
  
// Clear the object which means commands 
// written above are all deleted now 
$draw->clear(); 
  
// Set the font size 
$draw->setFontSize(110); 
$draw->setFillColor('white'); 
  
// Apply the annotation() function 
$draw->annotation(20, 120, 'New Content'); 
  
//  Render the draw commands in the ImagickDraw object 
$imagick->drawImage($draw); 
  
// Show the output 
$imagick->setImageFormat("png"); 
header("Content-Type: image/png"); 
echo $imagick->getImageBlob(); 
?>

輸出:

示例2:

<?php 
  
//Create a new Imagick object 
$imagick = new Imagick(); 
  
// Create a image on imagick object 
$imagick->newImage(800, 250, 'white'); 
  
// Create a new ImagickDraw object 
$draw = new ImagickDraw(); 
  
// Set the text properties 
$draw->setFontSize(110); 
$draw->setFillColor('green'); 
  
// Apply the annotation() function 
$draw->annotation(20, 120, 'GeeksforGeeks'); 
  
// Clear the object which means commands written 
// above are not all deleted now 
$draw->clear(); 
  
//  Render the draw commands in the ImagickDraw object 
$imagick->drawImage($draw); 
  
// Show the output 
$imagick->setImageFormat("png"); 
header("Content-Type: image/png"); 
echo $imagick->getImageBlob(); 
?>

輸出:

This will throw ImagickException because $draw is emptied by clear(), thus cannot be drawn.

參考: https://www.php.net/manual/en/imagickdraw.clear.php



相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | ImagickDraw clear() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。