【问题标题】:Image cropping script now showing broken link图像裁剪脚本现在显示断开的链接
【发布时间】:2015-05-08 19:18:42
【问题描述】:

多年来,我一直在使用以下 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";
} 

?>

【问题讨论】:

  • 如果您在content-type: text/plain 时获得响应数据,则请求格式正确。但是,对于图像 mime 类型,返回的实际数据可能无法正确格式化回复 mime 类型。数据文件是否已损坏?

标签: php image-processing


【解决方案1】:

啊,“如果你问一个关于堆栈溢出的问题,你会在一分钟后找出问题所在”这句古老的格言再次应验了。

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

【讨论】:

    猜你喜欢
    • 2010-11-24
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-26
    • 2023-03-09
    相关资源
    最近更新 更多