2015-05-08 140 views
2

我一直在使用以下PHP脚本多年。突然,几个星期前,它停止了工作。它创建的图像又嵌入到网页中,显示为断开的链接。如果我删除内容类型行(发送文本),图像文本似乎发送正常。我有一个单独的脚本来获取图像,使用这个脚本来裁剪它们,然后在本地保存输出。我查看了保存的.png文件。他们都有一个文件大小(30K左右)。图像裁剪脚本现在显示损坏的链接

我担心我的托管服务已更新PHP并破坏了某些内容(再次)。任何人都知道发生了什么?

#!/usr/bin/php -q 
<? 
$w=$_GET['w']; 
$h=isset($_GET['h'])?$_GET['h']:$w; 
$x=isset($_GET['x'])?$_GET['x']:0; 
$y=isset($_GET['y'])?$_GET['y']:0; 
$filename="http://".$_GET['src']; 
//echo $filename; 
$result_array = getimagesize($filename); 
//exit(); 

if ($result_array !== false) { 
    $mime_type = $result_array['mime']; 
    switch($mime_type) { 
     case "image/jpeg": 
      header('Content-type: image/jpeg'); 
      $image = imagecreatefromjpeg($filename); 
      break; 
     case "image/gif": 
      header('Content-type: image/gif'); 
      $image = imagecreatefromgif($filename); 
      break; 
     case "image/png": 
      header('Content-type: image/png'); 
      $image = imagecreatefrompng($filename); 
      break; 
     case "image/bmp": 
      header('Content-type: image/bmp'); 
      $image = imagecreatefrombmp($filename); 
      break; 
     default: 
      echo "Unsupported image type"; 
    } 

    $resized = imagecreatetruecolor(1200, 1200); 
    imagecopyresampled($resized, $image, 0, 0, 0, 0, 1200, 1200, imagesx($image), imagesy($image)); 
    $crop = imagecreatetruecolor($w,$h); 
    imagecopy ($crop, $resized, 0, 0, $x, $y, $w, $h); 
    imagepng($crop); 
} else { 
    echo "file is not a valid image file"; 
} 

?> 
+0

如果您在'content-type:text/plain'时获得响应数据,则请求格式良好。但是,对于图像MIME类型,返回的实际数据可能无法针对回复MIME类型正确格式化。数据文件被破坏的可能性有多大? –

回答

1

啊,旧的“如果你问一个关于堆栈溢出的问题,你会在一分钟后找出问题”,格言再次成为现实。

几乎肯定我的主机更新了PHP。由于某种原因,#!/usr/bin/php -q行被添加到PNG文件的开头。我删除了这条线,一切都恢复了。