duanlan8763 2014-04-29 21:13
浏览 45

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

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



?>
  • 写回答

1条回答 默认 最新

  • drnycqxwz63508434 2014-04-29 21:31
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测