Add ability to set transparency on images (#186)

This commit is contained in:
Mark Nelson 2018-12-20 09:14:28 +08:00
parent 6f7ad60f09
commit cddc2c4b41
3 changed files with 44 additions and 5 deletions

View file

@ -75,6 +75,24 @@ class element extends \mod_customcert\element {
$mform->setDefault('height', 0); $mform->setDefault('height', 0);
$mform->addHelpButton('height', 'height', 'customcertelement_image'); $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')) { if (get_config('customcert', 'showposxy')) {
\mod_customcert\element_helper::render_form_element_position($mform); \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 'height' => !empty($data->height) ? (int) $data->height : 0
]; ];
if (isset($data->alphachannel)) {
$arrtostore['alphachannel'] = (float) $data->alphachannel;
}
if (!empty($data->fileid)) { if (!empty($data->fileid)) {
// Array of data we will be storing in the database. // Array of data we will be storing in the database.
$fs = get_file_storage(); $fs = get_file_storage();
@ -189,12 +211,20 @@ class element extends \mod_customcert\element {
$location = make_request_directory() . '/target'; $location = make_request_directory() . '/target';
$file->copy_content_to($location); $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(); $mimetype = $file->get_mimetype();
if ($mimetype == 'image/svg+xml') { if ($mimetype == 'image/svg+xml') {
$pdf->ImageSVG($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); $pdf->ImageSVG($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
} else { } else {
$pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); $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) { public function definition_after_data($mform) {
global $COURSE, $SITE; 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())) { if (!empty($this->get_data())) {
$imageinfo = json_decode($this->get_data()); $imageinfo = json_decode($this->get_data());
if (!empty($imageinfo->filename)) { if (!empty($imageinfo->filename)) {
@ -276,6 +306,11 @@ class element extends \mod_customcert\element {
$element = $mform->getElement('height'); $element = $mform->getElement('height');
$element->setValue($imageinfo->height); $element->setValue($imageinfo->height);
} }
if (isset($imageinfo->alphachannel) && $mform->elementExists('alphachannel')) {
$element = $mform->getElement('alphachannel');
$element->setValue($imageinfo->alphachannel);
}
} }
// Set the context. // Set the context.

View file

@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @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'] = 'Height';
$string['height_help'] = 'Height of the image in mm. If equal to zero, it is automatically calculated.'; $string['height_help'] = 'Height of the image in mm. If equal to zero, it is automatically calculated.';
$string['image'] = 'Image'; $string['image'] = 'Image';

View file

@ -186,14 +186,16 @@ Feature: Being able to manage elements in a certificate template
# Image. # Image.
And I add the element "Image" to page "1" of the "Custom certificate 1" certificate template 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: And I set the following fields to these values:
| Width | 25 | | Width | 25 |
| Height | 15 | | Height | 15 |
| Alpha channel | 0.7 |
And I press "Save changes" And I press "Save changes"
And I should see "Image" in the "elementstable" "table" And I should see "Image" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Image" "table_row" And I click on ".edit-icon" "css_element" in the "Image" "table_row"
And the following fields match these values: And the following fields match these values:
| Width | 25 | | Width | 25 |
| Height | 15 | | Height | 15 |
| Alpha channel | 0.7 |
And I press "Save changes" And I press "Save changes"
# Student name. # Student name.
And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template