我在浏览器截图中遇到了这个问题,其中生成的具有透明背景的Base64图像被截断:How do I resize an image without it getting cropped while maintaining aspect ratio in browsershot headerHtml?

因此,我想在用户上传时将图像的背景色从透明转换为白色,以防止此问题发生.然而,我没有找到任何关于这方面的现有资源,只有反过来.

如何在php/laravel中做到这一点?谢谢

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

推荐答案

有多种方法可以做到这一点,一种方法是使用php gd library

在您的情况下,它的工作原理如下:

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

Php相关问答推荐

根据在WooCommerce购物车和 checkout 中 Select 的送货方式显示快捷代码内容

如何限制WordPress自定义分类术语页面中的术语数量

Laravel关系-为用户获取属于组织的所有团队

为什么本地主机(Xampp)上的laravel运行速度比普通html站点慢?

如果为布尔值,则 for each 循环重新启动

为什么PHP PDO忽略NOT NULL约束?

除WooCommerce中的特定国家/地区外,有条件地要求填写邮政编码 checkout 字段

对产品/库存结果集进行分组和计数,形成多维分层数组

WooCommerce 以编程方式添加的费用不会持续存在

PHP API - 本地主机中的 SSL 证书问题

我不明白为什么我收到未定义的数组键异常错误 - Laravel 9 PHP 8

为什么 debug_backtrace() 不返回任何内容?

如何正确判断 PHP 是否已正确配置为使用 DOMDocument?

PHP 中的curl -F request=@file.xml

Shopware 6 AuthControllerDecorator已弃用

Laravel 8:会话不适用

PHP/Laravel 中的自定义时区

使用 Eloquent 更新现有的外键值

服务器发送的事件:EventSource 抛出 MIME 错误

检测字符串是否包含Letter with Tilde或Letter + Combining Tilde