Remove Image background with php and save transparent png

后端 未结 7 1253
心在旅途
心在旅途 2020-12-15 01:23

I want to remove the white background of any image uploaded on the site working on PHP platform. The uploading function is done but messed up with this functionality.

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 02:01

    Version to conversion from URL and return on page:

    $img = imagecreatefromjpeg('http://mypage.com/image.jpg');
    
    $remove = imagecolorallocate($img, 255, 255, 255); // Define color rgb to remove
    imagecolortransparent($img, $remove);
    
    ob_start();
    imagepng($img);
    $imgData = ob_get_clean();
    imagedestroy($img);
    
    $data_img = 'data:image/png;base64,'.base64_encode($imgData);
    echo '';
    

提交回复
热议问题