更改图像的尺寸但仅声明宽度

时间:2017-09-04 16:47:06

标签: php image

我有这段代码:

<?php
// The file
$filename = 'http://www.tagesschau.de/multimedia/bilder/feinstaub-messstation-101.jpg';
$new_width = 400;
$new_height = 200;

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

// Get new dimensions
list($width, $height) = getimagesize($filename);

// 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);
?>

调用此脚本时,它会显示图像http://www.tagesschau.de/multimedia/bilder/feinstaub-messstation-101.jpg,但会在之前调整图像大小:400 x 200。

您可以使用$new_width$new_height中的值声明宽度和高度。我需要指定宽度和高度。

我想要达到的目标:我只想声明宽度。高度应根据原始尺寸动态调整。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

获取新宽度与原始宽度的比率,然后将该因子应用于高度:

$factor = $new_width / $width;
$new_height = $height * $factor;