php - 使用 PHP 调整图像大小

标签 php image

我有一个快速问题,但我不太确定是否可以设置。我在其他地方看到过例子,但没有什么特别像我的情况。我想使用 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/

相关文章:

php - 在 phpBB 中调整按钮大小

php - 如何根据点击的链接不同地填充 1 个 php 页面?

php - 用 PHP 隐藏照片中的 secret

python - 如何使用 OpenCV 在图像上标记数字并绘制圆圈

css - 如何通过重新格式化输出不断从 img 更改为 anchor 和 div 背景

PHP/IIS copy()/move_uploaded_file() 无法打开流 : Permission denied Warning

php - 多主题wordpress插件的想法

php 转到变量

java - 如何压缩一批图像?

CSS 背景图像缩放以适合屏幕高度