dongting7352 2016-10-29 14:52
浏览 59
已采纳

在PHP中调整图像大小会创建黑色图像

I wanted to resize an image in PHP using the GD library.

I found this question, which helped me alot. It allowed me to do exactly what I wanted. I changed the default value to true instead of the false in the answer, and I also added a way to resize png as well.

This resize_image function worked splendidly for all the images, until I wanted to scale down this image to 400x128 (landscape).

This created a black image. I even removed the parts I added on, and used the plain function copied from the question, and it still rendered black. This particular image seems to have trouble with other image sizes such as 400x400 as well. It all works as expected if you disable the crop option.

Is there something wrong with the math included in the function? I tried doing the math on paper replacing variables with the numbers I wanted, but I got confused midway.

PHPfiddle for an example. Try changing the values in the second last line to something else. It partially works (not to scale, even though cropping should be to scale).

  • 写回答

1条回答 默认 最新

  • douwen8424 2016-10-29 15:37
    关注

    When I debugged it, the width was negative. So, if I changed line 9 to the following

    $width = abs(ceil($width-($width*abs($r-$w/$h))));
    

    it's not black anymore. I don't check the formula whether it is correct or not as expected, but that abs() will make your image not black anymore.

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

报告相同问题?

悬赏问题

  • ¥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键失灵