Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5图像PNG不工作_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel 5图像PNG不工作

Php Laravel 5图像PNG不工作,php,laravel,laravel-5,Php,Laravel,Laravel 5,我试图创建一个脚本,该脚本获取图像的特定部分,但当我使用imagepng时,它会返回以下内容: 这是我的密码 $name = $path; header("Content-type: image/png"); if (strpos($name, '..') !== false) { exit(); // name in path with '..' in it would allow for directory traversal. } $size = $face_size

我试图创建一个脚本,该脚本获取图像的特定部分,但当我使用imagepng时,它会返回以下内容:

这是我的密码

$name = $path;

header("Content-type: image/png");

if (strpos($name, '..') !== false) {
   exit(); // name in path with '..' in it would allow for directory 
   traversal.
}

$size = $face_size > 0 ? $face_size : 100;

//Grab the skin
$src = imagecreatefrompng("./skins/" . $name . ".png");
//If no path was given or no image can be found, then create from default
if (!$src) {
    $src = imagecreatefrompng("./skins/default.png");
}
//Start creating the image
list($w, $h) = getimagesize("./skins/" . $name . ".png");
$w = $w / 8;

$dest = imagecreatetruecolor($w, $w);
imagecopy($dest, $src, 0, 0, $w, $w, $w, $w);   // copy the face
// Check to see if the helm is not all same color
$bg_color = imagecolorat($src, 0, 0);
$no_helm = true;
// Check if there's any helm
for ($i = 1; $i <= $w; $i++) {
    for ($j = 1; $j <= 4; $j++) {
        // scanning helm area
        if (imagecolorat($src, 40 + $i, 7 + $j) != $bg_color) {
            $no_helm = false;
        }
    }
    if (!$no_helm)
        break;
}
// copy the helm
if (!$no_helm) {
    imagecopy($dest, $src, 0, -1, 40, 7, $w, 4);
}  
//prepare to finish the image
$final = imagecreatetruecolor($size, $size);
imagecopyresized($final, $dest, 0, 0, 0, 0, $size, $size, $w, $w);

//if its not, just show image on screen
imagepng($final);

//Finally some cleanup
imagedestroy($dest);
imagedestroy($final);
$name=$path;
标题(“内容类型:图像/png”);
if(strpos($name,…')!==false){
exit();//路径中包含“..”的名称将允许使用目录
穿越。
}
$size=$face\u size>0$面部尺寸:100;
//抓住皮肤
$src=imagecreatefrompng(“./skins/”$name..png”);
//如果没有给定路径或找不到图像,则从默认创建
如果(!$src){
$src=imagecreatefrompng(“./skins/default.png”);
}
//开始创建图像
列表($w,$h)=getimagesize(“./skins/”$name.png”);
$w=$w/8;
$dest=imagecreatetruecolor($w,$w);
imagecopy($dest,$src,0,0,$w,$w,$w);//临摹
//检查头盔的颜色是否不完全相同
$bg_color=imagecolorat($src,0,0);
$no_helm=true;
//检查是否有舵

对于($i=1;$iLaravel和其他框架,使用中间件,因此当您的控制器方法完成时,应用程序尚未准备好发送响应。您可以通过将imagepng函数的输出存储在内部缓冲区中并正确发送来解决此问题(如果您想使用GD,我认为没有其他解决方案),还必须使用Laravel函数来设置HTTP头,而不是
函数

下面是一个简单的例子:

你可以看看这个,我希望它能帮助你


虽然它可以工作,但不是最好的方法,我建议您使用Imagick(如果可以的话)而不是GD。

问题与Laravel无关。请检查您的错误日志。您可能在向浏览器发送HTML的某个地方遇到错误,因此浏览器无法正确解释图像,这就是您得到结果的原因。我的日志中没有错误,我想您保存了将图像设置为
.png
不会更改消光。您需要获得原始扩展名并调用方法
imagepng()
imagejpeg()
。如果您使用的是laravel,为什么不使用或?@GeorgeLopton?它看起来像是PHP试图将图像输出到浏览器。使用浏览器的“开发工具”网络选项卡分析实际请求,以查看服务器是否真的发送了
内容类型:image/png
响应头。它看起来不像。或者您也可以y执行
print_r(headers_list());
以查看PHP发送了哪些headers。
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AppController extends Controller
{
    //Generates an image with GD and sends it to the client.
    public function image(){



    $im = imagecreatetruecolor(800, 420);
    $orange = imagecolorallocate($im, 220, 210, 60);

    imagestring($im, 3, 10, 9, 'Example image', $orange);

    //Turn on output buffering
    ob_start();

    imagepng($im);

    //Store the contents of the output buffer
    $buffer = ob_get_contents();
    // Clean the output buffer and turn off output buffering
    ob_end_clean();

    imagedestroy($im);

    return response($buffer, 200)->header('Content-type', 'image/png');

    }
}