我試圖在用戶個人資料頁面中顯示用戶上傳的圖像。圖像正在成功上傳,但在顯示時顯示此錯誤:「圖像無法顯示,因爲它包含錯誤」。已上傳的圖像包含錯誤
我上傳的代碼是:
<?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;" />
任何人都可以解釋爲什麼圖像不顯示?
如果您查看生成的DOM樹,生成的鏈接是什麼? – fredrik 2013-04-06 17:56:54
'SITEURL'從哪裏來? – Dharman 2013-04-06 17:56:56
你有沒有真的保存圖像?它看起來像你的上傳代碼只是將它們發送回瀏覽器作爲輸出,而不是保存到文件... – Dave 2013-04-06 18:01:25