image icon indicating copy to clipboard operation
image copied to clipboard

Remove black background image from png files

Open PRVN007 opened this issue 6 years ago • 1 comments

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; 
    }
}

PRVN007 avatar Jan 10 '20 10:01 PRVN007

i have so

vahidalvandi avatar Aug 04 '21 05:08 vahidalvandi

Duplicate of https://github.com/Intervention/image/issues/1251

olivervogel avatar Jan 07 '24 11:01 olivervogel