Convert PNG to JPG and set transparent background to white with ImageMagick and PHP

前端 未结 2 982
盖世英雄少女心
盖世英雄少女心 2021-02-03 23:48

How can I use ImageMagick (with the php extension) to set the transparent background to white when converting an image from PNG to JPEG?

2条回答
  •  花落未央
    2021-02-03 23:57

    If you are using the Imagick extension:

    setImageBackgroundColor(new ImagickPixel('white'));
    
    // flattens multiple layers
    $i = $i->flattenImages();
    
    // the output format
    $i->setImageFormat('jpg');
    
    // save to disk
    $i->writeImage('image.jpg');
    
    // and/or output directly
    // header('Content-Type: '.$i->getFormat());
    // echo $i->getImageBlob();
    
    // cleanup
    $i->clear();
    $i->destroy();
    

提交回复
热议问题