|
|
Also see 0005015 rougly about the same thing.
I hacked an automatic BMP->PNG conversion in file_api.php.
A full screendyump of 2MB is converted to 29KB. Pretty impressive, and nice for server diskspace and bandwith.
I'm now going to allow screenshot-uploads to our users. |
|
|
No problem.
I don't know what the standard for patches is, so here's my code:
File is core/file_api.php
At the bottom I added this function to remove the extension from a filename:
# --------------------
# Get basename given the filename or its full path.
function file_get_basename( $p_filename ) {
$ext = '';
$dot_found = false;
$i = strlen( $p_filename ) - 1;
while ( $i >= 0 ) {
if ( '.' == $p_filename[$i] ) {
$dot_found = true;
break;
}
# found a directoryarker before a period.
if ( ( $p_filename[$i] == "/" ) || ( $p_filename[$i] == "\\" ) ) {
return $p_filename;
}
$i--;
}
if ( $dot_found ) {
return ( substr ($p_filename, 0, $i ) );
} else {
return $p_filename;
}
}
################################################### the end
Then in function 'file_add' I add the following - the first IF statement is still part of the original code:
if ( !file_type_check( $p_file_name ) ) {
trigger_error( ERROR_FILE_NOT_ALLOWED, ERROR );
}
# Convert all bmp files to png - here's the bit I added:
$t_bmp2png = (strtolower( file_get_extension($p_file_name)) == "bmp" );
if ($t_bmp2png)
{
$png_filename = file_get_basename($p_file_name) . '.png';
$p_file_name = $png_filename;
if ( 1 )
{
# move from tempfile to tempfile.bmp
$tmp_file_bmp = file_get_basename($p_tmp_file) . '.bmp';
$tmp_file_png = file_get_basename($p_tmp_file) . '.png';
$dummy = shell_exec("/bin/mv -f $p_tmp_file $tmp_file_bmp");
# then convert tempfile.bmp to tempfile.png
$dummy = shell_exec("/usr/bin/convert $tmp_file_bmp $tmp_file_png");
# then move tempfile.png back to tempfile
$dummy = shell_exec("/bin/mv -f $tmp_file_png $p_tmp_file");
}
}
#################################### rest of 'file add'.
Notes:
- the 'if (1)' is the place to decide whether to run this conversion for DISK based files, DATABASE files, or both. Currently it will convert every time.
- I convert the file to PNG and I change the filename that's in the parameter $p_filename. That way the converted filename shows up on the screen, and not the 'bmp' filename.
- I use hardcoded calls to ImageMagic's 'convert', and then some hardcoded calls to 'mv' to change the filename. This is not how it should be done. I believe ImageMagick has a PHP plugin to perform conversions. I'm no PHP wizzard, so I did it the dirty way.
- There is no failsafe. If the conversion fails, for instance because 'convert' cannot be found, this is not detected and the uploaded file is lost.
- The rest of the code works without changes because
a) the filename is changed and
b) the variable containing the filename is changed accordingly.
To the rest of the software it looks as if a PNG file was uploaded instead of a BMP file.
Good luck with it. |
|
|
BMP format is hardly ever used nowadays, so I don't really see the point in implementing this anymore.
Instead of a shell_exec call to imagemagick, it would be better to rely on a PHP function, e.g. imagecreatefrombmp() from GD extension or some other library. |