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 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的