PHP imagepng()正在创建损坏的图像 [英] PHP imagepng() is creating corrupt image

查看:249
本文介绍了PHP imagepng()正在创建损坏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜索,找到解决问题的可能方法。不幸的是我似乎无法弄明白。我有一个.php文件,可以根据其他图像创建图像。我让脚本完全正常工作,之前没有任何缺陷。但是在摆弄了一些其他文件后,它突然停止工作,现在在Firefox,Chrome和IE上生成了腐败的.png图像。

I have searched everywhere to find a possible solution to my problem. Unfortunately I cannot seem to figure it out. I have a .php file that creates an image based on other images. I had the script working exactly as it is right now before without flaws. But after fiddling around with some other files it suddenly stopped working and is generating corrupt .png images now on Firefox, Chrome and IE.

image.php

<?php
ini_set("display_errors", 1);
ini_set("error_reporting", 2047);
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting("E_ALL");

include("GameEngine/Database.php");
if(isset($_GET['uid'])){
    $uid =  $_GET['uid'];
} else {
    $uid = "1";
}
if(isset($_GET['size'])){
    if($_GET['size']=='profile'){
        $size =  '31x40';
    }elseif($_GET['size']=='inventory'){
        $size =  '64x82';
    }elseif($_GET['size']=='sideinfo'){
        $size =  '119x136';
    }
} else {
    $size = "119x136";
}
$herodetail = $database->HeroFace($uid);
if($herodetail['color']==0){
    $color = "black";
}
if($herodetail['color']==1){
    $color = "brown";
}
if($herodetail['color']==2){
    $color = "darkbrown";
}
if($herodetail['color']==3){
    $color = "yellow";
}
if($herodetail['color']==4){
    $color = "red";
}
$geteye = $herodetail['eye'];
$geteyebrow = $herodetail['eyebrow'];
$getnose = $herodetail['nose'];
$getear = $herodetail['ear'];
$getmouth = $herodetail['mouth'];
$getbeard = $herodetail['beard'];
$gethair = $herodetail['hair'];
$getface = $herodetail['face'];

// USAGE EXAMPLE: 
$body = imagecreatefrompng('img/hero/head/'.$size.'/face0.png');
if($getbeard!=5){
    $beard = imagecreatefrompng('img/hero/head/'.$size.'/beard/beard'.$getbeard.'-'.$color.'.png');
}
$ear = imagecreatefrompng('img/hero/head/'.$size.'/ear/ear'.$getear.'.png');
$eye = imagecreatefrompng('img/hero/head/'.$size.'/eye/eye'.$geteye.'.png');
$eyebrow = imagecreatefrompng('img/hero/head/'.$size.'/eyebrow/eyebrow'.$geteyebrow.'-'.$color.'.png');
if($gethair!=5){
    $hair = imagecreatefrompng('img/hero/head/'.$size.'/hair/hair'.$gethair.'-'.$color.'.png');
}
$mouth = imagecreatefrompng('img/hero/head/'.$size.'/mouth/mouth'.$getmouth.'.png');
$nose = imagecreatefrompng('img/hero/head/'.$size.'/nose/nose'.$getnose.'.png');
$face = imagecreatefrompng('img/hero/head/'.$size.'/face/face'.$getface.'.png');

// SAME COMMANDS: 
$database->imagecopymerge_alpha($body, $face, 0, 0, 0, 0, imagesx($face), imagesy($face),100); 
$database->imagecopymerge_alpha($body, $ear, 0, 0, 0, 0, imagesx($ear), imagesy($ear),100);
$database->imagecopymerge_alpha($body, $eye, 0, 0, 0, 0, imagesx($eye), imagesy($eye),100);
$database->imagecopymerge_alpha($body, $eyebrow, 0, 0, 0, 0, imagesx($eyebrow), imagesy($eyebrow),100);
if($gethair!=5){
$database->imagecopymerge_alpha($body, $hair, 0, 0, 0, 0, imagesx($hair), imagesy($hair),100);
}
$database->imagecopymerge_alpha($body, $mouth, 0, 0, 0, 0, imagesx($mouth), imagesy($mouth),100);
$database->imagecopymerge_alpha($body, $nose, 0, 0, 0, 0, imagesx($nose), imagesy($nose),100);
if($getbeard!=5){
$database->imagecopymerge_alpha($body, $beard, 0, 0, 0, 0, imagesx($beard), imagesy($beard),100);
}

// OUTPUT IMAGE: 
header("Content-Type: image/png"); 
imagesavealpha($body, true); 
imagepng($body, NULL);
?>

我试过评论标题(),以便我可以检查是否有任何错误在渲染图像之前的PHP不是这种情况。我已经尝试了标签中的所有$变量来检查这些变量是否正确呈现。他们做到了。我不知道问题可能是什么。

I have tried commenting out the header() so that I can check if there is an error in any of the PHP before the image gets rendered. This is not the case. I have tried all $variables in an tag to check if these rendered properly. They did. I am at a loss as to what the problem could be.

我现在唯一的猜测就是有一个空格或回车符以某种方式添加到最终渲染中导致它腐败。但是,我已经搜索了这个可能的空白年龄,似乎无法找到它。

My only guess right now is that there is a whitespace or carriage return which somehow gets added to the final render causing it to corrupt. However, I have searched for this possible whitespace for ages and cannot seem find it.

任何提示和/或建议都非常感谢!

Any tips and/or suggestions are extremely appreciated!

已解决
此脚本包含的其中一个文件已保存为带有BOM的UTF-8。这给图像增加了一个字节,使其损坏。将所有文件保存为UTF-8而不使用BOM修复了我的问题。

推荐答案

我看不出问题(除非有一些错误报告正在进行?),但如果你将图像从浏览器保存到文件然后在文本编辑器中打开它,问题可能是显而易见的。

I cannot see the problem (unless there are some error reporting going on?), but if you save the image from the brower to a file and then open it in a text-editor, the problem may be obvious.

编辑:结果是utf-8文件中的字节顺序标记。

It turned out to be the byte-order-mark in an utf-8 file.

这篇关于PHP imagepng()正在创建损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆