gpt4 book ai didi

php - 为什么有imageCreateFromString就有imageCreateFrom*?

转载 作者:IT王子 更新时间:2023-10-28 23:54:42 26 4
gpt4 key购买 nike

我真的不明白为什么 GD 有不同的函数来加载图像,例如:

imagecreatefromjpeg() 
imagecreatefrompng()
imagecreatefromgif()

如果图片是字符串的话只有一个函数?

imagecreatefromstring()

确实,将图像读入字符串并将其传递给函数要好得多,例如:

$imgBlob = file_get_contents($imagePath);
imagecreatefromstring($imageBlob);
unset($imgBlob); //> Free memory, but I am not interested in memory consumpation

?或者我错过了什么?这可能会给新用户带来潜在的困惑

也许他们只是忘记创建函数 imageCreateFromFile()

Ps. 当然,我对使用file_get_contents 方法的内存消耗不感兴趣

最佳答案

imagecreatefromstring() 对传递的图像类型运行一个开关,检查您的系统是否支持该图像类型,然后实际运行正确的 imagecreatefrom* 函数。

您可以查看源代码以查看:https://github.com/php/php-src/blob/master/ext/gd/gd.c?source=cc (函数的第 2280 行,它打开图像类型并调用正确的函数的第 2301 行)。

因此,imagecreatefromstring() 函数实际上只是一个辅助包装器。如果调用实际图像类型函数,不运行 _php_image_type(第 2217 行),您将获得非常小的好处。

关于php - 为什么有imageCreateFromString就有imageCreateFrom*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10963453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com