function _color_render_images

Same name in other branches
  1. 7.x modules/color/color.module \_color_render_images()
  2. 9 core/modules/color/color.module \_color_render_images()

Renders images that match a given palette.

1 call to _color_render_images()
color_scheme_form_submit in core/modules/color/color.module
Form submission handler for color_scheme_form().

File

core/modules/color/color.module, line 604

Code

function _color_render_images($theme, &$info, &$paths, $palette) {
    // Prepare template image.
    $source = $paths['source'] . '/' . $info['base_image'];
    $source = imagecreatefrompng($source);
    $width = imagesx($source);
    $height = imagesy($source);
    // Prepare target buffer.
    $target = imagecreatetruecolor($width, $height);
    imagealphablending($target, TRUE);
    // Fill regions of solid color.
    foreach ($info['fill'] as $color => $fill) {
        imagefilledrectangle($target, $fill[0], $fill[1], $fill[0] + $fill[2], $fill[1] + $fill[3], _color_gd($target, $palette[$color]));
    }
    // Render gradients.
    foreach ($info['gradients'] as $gradient) {
        // Get direction of the gradient.
        if (isset($gradient['direction']) && $gradient['direction'] == 'horizontal') {
            // Horizontal gradient.
            for ($x = 0; $x < $gradient['dimension'][2]; $x++) {
                $color = _color_blend($target, $palette[$gradient['colors'][0]], $palette[$gradient['colors'][1]], $x / ($gradient['dimension'][2] - 1));
                imagefilledrectangle($target, $gradient['dimension'][0] + $x, $gradient['dimension'][1], $gradient['dimension'][0] + $x + 1, $gradient['dimension'][1] + $gradient['dimension'][3], $color);
            }
        }
        else {
            // Vertical gradient.
            for ($y = 0; $y < $gradient['dimension'][3]; $y++) {
                $color = _color_blend($target, $palette[$gradient['colors'][0]], $palette[$gradient['colors'][1]], $y / ($gradient['dimension'][3] - 1));
                imagefilledrectangle($target, $gradient['dimension'][0], $gradient['dimension'][1] + $y, $gradient['dimension'][0] + $gradient['dimension'][2], $gradient['dimension'][1] + $y + 1, $color);
            }
        }
    }
    // Blend over template.
    imagecopy($target, $source, 0, 0, 0, 0, $width, $height);
    // Clean up template image.
    imagedestroy($source);
    // Cut out slices.
    foreach ($info['slices'] as $file => $coord) {
        list($x, $y, $width, $height) = $coord;
        
        /** @var \Drupal\Core\File\FileSystemInterface $file_system */
        $file_system = \Drupal::service('file_system');
        $base = $file_system->basename($file);
        $image = $file_system->realpath($paths['target'] . $base);
        // Cut out slice.
        if ($file == 'screenshot.png') {
            $slice = imagecreatetruecolor(150, 90);
            imagecopyresampled($slice, $target, 0, 0, $x, $y, 150, 90, $width, $height);
            \Drupal::configFactory()->getEditable('color.theme.' . $theme)
                ->set('screenshot', $image)
                ->save();
        }
        else {
            $slice = imagecreatetruecolor($width, $height);
            imagecopy($slice, $target, 0, 0, $x, $y, $width, $height);
        }
        // Save image.
        imagepng($slice, $image);
        imagedestroy($slice);
        $paths['files'][] = $image;
        // Set standard file permissions for webserver-generated files.
        $file_system->chmod($image);
        // Build before/after map of image paths.
        $paths['map'][$file] = $base;
    }
    // Clean up target buffer.
    imagedestroy($target);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.