/    Sign up×
Community /Pin to ProfileBookmark

imagecreatefrom[BMP]

Hi,

I developed a GD based function to add watermark to images and I wanted to make that available for many image types as much as possible. πŸ™‚

JPG, GIF, PNG all works fine. But with BMP images, it is not recognized as an image.

We see functions as imagecreatefromjpeg, imagecreatefromgif, imagecreatefrompng.

But not a function imagecreatefrombmp() …

I tried to use imagecreatefromwbmp , but useless ; no error shown, but when goes to other functions such as imagesx , imagesy it says not valid ..

Do GD really support bmp ? If so how?

Thanks and Best Regards

to post a comment
PHP

7 Comments(s) ↴

Copy linkTweet thisAlerts:
@scragarOct 26.2008 β€”Β Isn't it wbmp, [B]W[/B]indows [B]B[/B]it[B]M[/B]a[B]P[/B]?

[code=php]
$image = imagecreatefromwbmp($file);
[/code]

http://php.net/imagecreatefromwbmp
Copy linkTweet thisAlerts:
@GUIRauthorOct 26.2008 β€”Β Hi!,

Its not working with imagecreatefromwbmp() also.

I am using the following function.

[code=php] public function waterMark($orgPath)
{
$imagesource = $orgPath;
//echo $imagesource;
$filetype = $this->getType($orgPath);
//echo $filetype;
if($filetype == "gif")
$image = imagecreatefromgif($imagesource);

if($filetype == "jpg")
$image = imagecreatefromjpeg($imagesource);

if($filetype == "jpeg")
$image = imagecreatefromjpeg($imagesource);

if($filetype == "png")
$image = imagecreatefrompng($imagesource);

if($filetype == "bmp")
$image = imagecreatefromwbmp($imagesource);

//if (!$image) die();

$watermark = @imagecreatefromgif('images/watermark.gif');

$imagewidth = imagesx($image);
$imageheight = imagesy($image);

$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);

$startwidth = (($imagewidth - $watermarkwidth) );
$startheight = (($imageheight - $watermarkheight) );

imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

$destPath = 'fs_tempWk/'.$orgPath.'.'.$this->getType($orgPath);


if($filetype == "gif")
imagegif($image,$destPath);

if($filetype == "jpg")
imagejpeg($image,$destPath,100);

if($filetype == "jpeg")
imagejpeg($image,$destPath,100);

if($filetype == "png")
imagepng($image,$destPath);

if($filetype == "bmp")
imagewbmp($image,$destPath);

imagedestroy($image);
imagedestroy($watermark);

}[/code]


When implemented,

[code=php]require_once('class.Image.php');

$i = new Image();

$i->waterMark('sunFire.bmp');[/code]


I get following errors, but for other image types everything works fine.

[FONT="Courier New"][SIZE="2"]Warning: imagecreatefromwbmp() [function.imagecreatefromwbmp]: 'sunFire.bmp' is not a valid WBMP file in /home/apg/public_html/abc-def-ghi/test/gd/watermark/class.Image.php on line 411



Warning: imagesx(): supplied argument is not a valid Image resource in /home/apg/public_html/abc-def-ghi/test/gd/watermark/class.Image.php on line 417



Warning: imagesy(): supplied argument is not a valid Image resource in /home/apg/public_html/abc-def-ghi/test/gd/watermark/class.Image.php on line 418



Warning: imagecopy(): supplied argument is not a valid Image resource in /home/apg/public_html/abc-def-ghi/test/gd/watermark/class.Image.php on line 426



Warning: imagewbmp(): supplied argument is not a valid Image resource in /home/apg/public_html/abc-def-ghi/test/gd/watermark/class.Image.php on line 444



Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/apg/public_html/abc-def-ghi/test/gd/watermark/class.Image.php on line 446[/SIZE]
[/FONT]

Thanks and Best Regards
Copy linkTweet thisAlerts:
@scragarOct 26.2008 β€”Β ooh, my mistake, reading the docs now I see the problem, wBMP is wireless bitmap, not windows.

use bmp2png to get them in PNG format first, then you can use them.

http://cetus.sakura.ne.jp/softlab/b2p-home/
Copy linkTweet thisAlerts:
@GUIRauthorOct 26.2008 β€”Β Thanks for the information, I am going to test bmp2png .
Copy linkTweet thisAlerts:
@putaNov 23.2008 β€”Β ENJOY THIS FUNCTION MY LITTLE FRIENDS!!!! =D

By Puta from BR :eek:
[code=php]<?php
function ImageCreateFromBMP($filename)
{
if (! $f1 = fopen($filename,"rb")) return FALSE;
$FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
if ($FILE['file_type'] != 19778) return FALSE;
$BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
$BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
$BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
$BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
$BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] = 4-(4*$BMP['decal']);
if ($BMP['decal'] == 4) $BMP['decal'] = 0;
$PALETTE = array();
if ($BMP['colors'] < 16777216)
{
$PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
}
$IMG = fread($f1,$BMP['size_bitmap']);
$VIDE = chr(0);
$res = imagecreatetruecolor($BMP['width'],$BMP['height']);
$P = 0;
$Y = $BMP['height']-1;
while ($Y >= 0)
{
$X=0;
while ($X < $BMP['width'])
{
if ($BMP['bits_per_pixel'] == 24)
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
elseif ($BMP['bits_per_pixel'] == 16)
{

$COLOR = unpack("n",substr($IMG,$P,2));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 8)
{

$COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 4)
{
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 1)
{
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
else
return FALSE;
imagesetpixel($res,$X,$Y,$COLOR[1]);
$X++;
$P += $BMP['bytes_per_pixel'];
}
$Y--;
$P+=$BMP['decal'];
}
fclose($f1);
return $res;
}
?>
[/code]
Copy linkTweet thisAlerts:
@hyperliskNov 23.2008 β€”Β Here's a nice set of functions I use, I don't remember who made them though...

[code=php]<?php
function ConvertBMP2GD($src, $dest = false) {
if (!($src_f = fopen($src, "rb"))) {
return false;
}
if (!($dest_f = fopen($dest, "wb"))) {
return false;
}
$header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f, 14));
$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40));

extract($info);
extract($header);

if ($type != 0x4D42) {
// signature "BM"
return false;
}

$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$gd_header = "";
// true-color vs. palette
$gd_header .= ($palette_size == 0) ? "xFFxFE" : "xFFxFF";
$gd_header .= pack("n2", $width, $height);
$gd_header .= ($palette_size == 0) ? "x01" : "x00";
if ($palette_size) {
$gd_header .= pack("n", $ncolor);
}
// no transparency
$gd_header .= "xFFxFFxFFxFF";

fwrite($dest_f, $gd_header);

if ($palette_size) {
$palette = fread($src_f, $palette_size);
$gd_palette = "";
$j = 0;
while ($j < $palette_size) {
$b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};
$gd_palette .= "$r$g$b$a";
}
$gd_palette .= str_repeat("x00x00x00x00", 256 - $ncolor);
fwrite($dest_f, $gd_palette);
}

$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;

for ($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
// BMP stores scan lines starting from bottom
fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($src_f, $scan_line_size);
if ($bits == 24) {
$gd_scan_line = "";
$j = 0;
while ($j < $scan_line_size) {
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "x00$r$g$b";
}
} elseif ($bits == 8) {
$gd_scan_line = $scan_line;
} elseif ($bits == 4) {
$gd_scan_line = "";
$j = 0;
while ($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= "$p1$p2";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
} elseif ($bits == 1) {
$gd_scan_line = "";
$j = 0;
while ($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr((int)(($byte & 0x80) != 0));
$p2 = chr((int)(($byte & 0x40) != 0));
$p3 = chr((int)(($byte & 0x20) != 0));
$p4 = chr((int)(($byte & 0x10) != 0));
$p5 = chr((int)(($byte & 0x08) != 0));
$p6 = chr((int)(($byte & 0x04) != 0));
$p7 = chr((int)(($byte & 0x02) != 0));
$p8 = chr((int)(($byte & 0x01) != 0));
$gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}

fwrite($dest_f, $gd_scan_line);
}
fclose($src_f);
fclose($dest_f);
return true;
}

function imagebmp ($im, $fn = false){
if (!$im) return false;

if ($fn === false) $fn = 'php://output';
$f = fopen ($fn, "w");
if (!$f) return false;

//Image dimensions
$biWidth = imagesx ($im);
$biHeight = imagesy ($im);
$biBPLine = $biWidth * 3;
$biStride = ($biBPLine + 3) & ~3;
$biSizeImage = $biStride * $biHeight;
$bfOffBits = 54;
$bfSize = $bfOffBits + $biSizeImage;

//BITMAPFILEHEADER
fwrite ($f, 'BM', 2);
fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));

//BITMAPINFO (BITMAPINFOHEADER)
fwrite ($f, pack ('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));

$numpad = $biStride - $biBPLine;
for ($y = $biHeight - 1; $y >= 0; --$y)
{
for ($x = 0; $x < $biWidth; ++$x)
{
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('V', $col), 3);
}
for ($i = 0; $i < $numpad; ++$i)
fwrite ($f, pack ('C', 0));
}
fclose($f);
return true;
}

function imagecreatefrombmp($filename) {
$tmp_name = tempnam("./temp_files", "GD");
if (ConvertBMP2GD($filename, $tmp_name)) {
$img = imagecreatefromgd($tmp_name);
unlink($tmp_name);
return $img;
}
return false;
}

?>[/code]
Copy linkTweet thisAlerts:
@GUIRauthorNov 24.2008 β€”Β ?

Hi, thanks guys

Though somepoints are Greek to me yet, with my less experience, I am really going to try this out.

?
Γ—

Success!

Help @GUIR spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 5.19,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...