duanjia9577 2012-11-21 13:41
浏览 124
已采纳

PHP imagepng()正在创建损坏的图像

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

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!

SOLVED One of the files this script was including was saved as UTF-8 with BOM. This gave the image an extra byte making it corrupt. Saving all the files as UTF-8 without BOM fixed my problem.

  • 写回答

2条回答 默认 最新

  • dotj78335 2012-11-21 13:45
    关注

    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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)