0

Few days ago i was looking for way to use my custom php on wordpress site and i found answer here: How to add a php page to Wordpress.
Tried few examples and it worked well for each of them. But then i wanted to go further and use PHP image processing in wordpress page added like that. It works on normal php site (using JUST this code), but when i try to use it in worpress i get error:

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

My code i am trying to use is:

<?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);
?>

Well i know the problem is i am trying to send header here, but i dont know how to solve it.
I tried moving this header to top of the file, but then whole worpress site don't load.

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

So looks like get_header(); is somehow "problem" here.
Tried using ob_start(), flush, clean etc, but then i get the image i want, without worpress site loading. I ran out of options, and non of these solved my problem.

My question is: How to send this header to make it work, cuz without it i see some weird symbols instead of png image (like i would open png image with notepad)

Any help appreciated.

Community
  • 1
  • 1
Kedor
  • 1,488
  • 5
  • 29
  • 53

2 Answers2

2

I encountered this problem when i had a space/newline after the closing php tag "?>" I removed it from all of my files and it worked for me. Pretty weird but it worked.

sulabh
  • 1,097
  • 1
  • 8
  • 22
  • Unfortunately that's not the case for me. Well, there is no space/newline in my files, but it would be impossible to search trough all the wordpress files. – Kedor Jul 06 '12 at 09:47
2

Okay, found solution to this. Posting answer as it might be useful for someone else in the future.

So I will be using simple words, which made me understand this.
header('Content-Type: image/png'); makes whole site to be "treated as image". So when I even found the place where to put my header in WordPress files, it still didn't show any result at all. To solve this problem, just create another file called image.php and put the PHP GD code there.

$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);

Of course in <?php ?> tags, and in WordPress page, where you want display it use <img src = "image.php">. This will get rid of all errors with headers, and also will display already created image by this PHP code.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Kedor
  • 1,488
  • 5
  • 29
  • 53