在PHP中将BMP转换为JPG [英] Converting BMP to JPG in PHP
问题描述
在我正在开发的网站中,我需要能够通过imagejpeg()传递我的所有图像,所以我决定(因为我的网站只接受JPG,BMP + PNG上传)来简单地将BMP和PNG转换为JPG第一个。
In a site I'm developing I need to be able to pass all my images through imagejpeg(), so I decided (as my site only accepts JPG, BMP + PNG uploads) to simply convert BMPs and PNGs to a JPG first.
现在转换BMP我使用了这里找到的脚本:
http://forums.codewalkers.com/php-coding-7/how-to-convert-bmp- to-jpg-879135.html
Now to convert the BMP I used the script found here: http://forums.codewalkers.com/php-coding-7/how-to-convert-bmp-to-jpg-879135.html
当我通过普通BMP时,脚本运行正常。
The script works fine when I pass a normal BMP through it.
现在,我有一个PNG,我通过imagecreatefrompng()进行转换时遇到了问题,过了一段时间我意识到它有mime-type image / x-ms-bmp ....
Now, I had a PNG which I had problems converting via imagecreatefrompng(), and after a while I realised it had the mime-type image/x-ms-bmp....
我尝试通过BMP脚本传递图像,但是我收到以下错误:
I tried passing the image through the BMP script but I get the following error:
警告:imagecreatefromgd( )[function.imagecreatefromgd]:'C:\ Users \Tom \ AppData \ Local \ Temp \ GD50C1.tmp'不是有效的GD第10行的C:\ xampp \ htdocs \ test\cropimage \ FCreateImageFromBMP.php文件
Warning: imagecreatefromgd() [function.imagecreatefromgd]: 'C:\Users\Tom\AppData\Local\Temp\GD50C1.tmp' is not a valid GD file in C:\xampp\htdocs\test\cropimage\FCreateImageFromBMP.php on line 10
如果有人有来之前,请帮忙。
如果您需要查看任何代码,请告诉我们。
If anyone has come accross this before, please help. If you need to see any code, just let me know.
提前致谢,Tom。
编辑:
可能有用的是,发生错误的行(从上面的链接开始)si这一行:
Might be useful to mention, the line which the error occurs on (as from the link above) si this one:
$tmp_name = tempnam("/tmp", "GD");
推荐答案
如果有一些先决条件,您链接的代码将会失效失败:
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
这篇关于在PHP中将BMP转换为JPG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!