...

View Full Version : Why is this telling me my header is already written?



Spudhead
06-11-2007, 12:14 PM
<?php

$parameters = array('axis', 'size', 'file');
foreach($parameters as $name){
$$name = isset($_GET[$name]) ? $_GET[$name] : '';
}


$root = realpath(@$_SERVER['DOCUMENT_ROOT']);
$filesroot = str_replace ("\\files", "\\_files", $root ) . "\\";

$path = $filesroot.$file;


$image = imagecreatefromjpeg($path);


if ($image){

// Get image size and scale ratio
$oldwidth = imagesx($image);
$oldheight = imagesy($image);
if ($axis == 'x'){
$newwidth = $size;
$newheight = floor(($newwidth * $oldheight) / $oldwidth);
}elseif ($axis == 'y'){
$newheight = $size;
$newwidth = floor(($newheight * $oldwidth) / $oldheight);
}

// create new image
$tmp_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
//write to file
ImageJPEG($tmp_img, $filesroot.$axis.$size."_".$file, 80);
imagedestroy ($tmp_img);
imagedestroy($image);
}


//header("Location: http://www.mysite.com/files/files.asp");

?>


This is the first fully-functioning PHP script I've written so apologies if it's a bit of a daft question, but - what's writing HTTP headers there? If I uncomment the header() line at the end I get a "headers already written" error, but I don't see anything there that should be sending output back down to the browser?

Assuming that something is, and that's what it's supposed to be doing, and I've just misunderstood what it's doing - how can I get this script to redirect somewhere once it's finished creating me some images?

mlseim
06-11-2007, 01:19 PM
First I have to ask if you're trying to save the image into a file for
use later, or are you dealing with an "image-on-the-fly".

If you're manipulating an image to use directly, you have something
like this on your webpage:

<img src="myscript.php?pic=myphoto.jpg&size=150" alt="" />

Then, inside your PHP script, you take in the variables using $_GET and
output the jpg image with a header. When your HTML is processed, the
<img src .... > line actually processes the PHP script as an image.

The last lines of your PHP script ("myscript.php") would look like this:

// Output
header('Content-type: image/jpeg');
imagejpeg($image, null, 100);
imagedestroy($image);

.... where you define the Content-Type, output the image at 100% quality,
and then destroy the image in memory (sort of cleaning up the process).

Spudhead
06-11-2007, 01:46 PM
I don't want the script to output anything via HTTP. I want it to write some images to file. I don't want to run a memory-intensive PHP script every time someone requests a thumbnail; I want the thumbnails to be created when the image is uploaded. This script is called by an ASP application (I'm only using PHP cos I can't get Imagemagik running properly on my IIS server and I'm not paying for an ASP component to do image resizing).

All I want is to pass it a filename (of a file that the ASP application has just uploaded), and it'll drop a resized version of that file into a directory I specify.

So - I've consulted the PHP.net manual, and nothing AFAIK in that script should be sending anything back to the browser. But something clearly is, cos by the time I get to my header() line, it reckons something's already been sent. But what??

mlseim
06-11-2007, 02:37 PM
Just curious what would happen if you were to use a function instead ...

Something like this ... although it's not exactly correct ... just the fact
that you use a function to resize the photo :



<?php

$parameters = array('axis', 'size', 'file');
foreach($parameters as $name){
$$name = isset($_GET[$name]) ? $_GET[$name] : '';
}


$root = realpath(@$_SERVER['DOCUMENT_ROOT']);
$filesroot = str_replace ("\\files", "\\_files", $root ) . "\\";

$path = $filesroot.$file;
$dest = $filesroot.$axis.$size."_".$file;

resampimagejpg($size,$size,$path,$dest,0);

header("Location: http://www.mysite.com/files/files.asp");

function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
{
$g_imgcomp=100-$imgcomp;
$g_srcfile=$sourcefile;
$g_dstfile=$destfile;
$g_fw=$forcedwidth;
$g_fh=$forcedheight;

if(file_exists($g_srcfile))
{
$g_is=getimagesize($g_srcfile);
if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh))
{
$g_iw=$g_fw;
$g_ih=($g_fw/$g_is[0])*$g_is[1];
}
else
{
$g_ih=$g_fh;
$g_iw=($g_ih/$g_is[1])*$g_is[0];
}
$img_src=imagecreatefromjpeg($g_srcfile);
$img_dst=imagecreate($g_iw,$g_ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
imagedestroy($img_dst);
return true;
}
else
return false;
}

?>

Spudhead
06-11-2007, 03:15 PM
That works! Thanks :)

So why does putting it in a function fix it? :confused:

(And... what do you mean, it's not exactly correct? Your code runs fine here - I don't need to change anything, do i?)

mlseim
06-11-2007, 05:44 PM
Spudhead ...

I just tossed out my example without trying it. If it worked, that
does amaze me ... because I never have that kind of good luck.

I'm sort of a PHP novice hack, and learn by other script examples ...
that's where that function came from (another script).
It was just a hunch that it might work.

I think the function works because things that happen inside of a
function are not global. The rest of your script does not see what
is happening.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum