现代API

介绍

从历史上看,Aspose Slides 依赖于 java.awt,并在其公共 API 中包含以下类:

截至版本 24.4,此公共 API 已声明为已弃用。

为了摆脱对这些类的依赖,我们添加了所谓的“现代 API”——即应替代已弃用的 API 使用的 API,其签名包含对 BufferedImage 的依赖。Graphics2D 被声明为已弃用,其支持将从公共 Slides API 中移除。

带有对 System.Drawing 依赖的已弃用公共 API 的删除将在 24.8 版本中进行。

现代API

向公共 API 添加了以下类和枚举:

  • IImage - 表示光栅或矢量图像。
  • ImageFormat - 表示图像的文件格式。
  • Images - 用于实例化和处理 IImage 接口的方法。

请注意,IImage 是可处置的(它实现了 IDisposable 接口,使用时应包裹在 using 中或以其他方便的方式处置)。

使用新 API 的典型场景如下所示:

use aspose\slides\Presentation;
use aspose\slides\ShapeType;
use aspose\slides\ImageFormat;
use aspose\slides\Images;

$pres = new Presentation();

# 从磁盘上的文件实例化一个可处置的 IImage 实例。
$image = Images::fromFile("image.png");

# 通过将 IImage 实例添加到演示文稿的图像中来创建 PowerPoint 图像。
$ppImage = $pres->getImages()->addImage($image);
$image->dispose();

# 在幻灯片 #1 上添加一个图片形状
$pres->getSlides()->get_Item(0)->getShapes()->addPictureFrame(ShapeType::Rectangle, 10, 10, 100, 100, $ppImage);

$dimension = new Java("java.awt.Dimension", 1920, 1080);
# 获取表示幻灯片 #1 的 IImage 实例。
$slideImage = $pres->getSlides()->get_Item(0)->getImage($dimension);

# 将图像保存到磁盘。
$slideImage->save("slide1.jpeg", ImageFormat::Jpeg);
$slideImage->dispose();

$pres->dispose();

用现代API替换旧代码

一般来说,您需要用新方法替换对使用 ImageIO 的旧方法的调用。

旧:

$dimension = new Java("java.awt.Dimension", 1920, 1080);
$slideImage = $pres->getSlides()->get_Item(0)->getThumbnail($dimension);
$imageio = new Java("javax.imageio.ImageIO");
$javafile = new Java("java.io.File", "image.png");
$imageio->write($slideImage, "PNG", $javafile);

新:

$dimension = new Java("java.awt.Dimension", 1920, 1080);
$slideImage = $pres->getSlides()->get_Item(0)->getImage($dimension);
$slideImage->save("image.png", ImageFormat::Png);
$slideImage->dispose();

获取幻灯片缩略图

使用已弃用 API 的代码:

use aspose\slides\Presentation;

$pres = new Presentation("pres.pptx");

$slideImage = $pres->getSlides()->get_Item(0)->getThumbnail();

$imageio = new Java("javax.imageio.ImageIO");
$javafile = new Java("java.io.File", "slide1.png");
$imageio->write($slideImage, "PNG", $javafile);

$pres->dispose();

现代 API:

use aspose\slides\Presentation;
use aspose\slides\ImageFormat;

$pres = new Presentation("pres.pptx");

$slideImage = $pres->getSlides()->get_Item(0)->getImage();
$slideImage->save("slide1.png", ImageFormat::Png);
$slideImage->dispose();

$pres->dispose();

获取形状缩略图

使用已弃用 API 的代码:

use aspose\slides\Presentation;

$pres = new Presentation("pres.pptx");

$shapeImage = $pres->getSlides()->get_Item(0)->getShapes()->get_Item(0)->getThumbnail();

$imageio = new Java("javax.imageio.ImageIO");
$javafile = new Java("java.io.File", "shape.png");
$imageio->write($shapeImage, "PNG", $javafile);

$pres->dispose();

现代 API:

use aspose\slides\Presentation;
use aspose\slides\ImageFormat;

$pres = new Presentation("pres.pptx");

$shapeImage = $pres->getSlides()->get_Item(0)->getShapes()->get_Item(0)->getImage();
$shapeImage->save("shape.png");
$shapeImage->dispose();

$pres->dispose();

获取演示文稿缩略图

使用已弃用 API 的代码:

use aspose\slides\Presentation;
use aspose\slides\RenderingOptions;

$pres = new Presentation("pres.pptx");

$renderingOptions = new RenderingOptions();
$dimension = new Java("java.awt.Dimension", 1920, 1080);

$bitmaps = $pres->getThumbnails($renderingOptions, $dimension);
for ($i = 0; $i < count(java_values($bitmaps)); $i++)
{
    $thumbnail = $bitmaps[$i];
    $imageio = new Java("javax.imageio.ImageIO");
    $javafile = new Java("java.io.File", "slide" . $i . ".png");
    $imageio->write($thumbnail, "PNG", $javafile);
}

$pres->dispose();

现代 API:

use aspose\slides\Presentation;
use aspose\slides\ImageFormat;
use aspose\slides\RenderingOptions;

$pres = new Presentation("pres.pptx");

$renderingOptions = new RenderingOptions();
$dimension = new Java("java.awt.Dimension", 1920, 1080);

$images = $pres->getImages($renderingOptions, $dimension);
for ($i = 0; $i < count(java_values($images)); $i++)
{
    $thumbnail = $images[$i];
    $thumbnail->save("slide" . $i . ".png", ImageFormat::Png);
}

$pres->dispose();

向演示文稿添加图片

使用已弃用 API 的代码:

use aspose\slides\Presentation;
use aspose\slides\ShapeType;

$pres = new Presentation();

$imageio = new Java("javax.imageio.ImageIO");
$javafile = new Java("java.io.File", "image.png");

$bufferedImages = $imageio->read($javafile);
$ppImage = $pres->getImages()->addImage($bufferedImages);

$pres->getSlides()->get_Item(0)->getShapes()->addPictureFrame(ShapeType::Rectangle, 10, 10, 100, 100, $ppImage);

$pres->dispose();

现代 API:

use aspose\slides\Presentation;
use aspose\slides\Images;
use aspose\slides\ShapeType;

$pres = new Presentation();

$image = Images::fromFile("image.png");
$ppImage = $pres->getImages()->addImage($image);
$image->dispose();

$pres->getSlides()->get_Item(0)->getShapes()->addPictureFrame(ShapeType::Rectangle, 10, 10, 100, 100, $ppImage);

$pres->dispose();

将要删除的方法及其在现代 API 中的替换

Presentation

方法签名 替换方法签名
public final BufferedImage[] getThumbnails(IRenderingOptions options) public final IImage[] getImages(IRenderingOptions options)
public final BufferedImage[] getThumbnails(IRenderingOptions options, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, float scaleX, float scaleY)
public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides) public final IImage[] getImages(IRenderingOptions options, int[] slides)
public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, int[] slides, float scaleX, float scaleY)
public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides, Dimension imageSize) public final IImage[] getImages(IRenderingOptions options, int[] slides, Dimension imageSize)
public final BufferedImage[] getThumbnails(IRenderingOptions options, Dimension imageSize) public final IImage[] getImages(IRenderingOptions options, Dimension imageSize)

Shape

方法签名 替换方法签名
public final BufferedImage getThumbnail() public final IImage getImage()
public final BufferedImage getThumbnail(int bounds, float scaleX, float scaleY) public final IImage getImage(int bounds, float scaleX, float scaleY)

Slide

方法签名 替换方法签名
public final BufferedImage getThumbnail() public final IImage getImage()
public final BufferedImage getThumbnail(float scaleX, float scaleY) public final IImage getImage(float scaleX, float scaleY)
public final BufferedImage getThumbnail(IRenderingOptions options) public final IImage getImage(IRenderingOptions options)
public final BufferedImage getThumbnail(IRenderingOptions options, float scaleX, float scaleY) public final IImage getImage(IRenderingOptions options)
public final BufferedImage getThumbnail(IRenderingOptions options, Dimension imageSize) public final IImage getImage(IRenderingOptions options, Dimension imageSize)
public final BufferedImage getThumbnail(ITiffOptions options) public final IImage getImage(ITiffOptions options)
public final BufferedImage getThumbnail(Dimension imageSize) public final IImage getImage(Dimension imageSize)
public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics) 将完全删除
public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, float scaleX, float scaleY) 将完全删除
public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, Dimension renderingSize) 将完全删除

输出

方法签名 替换方法签名
public final IOutputFile add(String path, BufferedImage image) public final IOutputFile add(String path, IImage image)

ImageCollection

方法签名 替换方法签名
public final IPPImage addImage(BufferedImage image) public final IPPImage addImage(IImage image)

PPImage

方法签名 替换方法签名
public final BufferedImage getSystemImage() public final IImage getImage()

PatternFormat

方法签名 替换方法签名
public final BufferedImage getTileImage(Color styleColor) public final IImage getTile(Color styleColor)
public final BufferedImage getTileImage(Color background, Color foreground) public final IImage getTile(Color background, Color foreground)

PatternFormatEffectiveData

方法签名 替换方法签名
public final java.awt.image.BufferedImage getTileImage(Color background, Color foreground) public final IImage getTileIImage(Color background, Color foreground)

对 Graphics2D 的 API 支持将被取消

使用 Graphics2D 的方法被声明为已弃用,并将从公共 API 中移除。

使用它的 API 部分将被移除:

Slide