duandgfh0506
2017-09-04 16:47 阅读 26
已采纳

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

I have this code:

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

When calling this script it'll show the image http://www.tagesschau.de/multimedia/bilder/feinstaub-messstation-101.jpg but resize the image before: 400 x 200.

You can declare width and height with the values in $new_width and $new_height. I need to specify width and height.

What I want to reach: I want to only declare width. Height should adapt dynamically proportional to the original dimensions. Is there a way to do this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    drd43058 drd43058 2017-09-04 16:51

    Get the ratio of new to original width, then apply that factor to height:

    $factor = $new_width / $width;
    $new_height = $height * $factor;
    
    点赞 评论 复制链接分享

相关推荐