2012-07-06 17 views
0

幾天前,我正在尋找方式使用我的自定義PHP的WordPress的網站,我在這裏找到答案:How to add a php page to Wordpress
試過幾個例子,它們對每個例子都很好。但後來我想更進一步,並在wordpress頁面中使用PHP圖像處理。它(只使用此代碼)在普通的PHP網站的作品,但是當我嘗試在worpress使用它我得到錯誤:添加PHP文件到wordpress - Header()已經發送

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\wordpress\wp-content\themes\twentyeleven\header.php:13) in C:\xampp\htdocs\wordpress\wp-content\themes\twentyeleven\mynewsite.php on line 26 

我的代碼我試圖使用方法是:

<?php 
$dest = imagecreatefrompng('image1.png'); 
$src = imagecreatefromjpeg('image2.jpg'); 

imagealphablending($dest, false); 
imagesavealpha($dest, true); 
imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //putting one image on top of other 

header('Content-Type: image/png'); 
imagepng($dest); 
imagedestroy($dest); 
imagedestroy($src); 
?> 

那麼我知道問題是我想在這裏發送標題,但我不知道如何解決它。
我試着將這個標題移動到文件的頂部,但整個worpress站點不加載。

header('Content-Type: image/png'); 
get_header(); //<-- part of wordpress template, cant get rid of it, cuz it ruins whole site look. 

所以看起來像get_header();這在某種程度上是「問題」。
嘗試使用ob_start(),刷新,乾淨等,但後來我得到我想要的圖像,沒有worpress網站加載。我用盡了選項,其中沒有解決了我的問題。

我的問題是:如何發送這個頭,使其工作的辯論,因爲沒有它,我看到一些奇怪的符號代替PNG圖片(像我會用記事本打開PNG圖片)

任何幫助表示讚賞。

回答

2

好的,找到了解決辦法。發佈答案,因爲它可能對未來的其他人有用。

所以我會用簡單的話,這讓我明白這一點。
header('Content-Type: image/png');使整個網站被「視爲圖像」。所以當我甚至找到了將我的標題放在WordPress文件中的地方時,它仍然沒有顯示任何結果。要解決這個問題,只需創建另一個名爲image.php的文件並將PHP GD代碼放在那裏。

$dest = imagecreatefrompng('image1.png'); 
$src = imagecreatefromjpeg('image2.jpg'); 
imagealphablending($dest, false); 
imagesavealpha($dest, true); 
imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //putting one image on top of other 
header('Content-Type: image/png'); 
imagepng($dest); 
imagedestroy($dest); 
imagedestroy($src); 

歷程<?php ?>標籤,並在WordPress的頁面,在這裏你要顯示它使用<img src = "image.php">。這將消除所有標題錯誤,並且還將顯示已經創建的圖像通過這個PHP代碼。

2

我遇到了這個問題,當我在關閉php標籤後有一個空格/換行符「?>」我從我的所有文件中刪除它,它對我有用。很奇怪,但它的工作。

+0

不幸的是,我不是這種情況。那麼,我的文件中沒有空格/換行符,但是不可能搜索所有的wordpress文件。 – Kedor 2012-07-06 09:47:24