Picture Frame
A picture frame is a shape that contains an image—it is like a picture in a frame.
You can add an image to a slide through a picture frame. This way, you get to format the image by formatting the picture frame.
Tip
Aspose provides free converters—JPEG to PowerPoint and PNG to PowerPoint—that allow people to create presentations quickly from images.Create Picture Frame
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
- Specify the image’s width and height.
- Create a PictureFrame based on the image’s width and height through the
AddPictureFrame
method exposed by the shape object associated with the referenced slide. - Add a picture frame (containing the picture) to the slide.
- Write the modified presentation as a PPTX file.
This PHP code shows you how to create a picture frame:
# Instantiates the Presentation class that represents a PPTX file
$pres = new Presentation();
try {
# Gets the first slide
$sld = $pres->getSlides()->get_Item(0);
# Instantiates the Image class
$imgx = $pres->getImages()->addImage(new Java("java.io.FileInputStream", new Java("java.io.File", "asp1.jpg")));
# Adds a picture frame with the picture's equivalent height and width
$sld->getShapes()->addPictureFrame(ShapeType::Rectangle, 50, 150, $imgx->getWidth(), $imgx->getHeight(), $imgx);
# Write the PPTX file to disk
$pres->save("RectPicFrame.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Create Picture Frame with Relative Scale
By altering an image’s relative scaling, you can create a more complicated picture frame.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Add an image to the presentation image collection.
- Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
- Specify the image’s relative width and height in the picture frame.
- Write the modified presentation as a PPTX file.
This PHP code shows you how to create a picture frame with relative scale:
# Instantiate Presentation class that represents the PPTX
$pres = new Presentation();
try {
# Get the first slide
$sld = $pres->getSlides()->get_Item(0);
# Instantiate the Image class
$imgx = $pres->getImages()->addImage(new Java("java.io.FileInputStream", new Java("java.io.File", "asp1.jpg")));
# Add Picture Frame with height and width equivalent of Picture
$pf = $sld->getShapes()->addPictureFrame(ShapeType::Rectangle, 50, 150, $imgx->getWidth(), $imgx->getHeight(), $imgx);
# Setting relative scale width and height
$pf->setRelativeScaleHeight(0.8);
$pf->setRelativeScaleWidth(1.35);
# Write the PPTX file to disk
$pres->save("RectPicFrame.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Extract Image from Picture Frame
You can extract images from PictureFrame objects and save them in PNG, JPG, and other formats. The code example below demonstrates how to extract an image from the document “sample.pptx” and save it in PNG format.
$presentation = new Presentation("sample.pptx");
try {
$firstSlide = $presentation->getSlides()->get_Item(0);
$firstShape = $firstSlide->getShapes()->get_Item(0);
if (java_instanceof($firstShape, new JavaClass("com.aspose.slides.PictureFrame"))) {
$pictureFrame = $firstShape;
try {
$slideImage = $pictureFrame->getPictureFormat()->getPicture()->getImage()->getImage();
$slideImage->save("slide_1_shape_1.png", ImageFormat::Png);
} finally {
if (!java_is_null($slideImage)) {
$slideImage->dispose();
}
}
}
} catch (JavaException $e) {
} finally {
$presentation->dispose();
}
Get Transparency of Image
Aspose.Slides allows you to get the transparency of an image. This PHP code demonstrates the operation:
$presentation = new Presentation($folderPath . "Test.pptx");
$pictureFrame = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$imageTransform = $pictureFrame->getPictureFormat()->getPicture()->getImageTransform();
foreach($imageTransform as $effect) {
if (java_instanceof($effect, new JavaClass("com.aspose.slides.AlphaModulateFixed"))) {
$alphaModulateFixed = $effect;
$transparencyValue = 100 - $alphaModulateFixed->getAmount();
echo("Picture transparency: " . $transparencyValue);
}
}
Picture Frame Formatting
Aspose.Slides provides many formatting options that can be applied to a picture frame. Using those options, you can alter a picture frame to make it match specific requirements.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
- Specify the image’s width and height.
- Create a
PictureFrame
based on the image’s width and height through the AddPictureFrame method exposed by the IShapes object associated with the referenced slide. - Add the picture frame (containing the picture) to the slide.
- Set the picture frame’s line color.
- Set the picture frame’s line width.
- Rotate the picture frame by giving it either a positive or negative value.
- A positive value rotates the image clockwise.
- A negative value rotates the image anti-clockwise.
- Add the picture frame (containing the picture) to the slide.
- Write the modified presentation as a PPTX file.
This PHP code demonstrates the picture frame formatting process:
# Instantiates the Presentation class that represents the PPTX
$pres = new Presentation();
try {
# Gets the first slide
$sld = $pres->getSlides()->get_Item(0);
# Instantiates the Image class
$imgx = $pres->getImages()->addImage(new Java("java.io.FileInputStream", new Java("java.io.File", "asp1.jpg")));
# Adds Picture Frame with height and width equivalent of Picture
$pf = $sld->getShapes()->addPictureFrame(ShapeType::Rectangle, 50, 150, $imgx->getWidth(), $imgx->getHeight(), $imgx);
# Applies some formatting to PictureFrameEx
$pf->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$pf->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
$pf->getLineFormat()->setWidth(20);
$pf->setRotation(45);
# Writes the PPTX file to disk
$pres->save("RectPicFrame.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Tip
Aspose recently developed a free Collage Maker. If you ever need to merge JPG/JPEG or PNG images, create grids from photos, you can use this service.Add Image as Link
To avoid large presentation sizes, you can add images (or videos) through links instead of embedding the files directly into presentations. This PHP code shows you how to add an image and video into a placeholder:
$presentation = new Presentation("input.pptx");
try {
$shapesToRemove = new Java("java.util.ArrayList");
$shapesCount = $presentation->getSlides()->get_Item(0)->getShapes()->size();
for($i = 0; $i < java_values($shapesCount) ; $i++) {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item($i);
if (java_is_null($autoShape->getPlaceholder())) {
continue;
}
switch ($autoShape->getPlaceholder()->getType()) {
case PlaceholderType::Picture :
$pictureFrame = $presentation->getSlides()->get_Item(0)->getShapes()->addPictureFrame(ShapeType::Rectangle, $autoShape->getX(), $autoShape->getY(), $autoShape->getWidth(), $autoShape->getHeight(), null);
$pictureFrame->getPictureFormat()->getPicture()->setLinkPathLong("https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");
$shapesToRemove->add($autoShape);
break;
case PlaceholderType::Media :
$videoFrame = $presentation->getSlides()->get_Item(0)->getShapes()->addVideoFrame($autoShape->getX(), $autoShape->getY(), $autoShape->getWidth(), $autoShape->getHeight(), "");
$videoFrame->getPictureFormat()->getPicture()->setLinkPathLong("https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg");
$videoFrame->setLinkPathLong("https://youtu.be/t_1LYZ102RA");
$shapesToRemove->add($autoShape);
break;
}
}
foreach($shapesToRemove as $shape) {
$presentation->getSlides()->get_Item(0)->getShapes()->remove($shape);
}
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
Crop Image
This PHP code shows you how to crop an existing image on a slide:
$pres = new Presentation();
# Creates new image object
try {
$picture;
$image = Images->fromFile($imagePath);
try {
$picture = $pres->getImages()->addImage($image);
} finally {
if (!java_is_null($image)) {
$image->dispose();
}
}
# Adds a PictureFrame to a Slide
$picFrame = $pres->getSlides()->get_Item(0)->getShapes()->addPictureFrame(ShapeType::Rectangle, 100, 100, 420, 250, $picture);
# Crops the image (percentage values)
$picFrame->getPictureFormat()->setCropLeft(23.6);
$picFrame->getPictureFormat()->setCropRight(21.5);
$picFrame->getPictureFormat()->setCropTop(3);
$picFrame->getPictureFormat()->setCropBottom(31);
# Saves the result
$pres->save($outPptxFile, SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Delete Cropped Areas of Picture
If you want to delete the cropped areas of an image contained in a frame, you can use the deletePictureCroppedAreas() method. This method returns the cropped image or the origin image if cropping is unnecessary.
This PHP code demonstrates the operation:
$presentation = new Presentation("PictureFrameCrop.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
# Gets the PictureFrame from the first slide
$picFrame = $slide->getShapes()->get_Item(0);
# Deletes cropped areas of the PictureFrame image and returns the cropped image
$croppedImage = $picFrame->getPictureFormat()->deletePictureCroppedAreas();
# Saves the result
$presentation->save("PictureFrameDeleteCroppedAreas.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
NOTE
The deletePictureCroppedAreas() method adds the cropped image to the presentation image collection. If the image is only used in the processed PictureFrame, this setup can reduce the presentation size. Otherwise, the number of images in the resulting presentation will increase.
This method converts WMF/EMF metafiles to raster PNG image in the cropping operation.
Lock Aspect Ratio
If you want a shape containing an image to retain its aspect ratio even after you change the image dimensions, you can use the setAspectRatioLocked method to set the Lock Aspect Ratio setting.
This PHP code shows you how to lock a shape’s aspect ratio:
$pres = new Presentation("pres.pptx");
try {
$layout = $pres->getLayoutSlides()->getByType(SlideLayoutType::Custom);
$emptySlide = $pres->getSlides()->addEmptySlide($layout);
$picture;
$image = Images->fromFile("image.png");
try {
$picture = $pres->getImages()->addImage($image);
} finally {
if (!java_is_null($image)) {
$image->dispose();
}
}
$pictureFrame = $emptySlide->getShapes()->addPictureFrame(ShapeType::Rectangle, 50, 150, $presImage->getWidth(), $presImage->getHeight(), $picture);
# set shape to have to preserve aspect ratio on resizing
$pictureFrame->getPictureFrameLock()->setAspectRatioLocked(true);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
NOTE
This Lock Aspect Ratio setting preserves only the aspect ratio of the shape and not the image it contains.Use StretchOff Property
Using the StretchOffsetLeft, StretchOffsetTop, StretchOffsetRight and StretchOffsetBottom properties from the IPictureFillFormat interface and PictureFillFormat class, you can specify a fill rectangle.
When stretching is specified for an image, a source rectangle is scaled to fit the specified fill rectangle. Each edge of the fill rectangle is defined by a percentage offset from the corresponding edge of the shape’s bounding box. A positive percentage specifies an inset while a negative percentage specifies an outset.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Add a rectangle
AutoShape
. - Create an image.
- Set the shape’s fill type.
- Set the shape’s picture fill mode.
- Add a set image to fill the shape.
- Specify image offsets from the corresponding edge of the shape’s bounding box
- Write the modified presentation as a PPTX file.
This PHP code demonstrates a process in which a StretchOff property is used:
# Instantiates the Prseetation class that represents a PPTX file
$pres = new Presentation();
try {
# Gets the first slide
$slide = $pres->getSlides()->get_Item(0);
# Instantiates the ImageEx class
$picture;
$image = Images->fromFile("aspose-logo.jpg");
try {
$picture = $pres->getImages()->addImage($image);
} finally {
if (!java_is_null($image)) {
$image->dispose();
}
}
# Adds an AutoShape set to Rectangle
$aShape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 300, 300);
# Sets the shape's fill type
$aShape->getFillFormat()->setFillType(FillType::Picture);
# Sets the shape's picture fill mode
$aShape->getFillFormat()->getPictureFillFormat()->setPictureFillMode(PictureFillMode->Stretch);
# Sets the image to fill the shape
$aShape->getFillFormat()->getPictureFillFormat()->getPicture()->setImage($picture);
# Specifies the image offsets from the corresponding edge of the shape's bounding box
$aShape->getFillFormat()->getPictureFillFormat()->setStretchOffsetLeft(25);
$aShape->getFillFormat()->getPictureFillFormat()->setStretchOffsetRight(25);
$aShape->getFillFormat()->getPictureFillFormat()->setStretchOffsetTop(-20);
$aShape->getFillFormat()->getPictureFillFormat()->setStretchOffsetBottom(-10);
# Writes the PPTX file to disk
$pres->save("StretchOffsetLeftForPictureFrame_out.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}