1

I have this problem with browsershot wherein generated base64 image with transparent background is being truncated: How do I resize an image without it getting cropped while maintaining aspect ratio in browsershot headerHtml?

Hence, I wanted to convert the image's background color from transparent to white on user upload to prevent this issue from occurring. However, I don't find any existing resources on this, only the other way around.

How to do it in php/laravel? thank you

    public function updatePdoImage($request, $id)
    {
        if (!$request->file('profile_image')) {
            return;
        }

        $request->validate([
            'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
        ]);

        $file = $request->file('profile_image');

        $image_properties = $this->uploadImage($file, 'uploads/');

        $data = [
            'file_name' => $image_properties[0],
            'file_dir' => $image_properties[1],
            'file_type' => $image_properties[2],
            'file_size' => $image_properties[3]
        ];

        $pdo = $this->show($id);

        // Convert the image to Base64
        $base64Image = base64_encode(file_get_contents($data['file_dir']));

        $data['grant_proposal_logo'] = $base64Image;

        // saving data in DB
        $this->savingUploadImage($pdo, $data);
    }
lance2k
  • 357
  • 1
  • 4
  • 14

1 Answers1

1

There are multiple ways to do it, one way would be to use php gd library

In your case it would work like this:

public function updatePdoImage($request, $id)
{
    if (!$request->file('profile_image')) {
        return;
    }

    $request->validate([
        'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
    ]);

    $file = $request->file('profile_image');

    $image_properties = $this->uploadImage($file, 'uploads/');

    $data = [
        'file_name' => $image_properties[0],
        'file_dir' => $image_properties[1],
        'file_type' => $image_properties[2],
        'file_size' => $image_properties[3]
    ];

    $pdo = $this->show($id);

    // Open the image using GD
    $image = imagecreatefrompng($data['file_dir']);

    // Get the dimensions of the image
    $width = imagesx($image);
    $height = imagesy($image);

    // Create a new white background image
    $newImage = imagecreatetruecolor($width, $height);
    $white = imagecolorallocate($newImage, 255, 255, 255);
    imagefill($newImage, 0, 0, $white);

    // Copy the original image onto the white background
    imagecopy($newImage, $image, 0, 0, 0, 0, $width, $height);

    // Save the modified image
    imagepng($newImage, $data['file_dir']);

    // Convert the modified image to Base64
    $base64Image = base64_encode(file_get_contents($data['file_dir']));

    $data['grant_proposal_logo'] = $base64Image;

    // saving data in DB
    $this->savingUploadImage($pdo, $data);

    // Free up memory
    imagedestroy($image);
    imagedestroy($newImage);
}
Tobias
  • 845
  • 10
  • 19