Hello,
I am trying to make a simple image editor by changing the brightness randomly.
I have a simple HTML page with an image and a button which is supposed to be clicked and the brightness of the image will change to a random value using POST method and a server-end PHP script.
The simple HTML is test.php as follows:
<html>
<head>
<title>
Image Editing
</title>
</head>
<body>
<p align="center"><img src="1810.jpg" border="0"></p>
<br>
<br>
<form method="post" action="zero.php">
<input type="submit" value="Change Brightness!" name="Brightness" >
</form>
<br>
<br>
</body>
</html>
The server-end PHP script (zero.php) to perform the image editing function is as follows:
<?php
if(isset($_POST['Brightness']))
{
$im = @imagecreatefromjpeg("1810.jpg");
$rand = rand(-255, 255);
imagefilter($im, IMG_FILTER_BRIGHTNESS, $rand);
header('Content-Type: image/jpeg');
imagejpeg($im, NULL, 75);
imagedestroy($im);
}
?>
This process works to a certain extent by changing brightness, but after clicking the image sent back from php is without any html info i.e., title of the page and the form submit buttons are lost.
My objective is to retain those info (title, form buttons) so that the user can click repeatedly to change the image brightness and the image sent back from the server should be always replaced only in its position.
I have also tried with the following option in a single test.php file:
<?php
echo '<html>';
echo '<head>';
echo '<title>Image Editing</title>';
echo '</head>';
echo '<body>';
echo '<p align="center"><img src="1810.jpg" border="0"></p>';
echo '<br>';
echo '<br>';
if(isset($_POST['Brightness']))
{
$im = @imagecreatefromjpeg("1810.jpg");
$rand = rand(-255, 255);
imagefilter($im, IMG_FILTER_BRIGHTNESS, $rand);
header('Content-Type: image/jpeg');
imagejpeg($im, NULL, 75);
imagedestroy($im);
}
echo '<form method="post" action="test.php">';
echo '<input type="submit" value="Change Brightness!" name="Brightness">';
echo '</form>';
echo '<br>';
echo '<br>';
echo '</body>';
echo '</html>';
?>
The initial display page is fine but just after clicking the button, the page output gets messed up with the same image (no change in brightness) on the top followed by
"Warning: Cannot modify header information - headers already sent by" error and then a huge dump of junk characters which probably caused due to a header error and the received image cannot be properly rendered.
Why the initial approach don't work according to my expectation and what are the changes required to make it working?
Why the latter approach is causing header errors?
Thanks!