From cddc2c4b4160e8f16fca0171b1b07c2a179acc29 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 20 Dec 2018 09:14:28 +0800 Subject: [PATCH] Add ability to set transparency on images (#186) --- element/image/classes/element.php | 37 ++++++++++++++++++- .../image/lang/en/customcertelement_image.php | 2 + tests/behat/managing_elements.feature | 10 +++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/element/image/classes/element.php b/element/image/classes/element.php index 8261bb8..7708dd7 100644 --- a/element/image/classes/element.php +++ b/element/image/classes/element.php @@ -75,6 +75,24 @@ class element extends \mod_customcert\element { $mform->setDefault('height', 0); $mform->addHelpButton('height', 'height', 'customcertelement_image'); + $alphachannelvalues = [ + '0' => 0, + '0.1' => 0.1, + '0.2' => 0.2, + '0.3' => 0.3, + '0.4' => 0.4, + '0.5' => 0.5, + '0.6' => 0.6, + '0.7' => 0.7, + '0.8' => 0.8, + '0.9' => 0.9, + '1' => 1 + ]; + $mform->addElement('select', 'alphachannel', get_string('alphachannel', 'customcertelement_image'), $alphachannelvalues); + $mform->setType('alphachannel', PARAM_FLOAT); + $mform->setDefault('alphachannel', 1); + $mform->addHelpButton('alphachannel', 'alphachannel', 'customcertelement_image'); + if (get_config('customcert', 'showposxy')) { \mod_customcert\element_helper::render_form_element_position($mform); } @@ -148,6 +166,10 @@ class element extends \mod_customcert\element { 'height' => !empty($data->height) ? (int) $data->height : 0 ]; + if (isset($data->alphachannel)) { + $arrtostore['alphachannel'] = (float) $data->alphachannel; + } + if (!empty($data->fileid)) { // Array of data we will be storing in the database. $fs = get_file_storage(); @@ -189,12 +211,20 @@ class element extends \mod_customcert\element { $location = make_request_directory() . '/target'; $file->copy_content_to($location); + // Check if the alpha channel is set, if it is, use it. + if (isset($imageinfo->alphachannel)) { + $pdf->SetAlpha($imageinfo->alphachannel); + } + $mimetype = $file->get_mimetype(); if ($mimetype == 'image/svg+xml') { $pdf->ImageSVG($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); } else { $pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); } + + // Restore to full opacity. + $pdf->SetAlpha(1); } } @@ -257,7 +287,7 @@ class element extends \mod_customcert\element { public function definition_after_data($mform) { global $COURSE, $SITE; - // Set the image, width and height for this element. + // Set the image, width, height and alpha channel for this element. if (!empty($this->get_data())) { $imageinfo = json_decode($this->get_data()); if (!empty($imageinfo->filename)) { @@ -276,6 +306,11 @@ class element extends \mod_customcert\element { $element = $mform->getElement('height'); $element->setValue($imageinfo->height); } + + if (isset($imageinfo->alphachannel) && $mform->elementExists('alphachannel')) { + $element = $mform->getElement('alphachannel'); + $element->setValue($imageinfo->alphachannel); + } } // Set the context. diff --git a/element/image/lang/en/customcertelement_image.php b/element/image/lang/en/customcertelement_image.php index 1422b6b..ba3ab25 100644 --- a/element/image/lang/en/customcertelement_image.php +++ b/element/image/lang/en/customcertelement_image.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['alphachannel'] = 'Alpha channel'; +$string['alphachannel_help'] = 'This value determines how transparent the image is. You can set the alpha channel from 0 (fully transparent) to 1 (fully opaque).'; $string['height'] = 'Height'; $string['height_help'] = 'Height of the image in mm. If equal to zero, it is automatically calculated.'; $string['image'] = 'Image'; diff --git a/tests/behat/managing_elements.feature b/tests/behat/managing_elements.feature index f2fe4b8..ccc441b 100644 --- a/tests/behat/managing_elements.feature +++ b/tests/behat/managing_elements.feature @@ -186,14 +186,16 @@ Feature: Being able to manage elements in a certificate template # Image. And I add the element "Image" to page "1" of the "Custom certificate 1" certificate template And I set the following fields to these values: - | Width | 25 | - | Height | 15 | + | Width | 25 | + | Height | 15 | + | Alpha channel | 0.7 | And I press "Save changes" And I should see "Image" in the "elementstable" "table" And I click on ".edit-icon" "css_element" in the "Image" "table_row" And the following fields match these values: - | Width | 25 | - | Height | 15 | + | Width | 25 | + | Height | 15 | + | Alpha channel | 0.7 | And I press "Save changes" # Student name. And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template