【问题标题】:GD Image resize and Header(Content-type)GD Image resize and Header(Content-type)
【发布时间】:2014-04-29 21:13:02
【问题描述】:

我正在尝试弄清楚 Header(content-type:image/png) 的正确用法。我有一个上传脚本,可以正确上传和调整大小,无论是否有标题,但它似乎建议有标题。问题是标题,一旦脚本完成,它会在窗口的左上角抛出一个“破碎的img”图标并退出脚本。如果我想重定向到新页面,我该如何让它表现得不同。在脚本末尾添加 Header(location:...) 似乎没有什么不同。任何帮助表示赞赏。

<?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);



?>

【问题讨论】:

  • 您正在为 PNG 图像设置内容类型标题但正在输出文本,您的浏览器不知道如何处理所有这些,并认为文本实际上是损坏的图像,并显示相应的图标。要么删除该标题,要么提供实际图像而不是文本。
  • 感谢安德烈..它确实可以在没有标题的情况下工作。我想我对标题的重要性以及是否需要指定 GD 调整大小感到更加困惑。换句话说……我是否会发现自己不得不在某个时候将其放回原处。此代码最终将成为图像处理类的一部分
  • 只有当你想serve an image时才需要header。
  • 谢谢!!为了澄清和链接......现在非常清楚。

标签: php


【解决方案1】:

您不能同时提供图片和重定向。如果要输出 PNG 图像,则输出 Content-Type 标头,不包括 Location 标头。

【讨论】:

    猜你喜欢
    • 2014-04-28
    • 2020-09-22
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2021-11-08
    相关资源
    最近更新 更多