gpt4 book ai didi

php - 图像大小调整不起作用(使用 base64)

转载 作者:行者123 更新时间:2023-12-03 22:22:56 24 4
gpt4 key购买 nike

下面的代码应该在字符串中输出调整大小的图像。不幸的是它只输出 MQ== 那么我的错误在哪里?非常感谢您的帮助:)

<?php
// The file
$filename = 'tick.png';

// Set a maximum height and width
$width = 200;
$height = 200;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

$newImage = imagepng($image_p, null, 100); // got in has to be from 0 to 9
echo base64_encode(file_get_contents($newImage)); // this still does not work°
?>

最佳答案

压缩级别不能是 100,只能是 0 到 9。

imagepng($image_p, null, from 0 to 9);

您可以使用 Base64 编码:

ob_start();
imagepng($image_p, null, 9); // got in has to be from 0 to 9
$stream = ob_get_clean();
echo base64_encode($stream);

关于php - 图像大小调整不起作用(使用 base64),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22559673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com