简体   繁体   English

GD图像调整大小和标题(内容类型)

[英]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. 我试图找出Header(content-type:image / png)的正确用法。我有一个上传脚本,可以正确地上传和调整大小,无论是否包含标题,但似乎建议使用标题。问题是带有标头的脚本一旦完成,就会在窗口的左上角抛出“破碎的img”图标并退出脚本。 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. 在脚本末尾添加Header(location:...)似乎没有什么不同。 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. 如果要输出PNG图像,则输出Content-Type标头,但不包括Location标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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