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 idea自动补全键位冲突
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页