Jump to content

[SOLVED] Redirection after image resize


bertwagner

Recommended Posts

What I'm trying to do: After I upload a file to the server I want it to automatically be resized.  After the image is resized I want the script to redirect to a new page.

 

My problem: I'm able to successfully upload and resize the image.  When I want to redirect from the script to a new page however, any code after my image resize function does not get processed, outputting only the url of the script.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Upload a Photo</title>
</head>
<body>
<?php
/************************************************************\
* Resize an image to have a max height or width of 200px
\************************************************************/
function resizeimage($fname, $fext, $fpath)
{
    $fname = $fname;
    $fext = $fext;
    $fpath = $fpath;
    list($original_width, $original_height) = getimagesize($fpath.$fname.$fext);
    $width = 200;
    $height = 200;
    header('Content-type: image/jpeg');
    $ratio_orig = $original_width/$original_height;
    if ($width/$height > $ratio_orig)
    {
        $width = $height*$ratio_orig;
    }
    else
    {
        $height = $width/$ratio_orig;
    }
    $image_new = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($fpath.$fname.$fext);
    imagecopyresampled($image_new, $image, 0, 0, 0, 0, $width, $height, $original_width, $original_height);
    imagedestroy($image);
    header("Content-type: image/jpeg");
    $new_file_name = $fpath.$fname."_small".$fext;
    if(!file_exists($new_file_name))
    imagejpeg($image_new, $new_file_name, 90);
    imagejpeg($image_new, null, 90);
    imagedestroy($image_new);
}

/************************************************************\
* Uploads the photo and resizes it as well
\************************************************************/

date_default_timezone_set('EST');
$current_month = date("m");
$current_year = date("Y");
$target_path = "photos/".$current_year."/".$current_month."/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
$fileinfo = pathinfo($target_path);
$fname = $fileinfo['filename'];
$fext = ".".$fileinfo['extension'];
$fpath = "photos/".$current_year."/".$current_month."/";
resizeimage($fname, $fext, $fpath);
/************************************************************\
* Everything up to this point in the code works fine. Anything 
* past this point doesn't get processed.  Instead of processing 
* the next line, the script stops and outputs the url of the 
* script itself instead.
\************************************************************/
header("Location: http://www.google.com/");
}
else
{
echo "There was an error uploading your photo, please try again!";
}


?>
</body>
</html>

 

 

This problem has been driving me bonkers, any help or advice would be greatly appreciated. Thanks!

Link to comment
Share on other sites

This is a very, very common error.  Calling header() modifies header information.  You may not modify header information if output has already been generated.  The very top of your script is a doctype declaration, which counts as output.  You will have to restructure your code so that the processing occurs before any output.

 

The other, more minor but also common error you made, after a call to header() in which you change the location, you should call exit().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.