如何在 PHP 中使用 imagedestroy() 函式銷燬影像?


imagedestroy() 是一個內建的 PHP 函式,用於銷燬影像並釋放與影像關聯的任何記憶體。

語法

bool imagedestroy(resource $image)

引數

imagedestroy() 只接受一個引數,$image。它儲存影像的名稱。

返回值

imagedestroy() 成功時返回 true,失敗時返回 false。

示例 1 - 載入影像後銷燬它。

<?php
   // Load the png image from the local drive folder
   $img = imagecreatefrompng('C:\xampp\htdocs\Images\img32.png');

   // Crop the image
   $cropped = imagecropauto($img, IMG_CROP_BLACK);

   // Convert it to a png file
   imagepng($cropped);

   // It will destroy the cropped image to free/deallocate the memory.
   imagedestroy($cropped);
?>

輸出

Note − By using imagedestroy() function, we have destroyed the $cropped variable and therefore, it can no longer be accessed.

解釋 - 在示例 1 中,imagecreatefrompng() 從本地驅動器資料夾載入影像,並使用 imagecropauto() 函式從給定影像中裁剪影像的一部分。裁剪後,使用 imagedestroy() 函式銷燬影像。銷燬影像後,我們無法訪問影像或 $cropped 變數。

示例 2

<?php
   // create a 50 x 50 image
   $img = imagecreatetruecolor(50, 50);
   
   // frees image from memory
   imagedestroy($img);
?>

注意 - 在上面的 PHP 程式碼中,使用 imagecreatetruecolor() 函式建立了一個 50×50 的影像。建立影像後,使用 imagedestroy() 函式釋放或釋放已使用的記憶體。

更新於: 2021-08-09

515 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.