简体   繁体   中英

GD Image resize and Header(Content-type)

I'm trying to figure out the correct use of the Header(content-type:image/png) .I have an upload script that uploads and resizes correctly,with or without the header, but it seems like its recommended to have the header .Problem is that with the header, once the script completes, it throws a 'broken img' icon in the top left corner of the window and exits the script. How do I make it behave differently like if I want to redirect to a new page. Adding Header(location:...) at the end of the script doesn't seem to make a difference. ANY Help is appreciated.

<?PHP


  /*image resize with Imagick*/
  function imageResize($image){
   /*call to imagick class and resize functions will go here*/

   echo 'the image to be resized is : '.$image;
   $newImage=new Imagick();
   $newImage->readImage($image);
   $newImage->resizeImage(1024,768,imagick::FILTER_LANCZOS, 1);
   $newImage->writeImage('myImage.png');
   $newImage->clear();
   $newImage->destroy();

 }

 /*image resize with GD image functions*/


 function imgResize($image){
 header('Content-Type: image/png');

 $newWidth='1024';
 $newHeight='768';

 $size=getimagesize($image);

 $width=$size[0];
 $height=$size[1];



 //return $width;
 $dest=imagecreatetruecolor($newWidth,$newHeight);
 $source=imagecreatefrompng($image);
 imagecopyresized($dest,$source,0,0,0,0,$newWidth,$newHeight,$width,$height );
 return imagepng($dest,'./users/uploads/newimage.png'); 
 imagedestroy($dest);

 }

 /*Function that actually does the upload*/

 function file_upload(){ 

  if(!empty( $_FILES) ){
  print_r($_FILES);

  echo '<hr>';

  $tmpFldr=$_FILES['upFile']['tmp_name'];
  $fileDest='./users/uploads/'.$_FILES['upFile']['name'];

    if(move_uploaded_file($tmpFldr,$fileDest)){

      echo 'Congratulations, your folder was uploaded successfully <br><br>';

      }
    else{
     echo 'Your file failed to upload<br><br>';

     }
     return $fileDest; 
    } else{die( 'Nothing to upload');}




 } /*End file upload function */ 



$fileLocation=file_upload();

echo 'location of the new file is : '.$fileLocation.'<hr>';

$newImage=imgResize($fileLocation);



?>

You can't serve an image and serve a redirect at the same time. If you want to output a PNG image then you output the Content-Type header and that's exclusive of the Location header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM