2013-04-06 75 views
0

我試圖在用戶個人資料頁面中顯示用戶上傳的圖像。圖像正在成功上傳,但在顯示時顯示此錯誤:「圖像無法顯示,因爲它包含錯誤」。已上傳的圖像包含錯誤

我上傳的代碼是:

<?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;" /> 

任何人都可以解釋爲什麼圖像不顯示?

+0

如果您查看生成的DOM樹,生成的鏈接是什麼? – fredrik 2013-04-06 17:56:54

+0

'SITEURL'從哪裏來? – Dharman 2013-04-06 17:56:56

+0

你有沒有真的保存圖像?它看起來像你的上傳代碼只是將它們發送回瀏覽器作爲輸出,而不是保存到文件... – Dave 2013-04-06 18:01:25

回答

0

好吧,我看到2個問題...

首先狡猾的焦炭,文件的結尾......「`」不知道這是在實際的代碼或不...確保它不是「T

其次,改變

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

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

imagejpeg

To skip this argument in order to provide the quality parameter, use NULL. 

我相信如果你把一個空字符串它會嘗試將圖像保存爲一個文件,而不是作爲一個流發送。

+0

任何人都可以幫助我定製一個免費的php腳本嗎?我知道這幾乎是不可能的,但希望有人會出面!我只需要3/4的定製,這是一個非常簡單的專家的PHP編碼器。 – user2252617 2013-04-08 19:12:02