且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PHP中将BMP转换为JPG

更新时间:2023-11-09 13:36:22

如果有一些先决条件,您链接的代码将会失效失败:

The code you linked to bails out if a few preconditions fail:

if(!($src_f = fopen($src, "rb"))) { 
...
if(!($dest_f = fopen($dest, "wb"))) { 
...
if($type != 0x4D42) { // signature "BM" 

因此假设源文件和tmp文件是可读/可写的,它看起来就像你的文件给它(它以.png发送但是给出BMP mime类型)实际上也不是BMP文件,因为文件内容不以BM标识符开头。如果您附加文件,也许有人可以确定它到底是什么。

So assuming that the source and tmp files are read/writable, it looks like the file you are giving it (which is sent as a .png but gives a BMP mime type) is in fact not a BMP file either, because the file contents don't start with the "BM" identifier. If you attach the file, perhaps somebody could identify what it really is.

我用来解决这个问题的另一个解决方案是使用ImageMagick的convert命令来转换大多数文件类型,无论它们是什么,都是所需的格式:

Another solution I've used to this problem is to use ImageMagick's convert command to convert most file types, whatever they may be, to the desired format:

// convert uses the file extension to determine the output format, so
// change .jpg to whatever you'd like
$tmp_name = tempnam("/tmp", "convert") . '.jpg';
exec('c:\path\to\imagemagick\convert ' . $inputFile . ' ' . $tmp_name);

您可以在此处获取适用于Windows和其他平台的ImageMagick:
http://www.imagemagick.org/script/binary-releases.php

You can get ImageMagick for Windows and other platforms here: http://www.imagemagick.org/script/binary-releases.php