Alfred
Alfred

Reputation: 21386

Black result image problem on BMP image resize using PHP

I have a PHP script to re size image file as below;

$file = "test.bmp";
$ext = pathinfo($file, PATHINFO_EXTENSION);
$info = pathinfo($file);
$file_name =  basename($file,'.'.$info['extension']);
$thumbname = "thumb/".$file_name.".".$ext;
$maxh = 200;
$maxw = 200;
$quality = 100;
list($width,$height)=getimagesize($file);

  $src = imagecreatefromwbmp($file);
  $tmp = imagecreatetruecolor($maxw,$maxh);
  imagecopyresampled($tmp,$src,0,0,0,0,200,200,$width,$height);
  imagejpeg($tmp,$thumbname,$quality);
  imagedestroy($tmp);

The script is suppose to resize a Windows bitmap image to 200x200 thumbnail. But instead, I am getting a black 200x200 image. I am using PHP with Apache in Windows PC. How can I fix this?

Upvotes: 1

Views: 2520

Answers (4)

Jarrod
Jarrod

Reputation: 9465

There is a new opensource project on Github that allows reading and saving of BMP files (and other file formats) in PHP.

The project is called PHP Image Magician.

Upvotes: 1

Sean Thayne
Sean Thayne

Reputation: 883

<?php

//Create New 'Thumbnail' Image
$newImageWidth   = 200;
$newImageHeight  = 200;
$newImage        = imagecreatetruecolor($newImageWidth, $newImageHeight);
$newImageFile    = 'output.jpg';
$newImageQuality = 100;

//Load old Image(bmp, jpg, gif, png, etc)
$oldImageFile    = "test.jpg";

//Specific function
$oldImage        = imagecreatefromjpeg($oldImageFile);

//Non-Specific function
//$oldImageContent = file_get_contents($oldImageFile);
//$oldImage        = imagecreatefromstring($oldImageContent);

//Get old Image's details
$oldImageWidth   = imagesx($oldImage);
$oldImageHeight  = imagesy($oldImage);

//Copy to new Image
imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $oldImageWidth, $oldImageHeight);

//Output to file
imagejpeg($newImage, $newImageFile, $newImageQuality);

Upvotes: 0

Paolo Stefan
Paolo Stefan

Reputation: 10253

As pointed out in PHP imagecopyresampled() docs:


Note:

There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor().


To see if it's the case you can use imageistruecolor() and copy the contents to a new truecolor image before "copyresampling" it:

if( !imageistruecolor($src) ){
    $newim = imagecreatetruecolor( $width, $height );
    imagecopy( $newim, $src, 0, 0, 0, 0, $width, $height );
    imagedestroy($src);
    $src = $newim;
}

Upvotes: 1

rockerest
rockerest

Reputation: 10508

.bmp and wbmp are VERY, VERY different file types.

Note the content-type headers:

Content-Type: image/x-xbitmap
Content-Type: image/vnd.wap.wbmp

Calling imagecreatefromwbmp($file) where $file is a .bmp will fail every time.

See this thread for info on how to load a .bmp file. It's not pretty.

Upvotes: 5

Related Questions