Manage Presentation Backgrounds in PHP
Overview
Solid colors, gradients, and images are commonly used for slide backgrounds. You can set the background for a normal slide (a single slide) or a master slide (applies to multiple slides at once).
Set a Solid Color Background for a Normal Slide
Aspose.Slides allows you to set a solid color as the background for a specific slide in a presentation—even if the presentation uses a master slide. The change applies only to the selected slide.
- Create an instance of the Presentation class.
- Set the slide’s BackgroundType to
OwnBackground
. - Set the slide background FillType to
Solid
. - Use the getSolidFillColor method on FillFormat to specify the solid background color.
- Save the modified presentation.
The following PHP example shows how to set a blue solid color as the background for a normal slide:
// Create an instance of the Presentation class.
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
// Set the background color of the slide to blue.
$slide->getBackground()->setType(BackgroundType::OwnBackground);
$slide->getBackground()->getFillFormat()->setFillType(FillType::Solid);
$slide->getBackground()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
// Save the presentation to disk.
$presentation->save("SolidColorBackground.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Set a Solid Color Background for the Master Slide
Aspose.Slides allows you to set a solid color as the background for the master slide in a presentation. The master slide acts as a template that controls formatting for all slides, so when you choose a solid color for the master slide’s background, it applies to every slide.
- Create an instance of the Presentation class.
- Set the master slide’s BackgroundType (via
getMasters
) toOwnBackground
. - Set the master slide background FillType to
Solid
. - Use the getSolidFillColor method to specify the solid background color.
- Save the modified presentation.
The following PHP example shows how to set a solid color (green) as the background for a master slide:
// Create an instance of the Presentation class.
$presentation = new Presentation();
try {
$masterSlide = $presentation->getMasters()->get_Item(0);
// Set the background color for the Master slide to Forest Green.
$masterSlide->getBackground()->setType(BackgroundType::OwnBackground);
$masterSlide->getBackground()->getFillFormat()->setFillType(FillType::Solid);
$masterSlide->getBackground()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GREEN);
// Save the presentation to disk.
$presentation->save("MasterSlideBackground.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Set a Gradient Background for a Slide
A gradient is a graphical effect created by a gradual change in color. When used as a slide background, gradients can make presentations look more artistic and professional. Aspose.Slides allows you to set a gradient color as the background for slides.
- Create an instance of the Presentation class.
- Set the slide’s BackgroundType to
OwnBackground
. - Set the slide background FillType to
Gradient
. - Use the getGradientFormat method on FillFormat to configure your preferred gradient settings.
- Save the modified presentation.
The following PHP example shows how to set a gradient color as the background for a slide:
// Create an instance of the Presentation class.
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
// Apply a gradient effect to the background.
$slide->getBackground()->setType(BackgroundType::OwnBackground);
$slide->getBackground()->getFillFormat()->setFillType(FillType::Gradient);
$slide->getBackground()->getFillFormat()->getGradientFormat()->setTileFlip(TileFlip::FlipBoth);
// Save the presentation to disk.
$presentation->save("GradientBackground.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Set an Image as a Slide Background
In addition to solid and gradient fills, Aspose.Slides allows you to use images as slide backgrounds.
- Create an instance of the Presentation class.
- Set the slide’s BackgroundType to
OwnBackground
. - Set the slide background FillType to
Picture
. - Load the image you want to use as the slide background.
- Add the image to the presentation’s image collection.
- Use the getPictureFillFormat method on FillFormat to assign the image as the background.
- Save the modified presentation.
The following PHP example shows how to set an image as the background for a slide:
// Create an instance of the Presentation class.
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
// Set background image properties.
$slide->getBackground()->setType(BackgroundType::OwnBackground);
$slide->getBackground()->getFillFormat()->setFillType(FillType::Picture);
$slide->getBackground()->getFillFormat()->getPictureFillFormat()->setPictureFillMode(PictureFillMode::Stretch);
// Load the image.
$image = Images::fromFile("Tulips.jpg");
// Add the image to the presentation's image collection.
$ppImage = $presentation->getImages()->addImage($image);
$image->dispose();
$slide->getBackground()->getFillFormat()->getPictureFillFormat()->getPicture()->setImage($ppImage);
// Save the presentation to disk.
$presentation->save("ImageAsBackground.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
The following code sample shows how to set the background fill type to a tiled picture and modify the tiling properties:
$presentation = new Presentation();
try {
$firstSlide = $presentation->getSlides()->get_Item(0);
$background = $firstSlide->getBackground();
$background->setType(BackgroundType::OwnBackground);
$background->getFillFormat()->setFillType(FillType::Picture);
$newImage = Images::fromFile("image.png");
$ppImage = $presentation->getImages()->addImage($newImage);
$newImage->dispose();
// Set the image used for the background fill.
$backPictureFillFormat = $background->getFillFormat()->getPictureFillFormat();
$backPictureFillFormat->getPicture()->setImage($ppImage);
// Set the picture fill mode to Tile and adjust the tile properties.
$backPictureFillFormat->setPictureFillMode(PictureFillMode::Tile);
$backPictureFillFormat->setTileOffsetX(15);
$backPictureFillFormat->setTileOffsetY(15);
$backPictureFillFormat->setTileScaleX(46);
$backPictureFillFormat->setTileScaleY(87);
$backPictureFillFormat->setTileAlignment(RectangleAlignment::Center);
$backPictureFillFormat->setTileFlip(TileFlip::FlipY);
$presentation->save("TileBackground.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Change the Background Image Transparency
You may want to adjust the transparency of a slide’s background image to make the contents of the slide stand out. The following PHP code shows you how to change the transparency for a slide background image:
$transparencyValue = 30; // For example.
// Get the collection of picture transform operations.
$imageTransform = $slide->getBackground()->getFillFormat()->getPictureFillFormat()->getPicture()->getImageTransform();
// Find an existing fixed-percentage transparency effect.
$transparencyOperation = null;
foreach($imageTransform as $operation) {
if (java_instanceof($operation, new JavaClass("com.aspose.slides.AlphaModulateFixed"))) {
$transparencyOperation = $operation;
break;
}
}
// Set the new transparency value.
if (java_is_null($transparencyOperation)) {
$imageTransform->addAlphaModulateFixedEffect(100 - $transparencyValue);
} else {
$transparencyOperation->setAmount(100 - $transparencyValue);
}
Get the Slide Background Value
Aspose.Slides provides the BackgroundEffectiveData
class for retrieving a slide’s effective background values. This class exposes the effective FillFormat and EffectFormat.
Using the BaseSlide class’s getBackground
method, you can obtain the effective background for a slide.
The following PHP example shows how to get a slide’s effective background value:
// Create an instance of the Presentation class.
$presentation = new Presentation("Sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
// Retrieve the effective background, taking into account master, layout, and theme.
$effBackground = $slide->getBackground()->getEffective();
if ($effBackground->getFillFormat()->getFillType() == FillType::Solid)
echo "Fill color: " . $effBackground->getFillFormat()->getSolidFillColor() . "\n";
else
echo "Fill type: " . $effBackground->getFillFormat()->getFillType() . "\n";
} finally {
$presentation->dispose();
}