Jesus Loves GRASS

Technical blog about GRASS/GIS,open source geoinformatics and MAPSERVER.

Wednesday, February 27, 2008

The image .... cannot be displayed, because it contains errors.

"The image 'http://localhost/ACI/test.php' cannot be displayed, because it contains errors." Was the only thing I was getting when running some examples and source codes for image creation using GD and PHP. I googled it and all the information found concerned bad installation of GD and other libraries.

But when dumping the GD info with var_dump(gd_info()); everything was ok.

In the end I discovered that actually the error is not related to GD but with the header used to send the image, this example works fine:

<?php
header("Content-type: image/png");
$im = imagecreate(210, 20) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 25, 25, 25);
imageline($im,10,10,20,20,$text_color);
imagepng($im);
imagedestroy($im);
?>

But something like this:
<?php
//Something, source or text
?>
<?php
header("Content-type: image/png");
$im = imagecreate(210, 20) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 25, 25, 25);
imageline($im,10,10,20,20,$text_color);
imagepng($im);
imagedestroy($im);
?>

Will only generate the stupid GD error message.

The header() needs to be the first thing in the PHP code as explained in the PHP doc "Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file."

Labels: , , , ,

Tuesday, February 26, 2008

$oMap->setExtent problems and general data gathering

I made some PHP programming/MapScript for rendering a GeoTiff created by an interpolation service (WPS) as a PNG. The Geotiff is stored in the server's temporary folder and all the mapserver parameters are set by PHP programing.

The fist part of the posted code gets the image info using GdalInfo and regular expressions to obtain the extent value of the image, I haven't managed to do this properly using the PHP's Exif lib....

The major problem was the SetExtent of the Map object it was always complaining that the values had some problem, the problem was that the parameters were being passed as a string, and they needed to be as double, even if they are passed as float (or int?) the SetExtent will complain

//Some code before

$FileName=/usr/local/apache2/htdocs/tmp/interpolation3454.tiff;
$CmdStr="/usr/local/bin/gdalinfo ".$FileName;
//General string for getting float numbers
$StrPattern='/[-+]?\b[0-9]*\.?[0-9]+\b/';
exec($CmdStr,$ResultArray);

//Upper left point info
$UpperLeftPoint=$ResultArray[6];
preg_match_all($StrPattern,$UpperLeftPoint,$TmpArray);
$MinX=$TmpArray[0][0];
$MaxY=$TmpArray[0][1];

//Lower right point info
$LowerRightPoint=$ResultArray[9];
preg_match_all($StrPattern,$LowerRightPoint,$TmpArray);
$MaxX=$TmpArray[0][0];
$MinY=$TmpArray[0][1];

//Passing to Map object
//double minx, double miny, double maxx, double maxy
$oMap->setExtent((double)$MinY,(double)$MinY,(double)$MaxX,(double)$MaxY);

//Same code after

Labels: , , , , , ,

Monday, February 04, 2008

Basic Image in PHP/Mapscript

This script makes a simple web page with a PNG image based on a GeoTiff that contains interpolation values in Float32. The script uses the ms_GetErrorObj() to echo any errors.

The objective of the script is to totally replace any info from the map file, nevertheless it is necessary for a map file to start the Map object.

The map file called test2.map is only:
MAP

END

The rest is defined as follows.....

<?php
/*
* Created on Feb 1, 2008
*/

// Loading Lib (just to be certain)
dl('php_mapscript.so');

//Reset error list (just to be certain)
ms_ResetErrorList();

$map_path="/usr/local/apache2/htdocs/wpstmp/";

// Creation of map object
$oMap = ms_newMapObj($map_path."test2.map");

//Setting the name
$oMap->set("name","map1");

//Setting size and backcolor
$oMap->setSize(800,400);
$oMap->imagecolor->setRGB(125,125,125);

//Setting Extent
//Data gathered using GdalInfo from the GeoTIFF
//double minx, double miny, double maxx, double maxy
$oMap->setExtent(-69000.000,-42000.000,261000.000,633000.000);

//Seting units
$oMap->set("units",MS_METERS);

//Output type
$oMap->selectOutputFormat("PNG24");
$oMap->outputformat->setOption("imagemode",MS_IMAGEMODE_RGB);


//Paths
$oMap->web->set("imagepath","/usr/local/apache2/htdocs/wpstmp/");
$oMap->web->set("imageurl","/wpstmp/");


//Layer Object
$oLayer=ms_newLayerObj($oMap);
$oLayer->set("name","interpolation1");
$oLayer->set("status",MS_ON);
$oLayer->set("type",MS_LAYER_RASTER);
$oLayer->set("data","/usr/local/apache2/htdocs/wpstmp/interpolation610385.tif");

//RASTER processing of band1 of Geottif, automatic color scaling
//http://mapserver.gis.umn.edu/docs/howto/raster_data
$oLayer->setProcessing("SCALE=AUTO");
$oLayer->setProcessing("BANDS=1");


//Dumping image
$oImage=$oMap->draw();
$image_url=$oImage->saveWebImage();

//Error dumping part, not very necessary, only for debug
$error=ms_GetErrorObj();
while($error && $error->code!=MS_NOERR)
{
printf("Error in %s: %s
\n",$error->routine,$error->message);
$error=$error->next();
}

?>
<html>
<head>
</head>
<body>

<img src="http://localhost<?php echo $image_url?>" alt="Map" />

</body>
</html>

Labels: , ,