此函式的參數真的令人害怕。高斯半徑和高斯標準差?在玩了一會兒之後,我想我把它們組合起來了。高斯是「筆刷」(在 Gimp/Photoshop 行話中),半徑只是筆刷的半徑,而標準差是筆刷印記大小之間的變化程度。$radius 參數可以是從 0 到您能想到的任何高數字,但是一旦您超過 10、20 或 30 像素,具體取決於影像大小,整個影像就會模糊到無法辨識。$sigma 作為標準差,應小於您的半徑才能獲得想要的效果。把它想成「大小為 $radius 像素的炭筆筆刷,每個筆刷都比這個大小大或小 $sigma 像素」。
對於平均 500 x 500 像素的影像,您可能需要 3 到 5 的 $radius 和 1 到 3 的 $sigma,但通常可以達到 10 像素,然後影像才會模糊到無法辨識。(目前,$radius: 5 / $sigma: 2 是我目前正在處理的 400x400 影像的完美組合。)
此專案的官方文件網站也沒有太多幫助:https://imagemagick.dev.org.tw/RMagick/doc/image1.html 。那裡的作者指出:「您可以通過變更半徑和 sigma 參數來改變效果的強度。」因此,我在此處對其功能的描述主要基於經驗而不是難以找到的文件。
以下是將效果應用於影像的完整程式碼。此程式碼將開啟指定的檔案,將 charcoalImage 效果應用於該檔案,然後將其儲存到另一個指定的檔案。參數是通過 POST 資料提供的,而使用的 ImageMagick 類的唯一函式是 readImageFile、charcoalImage 和 writeImageFile,如下所示
<?php
// 作者:holdoffhunger@gmail.com
// 抓取輸入資料 -- 函式參數
// --------------------------------------------------
$inbound_gaussian_radius = $_POST['radius_of_gaussian'];
$inbound_standard_deviation = $_POST['standard_deviation_of_gaussian'];
// 抓取輸入資料 -- 讀取檔案和寫入檔案
// --------------------------------------------------
$filename_for_function = $_POST['file_target'];
$inbound_save_as_filename = $_POST['saveable_result_file'];
// 抓取影像檔案資料
// ---------------------------------------------
$folder_location = "images/workshop/";
$file_to_grab_with_location = $folder_location . $filename_for_function;
$imagick_type = new Imagick();
// 開啟檔案
// ---------------------------------------------
$file_handle_for_viewing_image_file = fopen($file_to_grab_with_location, 'a+');
// 讀取檔案
// ---------------------------------------------
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
// 執行函式
// ---------------------------------------------
$imagick_type->charcoalImage($inbound_gaussian_radius, $inbound_standard_deviation);
// 儲存檔案
// ---------------------------------------------
$folder_location = "images/workshop/";
$file_to_grab_with_location = $folder_location . $inbound_save_as_filename;
$file_handle_for_saving_image_file = fopen($file_to_grab_with_location, 'a+');
// 寫入檔案
// ---------------------------------------------
$imagick_type->writeImageFile($file_handle_for_saving_image_file);
?>