Repository: Intervention/image

Issue #1251: It adds black background to the image when converting from PNG to JPEG and resizing
Created: 2024-01-05T13:58:02Z
Describe the bug
Mere PNG to JPEG works fine. It converts it and the background is just white.
However, when I'm trying to convert a PNG to JPEG and resize it using scaleDown, it suddenly adds black background to the image.

Notice that it adds black background only when I combine PNG to JPEG and resizing.

Code Example
ImageManager::gd()
    ->read($pathToPng)
    ->modify(new class implements ModifierInterface {
        public function apply(ImageInterface $image): ImageInterface
        {
            // This line below adds black background to the image.
            // If you comment it out, it will work fine.
            $image->scaleDown(1400, 1400);

            return $image;
        }
    })
    ->toJpeg();
Expected behavior
Background stays the same.
Images

Original PNG Image



Image when converted to JPEG



Image when converted to JPEG *and* resized (black background)


Environment (please complete the following information):

PHP Version: 8.2.3
OS: macOS 14.0
Intervention Image Version: 3.2.1
GD or Imagick: GD

Duplicate issue #999: Remove black background image from png files  
Created: 2020-01-10T10:47:06Z
I am working with image cropping and its working file but in case of png files it generates black background.
public static function ImageCrop($file,$arDetails = array())
{
    $arReturn = array(); 
    if(!empty($file))
    {
       //get filename with extension
       $filenamewithextension = $file->getClientOriginalName();

        //get filename without extension
        $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

        //get file extension
        $extension = $file->getClientOriginalExtension();

        //filename to store
        $filenametostore = $filename.'_'.time().'.'.$extension;
        
        //make Directory
        if(!file_exists(public_path($arDetails['szUploadDirectory']))) {
            mkdir(public_path($arDetails['szUploadDirectory']), 777);
        }
        //make crop Directory
        if(!file_exists(public_path($arDetails['szUploadDirectory'].'/crop'))) {
            mkdir(public_path($arDetails['szUploadDirectory'].'/crop'), 777 );
        }
        ///$file->move(public_path($arDetails['szUploadDirectory']), $filenametostore);
        $path = public_path($arDetails['szUploadDirectory'].'/'.$filenametostore); 
        $image = Image::make($file);
        $image->rotate($arDetails['rotation']); 
        $image->save($path);
    
        // crop image
        $img = Image::make(public_path($arDetails['szUploadDirectory'].'/'.$filenametostore));
        $croppath = public_path($arDetails['szUploadDirectory'].'/crop/'.$filenametostore);
        $img->crop((int)$arDetails['width'], (int)$arDetails['height'], (int)$arDetails['x'], (int)$arDetails['y']);
        $img->save($croppath);
        // you can save crop image path below in database
        $szImagePath =$croppath;
        $arReturn['szImagePath'] = ($arDetails['szUploadDirectory'].'/crop/'.$filenametostore);
        $arReturn['szImageName'] = $filenametostore; 
       return $arReturn; 
    }
}

Closing comment (2024-01-07T11:29:31Z): Duplicate of #1251