menu 贺大礼(乱丶心)的博客 - 软件开发与技术分享
thinkphp将所有的图片都替换为webp
141 浏览 | 2025-02-12 | 阅读时间: 约 1 分钟 | 分类: php | 标签:
    public function updateImg()
    {
        set_time_limit(0);
        ini_set('memory_limit', '256M');
        $attachmentModel = app()->make(Attachment::class);

        $where = [
            ['mime_type', 'in', ['png', 'jpg', '.png', '.jpg', 'image/png', 'image/jpeg']],
        ];

        $publicPath = app()->getRootPath() . 'public';

        $list = $attachmentModel->where($where)->limit(1000)->select();
        foreach ($list as $value) {
            if (is_numeric($value->size) && $value->size < 10) {
                continue;
            }

            $url = $value->url;
            $url = str_replace('\\', '/', $url);
            if (strpos($url, 'http') !== false) {
                continue;
            }
            if (strpos($url, '/') !== 0) {
                $url = '/' . $url;
            }
            $filePath = $publicPath . $url;

            // 更换文件路径的后缀
            if (file_exists($filePath)) {
                $fileInfo = pathinfo($filePath);
                if (!empty($fileInfo['extension'])) {
                    $newFilePath = $fileInfo['dirname'] . '/' . $fileInfo['filename'] . '.webp';
                    $newUrl = str_replace($fileInfo['extension'], 'webp', $url);
                    if (file_exists($newFilePath)) {
                        $value->name = str_replace($fileInfo['extension'], 'webp', $value->name);
                        $value->mime_type = 'image/webp';
                        $value->size = filesize($newFilePath) / 1024;
                        $value->url = $newUrl;
                        $value->save();
                        continue;
                    }
                    $res = $this->img2webp($filePath, $newFilePath);
                    if ($res) {
                        $value->name = str_replace($fileInfo['extension'], 'webp', $value->name);
                        $value->mime_type = 'image/webp';
                        $value->size = filesize($newFilePath) / 1024;
                        $value->url = $newUrl;
                        $value->save();
                    }
                }

            }
        }

    }
    
    public function img2webp($sourcePath, $webpPath)
    {
//// 原始图片路径
//    $sourcePath = 'path/to/your/image.jpg';
//// 输出WebP图片路径
//    $webpPath = 'path/to/your/image.webp';

        $fileInfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimeType = finfo_file($fileInfo, $sourcePath);
        finfo_close($fileInfo);

        if ($mimeType === 'image/png') {
            $image = imagecreatefrompng($sourcePath); // 如果是PNG,用imagecreatefrompng
        } elseif ($mimeType === 'image/jpeg') {
            $image = imagecreatefromjpeg($sourcePath); // 如果是JPG,用imagecreatefromjpeg
        } else {
            return false;
        }

// 保存为WebP格式
        imagepalettetotruecolor($image); // 将调色板转换为真彩色,对于某些图像很重要
        imagealphablending($image, true); // 开启混合模式
        imagesavealpha($image, true); // 保存完整alpha通道信息
        imagewebp($image, $webpPath, 70); // 保存为WebP格式,90是质量参数,范围是0-100

// 释放内存
        imagedestroy($image);
        return true;
    }
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

发表评论

email
web

全部评论 (暂无评论)

info 还没有任何评论,你来说两句呐!