2014-09-04 75 views
-2

當我運行這段代碼,我得到了以下錯誤:GD2 URL表示作爲一個圖像

圖像「http://siteprevue.net/flipit.php」無法顯示,因爲它包含錯誤。

我在想...當然,它不能被顯示,它是一個網址,而不是圖像...呃?!

一切正常,我只是得到這個認爲這個url是圖像的黑色頁面。

這發生在(某些)其他GD2編碼頁面上,但不是全部。

有沒有人有任何想法發生了什麼?

<?php 
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg'; 
$new_img = '../../Uploads/Gallery/copy_bg_200.jpg'; 

$image = imagecreatefromjpeg($src); 
$image = flip($image,1,0); // flips horizontal 
//$image = flip($image,0,1); // flips vertical 
//$image = flip($image,1,1); // flips both 

header("Content-type: image/jpeg"); 
imagejpeg($image, $new_img, 80); 
imagedestroy($image); 

function flip($i,$h=1,$v=0) { 
$width = imagesx($i); 
$height = imagesy($i); 
$temp = imagecreatetruecolor($width,$height); 
imagecopy($temp,$i,0,0,0,0,$width,$height); 
if ($h==1) { 
for ($x=0 ; $x<$width ; $x++) { 
imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height); 
} 
imagecopy($temp,$i,0,0,0,0,$width,$height); 
} 
if($v==1) { 
for ($x=0; $x<$height ; $x++) { 
imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1); 
} 
} 
return $i; 
} 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
</body> 
</html> 
+1

爲什麼你的圖片包含html標籤? – JaMaBing 2014-09-04 12:19:35

+0

謝謝@CBroe您的評論指出我正確的方向。通過刪除「標題(」Content-type:image/jpeg「);」我再次向前邁進。 – Kuya 2014-09-04 14:17:24

+0

好的,我已經添加了這個(簡短)答案。 – CBroe 2014-09-04 14:20:50

回答

0

I'm thinking... of course it can't be displayed, it's a url not an image... duh?!

這一行

header("Content-type: image/jpeg"); 

你說的是瀏覽器,他在此之後收到的數據是JPEG圖像的 - 所以如果你是而不是打算之後發送圖像數據,該行沒有地方在那裏。

只需將其刪除,以便PHP將再次發送其默認的Content-Type text/html

+0

謝謝@CBroe ...當你發佈這個時,我正在發佈「我的」答案。你指出我正確的方向,所以......指向你。非常感激。也許你也可以幫我解決一個相關的問題。 http://stackoverflow.com/questions/25664985/gd2-calling-a-function-after-form-submit – Kuya 2014-09-04 14:33:57

0

首先你不需要html,因爲你的輸出是一個jpeg。 你錯過了@@imagecreatefromjpeg。 但是你的問題是輸出文件名不能與header一起工作。該圖像會成功創建,但會出現錯誤。

imagejpeg

The path to save the file to. If not set or NULL, 
the raw image stream will be outputted directly. 

您可以將文件保存奧德打印它header。所以你應該插入null。這對我的作品,但它不會存儲你的改變:

<?php 
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg'; 

$image = imagecreatefromjpeg($src); 
$image = flip($image,1,0); // flips horizontal 
//$image = flip($image,0,1); // flips vertical 
//$image = flip($image,1,1); // flips both 

header("Content-type: image/jpeg"); 
imagejpeg($image, null, 80); 
imagedestroy($image); 

function flip($i,$h=1,$v=0) { 
    $width = imagesx($i); 
    $height = imagesy($i); 
    $temp = imagecreatetruecolor($width,$height); 
    imagecopy($temp,$i,0,0,0,0,$width,$height); 
    if ($h==1) { 
     for ($x=0 ; $x<$width ; $x++) { 
      imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height); 
     } 
     imagecopy($temp,$i,0,0,0,0,$width,$height); 
    } 
    if($v==1) { 
     for ($x=0; $x<$height ; $x++) { 
      imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1); 
     } 
    } 
    return $i; 
} 

?> 
+0

我需要存儲更改。我想保存文件,而不是顯示它。這最終將成爲圖片上傳表單的一部分。我並不陌生編碼,但我是GDlib的新手。感謝:) – Kuya 2014-09-04 14:05:34

+0

你的代碼已經爲存儲工作 – JaMaBing 2014-09-04 14:06:20