Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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 上载的图像包含错误_Php_Image - Fatal编程技术网

Php 上载的图像包含错误

Php 上载的图像包含错误,php,image,Php,Image,我试图在用户配置文件页面中显示用户上传的图像。图像上载成功,但显示时显示此错误:“图像包含错误,无法显示” 我的上传代码是: <?php session_start(); require_once("config.php"); header ("Content-type: image/jpg" ); if ( $_GET["id"] != "" ) { if ( file_exists ( "media/userphotos/".$_GET["id"].".jpg" ) )

我试图在用户配置文件页面中显示用户上传的图像。图像上载成功,但显示时显示此错误:“图像包含错误,无法显示”

我的上传代码是:

<?php
session_start();
require_once("config.php");
header ("Content-type: image/jpg" );

if ( $_GET["id"] != "" )
{
    if ( file_exists ( "media/userphotos/".$_GET["id"].".jpg" ) )
        $imageToShow = SITEURL."media/userphotos/".$_GET["id"].".jpg";
    else
    {
        $imgNumber = rand ( 1, 5 );
        $imageToShow = SITEURL."images/nimg$imgNumber.jpg";
    }
}

if ( strchr ( $imageToShow , ".gif" ) )
    $image = imagecreatefromgif ( $imageToShow );
else
    $image = imagecreatefromjpeg ( $imageToShow );

list ( $width, $height ) = getimagesize ( $imageToShow );

$diffWidth = 1;
$diffHeight = 1;

if ( $width > $height )
{
    if ( $width > 130 )
    {
        $diffWidth = 1 - ( ( $width-130 ) / $width ) ;
        $diffHeight = $diffWidth ;
    }
}
else
{
    if ( $height > 110 )
    {
        $diffHeight = 1 - ( ( $height-110 ) / $height ) ;
        $diffWidth = $diffHeight ;
    }
}

$modwidth = $width * $diffWidth ;
$modheight = $height * $diffHeight ;

//$modwidth = 130 ;
//$modheight = 110 ;

// Resizing the Image
$tn = imagecreatetruecolor ( $modwidth, $modheight ) ;
imagecopyresampled ( $tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height ) ;

/******** this line outputs image as thumbnail or not ( conditioned ) *********/

if ( $tn )
    imagejpeg( $tn , "" , 99 ) ;
else
    imagejpeg( $image , "" , 99 ) ;

imagedestroy ( $image ) ;

?>

要显示我使用的图像,请执行以下操作:

<img src="<?php echo SITEURL."userimage.php?id=".$userDetails["UserID"] ?>" style="width:120px; padding:2px; border:#999999 1px solid;" />
style=“宽度:120px;填充:2px;边框:#999999 1px实心;" /> 有人能解释为什么图像不显示吗?

好的,我看到两个问题

首先是一个不可靠的字符和文件的结尾…“``不确定这是否在实际代码中…确保它不是

第二,改变

if ( $tn )
    imagejpeg( $tn , "" , 99 ) ;
else
    imagejpeg( $image , "" , 99 ) ;


我相信如果你输入一个空字符串,它会尝试将图像保存为文件,而不是以流的形式发送。

如果你查看生成的DOM树,生成的链接是什么?SITEURL从哪里来?你是否确实保存过图像?看起来你的上传代码只是将它们作为输出发送回浏览器,而不是正在保存到文件…您可能希望启用
错误报告
并注释掉
标题('Content-Type…')
这样您就可以发现潜在的PHP错误,并查看浏览器中的纯文本内容。有人能帮我免费定制一个PHP脚本吗?我知道这几乎是不可能的,但希望有人能站出来!我只需要3/4的定制,这对于专业的PHP程序员来说非常简单。
if ( $tn )
    imagejpeg( $tn , NULL , 99 ) ;
else
    imagejpeg( $image , NULL , 99 ) ;
To skip this argument in order to provide the quality parameter, use NULL.