Convert PNG to JPG in PHP

Place a PNG image on a slide and render the slide in JPEG format with Aspose.Slides for PHP via Java.

Convert PNG to JPG in PHP

Aspose.Slides for PHP via Java can place a PNG image on a presentation slide and render the slide as a JPEG image.

Load the PNG with Images::fromFile, resize the slide with setSize, add the image with addPictureFrame, and render the slide with getImage. Because JPEG does not support transparency, transparent PNG areas are rendered against the slide background.

Convert PNG to JPG using PHP

Create a Presentation, match the slide size to the PNG, place the image on the slide, call getImage, and save the rendered image with ImageFormat::Jpeg.

PHP code for converting PNG into JPG

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $sourceImage = Images::fromFile("input.png");
    try {
        $embeddedImage = $presentation->getImages()->addImage($sourceImage);
    } finally {
        $sourceImage->dispose();
    }

    $imageWidth = java_values($embeddedImage->getWidth());
    $imageHeight = java_values($embeddedImage->getHeight());

    $presentation->getSlideSize()->setSize(
        $imageWidth, $imageHeight, SlideSizeScaleType::DoNotScale);

    $slide->getShapes()->addPictureFrame(
        ShapeType::Rectangle, 0, 0, $imageWidth, $imageHeight, $embeddedImage);

    $slideImage = $slide->getImage(1.0, 1.0);
    try {
        $slideImage->save("output.jpg", ImageFormat::Jpeg);
    } finally {
        $slideImage->dispose();
    }
} finally {
    $presentation->dispose();
}

How to convert PNG to JPG using Aspose.Slides for PHP API

These are the steps to convert PNG to JPG in PHP.

  1. Install Aspose.Slides for PHP via Java .

  2. Create a Presentation, access its first slide, and load the PNG with Images::fromFile.

  3. Set the slide dimensions with setSize, then place the image on the slide with addPictureFrame.

  4. Render the slide with getImage and save it with ImageFormat::Jpeg.

Free Online Converter

Try our free Conversion app

Convert PNG To Other Supported Formats

You can also convert PNG and save to other file formats. See all supported formats below: