Page 1 of 1

why is this code producing a blank white page?

Posted: Sat Apr 01, 2006 12:26 pm
by bruceg
I am using PHP 4.1, not 5.0. Is that the problem?

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
<title>Inspired Evolution : : PHP Drop Shadow</title>
</head>
<body>
<?php
/* set drop shadow options */

/* offset of drop shadow from top left */
define("DS_OFFSET",  5);

/* number of steps from black to background color /*
define("DS_STEPS", 10);

/* distance between steps */
define("DS_SPREAD", 1);

/* define the background color */
$background = array("r" => 255, "g" => 255, "b" => 255);

$src = isset($_REQUEST['src']) ? urldecode($_REQUEST['src']) : null;
if(isset($src) && file_exists($src)) {

 /* create a new canvas.  New canvas dimensions should be larger than
the original's */
 list($o_width, $o_height) = getimagesize($src);
 $width  = $o_width + DS_OFFSET;
 $height = $o_height + DS_OFFSET;
 $image = imagecreatetruecolor($width, $height);

 /* determine the offset between colors */
 $step_offset = array("r" => ($background["r"] / DS_STEPS), "g" =>
($background["g"] / DS_STEPS), "b" => ($background["b"] / DS_STEPS));

 /* calculate and allocate the needed colors */
 $current_color = $background;
 for ($i = 0; $i <= DS_STEPS; $i++) {
   $colors[$i] = imagecolorallocate($image,
round($current_color["r"]), round($current_color["g"]),
round($current_color["b"]));

   $current_color["r"] -= $step_offset["r"];
   $current_color["g"] -= $step_offset["g"];
   $current_color["b"] -= $step_offset["b"];
 }

 /* floodfill the canvas with the background color */
 imagefilledrectangle($image, 0,0, $width, $height, $colors[0]);

 /* draw overlapping rectangles to create a drop shadow effect */
 for ($i = 0; $i < count($colors); $i++) {
   imagefilledrectangle($image, DS_OFFSET, DS_OFFSET, $width,
$height, $colors[$i]);
   $width -= DS_SPREAD;
   $height -= DS_SPREAD;
 }

 /* overlay the original image on top of the drop shadow */
 $original_image = imagecreatefromjpeg($src);
 imagecopymerge($image, $original_image, 0,0, 0,0, $o_width, $o_height, 100);

 /* output the image */
 header("Content-type: image/jpeg");
 imagejpeg($image, "", 100);

 /* clean up the image resources */
 imagedestroy($image);
 imagedestroy($original_image);
}
?>
</body>
</head>
thanks in advance!

Posted: Sat Apr 01, 2006 6:28 pm
by feyd
you're trying to send an image inside an HTML stream. That doesn't work.

Posted: Sat Apr 01, 2006 7:13 pm
by bruceg
so I should take out the HTML tags and it should work?

Posted: Sat Apr 01, 2006 7:25 pm
by feyd
I didn't say it'd work, but it's a step in the right direction.

The only thing that pops out at me is your second define is commented out but still used in your code creating a divide by zero problem.

Re: why is this code producing a blank white page?

Posted: Sat Apr 01, 2006 7:29 pm
by Christopher
I think you should break this into two scripts:

The HTML document:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
<title>Inspired Evolution : : PHP Drop Shadow</title>
</head>
<body>
<img src="image.php"/>
</body>
</html>
And the "image.php" script that generates the image:

Code: Select all

<?php
/* set drop shadow options */

/* offset of drop shadow from top left */
define("DS_OFFSET",  5);

/* number of steps from black to background color /*
define("DS_STEPS", 10);

/* distance between steps */
define("DS_SPREAD", 1);

/* define the background color */
$background = array("r" => 255, "g" => 255, "b" => 255);

$src = isset($_REQUEST['src']) ? urldecode($_REQUEST['src']) : null;
if(isset($src) && file_exists($src)) {

 /* create a new canvas.  New canvas dimensions should be larger than
the original's */
 list($o_width, $o_height) = getimagesize($src);
 $width  = $o_width + DS_OFFSET;
 $height = $o_height + DS_OFFSET;
 $image = imagecreatetruecolor($width, $height);

 /* determine the offset between colors */
 $step_offset = array("r" => ($background["r"] / DS_STEPS), "g" =>
($background["g"] / DS_STEPS), "b" => ($background["b"] / DS_STEPS));

 /* calculate and allocate the needed colors */
 $current_color = $background;
 for ($i = 0; $i <= DS_STEPS; $i++) {
   $colors[$i] = imagecolorallocate($image,
round($current_color["r"]), round($current_color["g"]),
round($current_color["b"]));

   $current_color["r"] -= $step_offset["r"];
   $current_color["g"] -= $step_offset["g"];
   $current_color["b"] -= $step_offset["b"];
 }

 /* floodfill the canvas with the background color */
 imagefilledrectangle($image, 0,0, $width, $height, $colors[0]);

 /* draw overlapping rectangles to create a drop shadow effect */
 for ($i = 0; $i < count($colors); $i++) {
   imagefilledrectangle($image, DS_OFFSET, DS_OFFSET, $width,
$height, $colors[$i]);
   $width -= DS_SPREAD;
   $height -= DS_SPREAD;
 }

 /* overlay the original image on top of the drop shadow */
 $original_image = imagecreatefromjpeg($src);
 imagecopymerge($image, $original_image, 0,0, 0,0, $o_width, $o_height, 100);

 /* output the image */
 header("Content-type: image/jpeg");
 imagejpeg($image, "", 100);

 /* clean up the image resources */
 imagedestroy($image);
 imagedestroy($original_image);
}
?>

Posted: Sun Apr 02, 2006 1:02 am
by R4000

Code: Select all

/* number of steps from black to background color /*
define("DS_STEPS", 10);
(wtf? php tags broken...)

Code: Select all

/* number of steps from black to background color /*
define("DS_STEPS", 10);
should be

Code: Select all

/* number of steps from black to background color */
define("DS_STEPS", 10);
last part of comment was wrong