doubingjian2006 2012-10-11 23:08
浏览 81
已采纳

PHP调整图像大小保持宽高比到精确大小

I need to manually find DPI of new image.

$input_width = 361;
$input_height = 413;

$input_dpi_x = 72;
$input_dpi_y = 72;

$output_width = 800;
$output_height = $input_height * $output_width / $input_width;

$output_dpi_x = ceil(($input_dpi_x / $input_width) * $output_width);
$output_dpi_y = ceil(($input_dpi_y / $input_height) * $output_y_res);

echo "Outpud_dpi_x = " . $output_dpi_x;
//Outpud_dpi_x = 160

Why when i resize image i get 802 instead of 800?

and i must use DPI dont ask why

  • 写回答

1条回答 默认 最新

  • douwendu2460 2012-10-12 13:44
    关注

    The answer is all in the math... Let's just focus on the width for simplicity.

    Pulling back the layers a little bit, start with scaling operations (reordered to help with loss of precision). Here, I'm calculating the output DPI value and verifying the result by solving the original equation for $output_width.

    $output_dpi_x = $output_width * $input_dpi_x / $input_width;  // 159.5567867...
    $output_width = $output_dpi_x * $input_width / $input_dpi_x;  // 800
    

    You can see with this non-corrected value for DPI, we arrive back at 800 for the width value. When we round up to the next number (using the ceil operator), it changes the results. By unwinding the math, we can see why you end up with 802px in the output:

    $output_dpi_x = ceil($output_dpi_x);  // 160
    $output_width = $output_dpi_x * $input_width / $input_dpi_x;  // 802.22222222...
    

    Of course, images can't contain partial pixels, so your resized image is rounded down to 802px.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵