gpt4 book ai didi

php - 使用 PHP 调整图像大小

转载 作者:行者123 更新时间:2023-12-05 00:15:31 24 4
gpt4 key购买 nike

我有一个快速问题,但我不太确定是否可以设置。我在其他地方看到过例子,但没有什么特别像我的情况。我想使用 PHP 调整图像的大小,以便它们可读,而不是像使用 HTML 那样随意拉伸(stretch)。如果它们不是 250 像素宽或 160 像素高,我如何调整图片大小使其成比例但适合该空间?

谢谢!

最佳答案

PHP 不直接操作图像。您将需要使用图像处理库,例如 gdImageMagick实现这个目标。

在 ImageMagick 中,image resizing是这样完成的:

$thumb = new Imagick('myimage.gif');

$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');

有了 GD,您可以 do it like this :

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

关于php - 使用 PHP 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7393319/

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