doujia1939 2013-06-26 00:30
浏览 97
已采纳

GD - 从imagecreatefromstring()获取正确的输出函数

I've read my image path through imagecreatefromstring(), processed it, but now I need to output it, which is where I'm stuck.

$imgpath = "test.jpg"; //Testing
$img = imagecreatefromstring(file_get_contents($imgpath));

//Do stuff to $img

imagejpeg($img);
imagedestroy($img);

This will load up the image, regardless of type, into the $img resource variable. However, I'm currently just using imagejpeg() to output the image, which works fine if the source ($imgpath) was a jpeg. However, if I change imagejpeg to imagepng, the server has to convert the jpeg to a png, which is very noticeable on image load times.

Since I will never know precisely what type of image is being fed through the function, how can I tell what type of image it is and act accordingly for output? I am trying to avoid the filename checking method, but it seems I'll have to go that way if there's no way for GD to return what type of image it's working with.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongzuan4491 2013-06-26 00:38
    关注

    You can use getimagesize() php function in order to see what type of image you are dealing with and accordingly use the proper method based on it. http://php.net/manual/en/function.getimagesize.php

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)