...

View Full Version : header( "Content-type: image/png" ); not working



hddctr
07-20-2012, 03:01 PM
when i first pull up my webpage the png file shows up but when i submit a different image all i get is garbage on the screen like:
�PNG  IHDR�<虻�PLTEK7(�oL�g8��l�zFӜh�n=ɯ��Ŗ�׹��Oԗ\깇�tBŋU�U-.$������e��m[L�Z2κ�UON�̤tJ'���Nܢg�ue8)!���jE'꽔�������ŴUC5daaxcQ��v~~~צz8QL��{��Ȭ��yX:��o'''��������l��������Ү�� �ʒ]!�d?aJ7~R,���C.!���Ĥ|Ktkqpp������t���ӿ����Ͷ�^3(96�l=�������wOeQA��؍[.j] #!�����������b:96͜~W���~NA0'}N(sO1��ΰ���$/,Z;#ᲊ/EB��u��������w���ӓT�b4�~Gڝa��`̎R�xwwc@$�Ϡ>`[ݝZ�z���IEE����

myfirstpage.php



<form action="created_banner.php" method="post" id="banner_creator_form">
<div id="banner_generator">
<input type="hidden" name="banner_design" id="banner_design" />
<input type="hidden" name="banner_fontstyle" id="banner_fontstyle" value="verdana" />
<input type="hidden" name="banner_fontsize" id="banner_fontsize" value="12" />
<input type="hidden" name="banner_fontcolor" id="banner_fontcolor" value="ffffff" />
<input type="hidden" name="banner_textx" id="banner_textx" value="0" />
<input type="hidden" name="banner_texty" id="banner_texty" value="0" />

<div id="banner_preview_container">

this is the form that posts the new file name


mysecondpage.php


$img = $_POST [ banner_design ];
if ( $img == "" ){ $img = "blank-banner"; }
//$banner_text = $_POST [ banner_text_content ];
//$banner_fontstyle = $_POST [ banner_fontstyle ];
//$banner_fontsize = $_POST [ banner_fontsize ];
//$banner_fontcolor = $_POST [ banner_fontcolor ];
//$banner_textx = $_POST [ banner_textx ];
//$banner_texty = $_POST [ banner_texty ];
$this_img = "images/free-banner/".$img.".png";
$my_img = imagecreatefrompng ( $this_img );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, $banner_text, $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );
header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $my_img, $background );
imagecolordeallocate( $my_img, $text_colour );
imagecolordeallocate( $my_img, $line_colour );
imagedestroy( $my_img );


this is the page that should display the image with text... when i first pull the page up the blank-banner.png shows fine but when i submit a new file name all i see is garbage. i have been fighting this for some time now and cannot figure it out.

Fou-Lu
07-20-2012, 03:33 PM
Looks to me that your headers are already sent.
Is that exactly the code in the mysecondpage.php? And is that exactly the output? Given what I see in the first line of mysecondpage.php, you have an error on the first line.

hddctr
07-20-2012, 03:43 PM
mysecondpage.php is exactly the code i'm using, i think the trees are blocking my view to the forest, i dont see the error you are referring to, i tried moving the header ( Content to the first line and it makes no difference

Fou-Lu
07-20-2012, 03:50 PM
This is invalid:


$img = $_POST [ banner_design ];

banner_design has not been declared as a constant in this code. Assuming that's a string, that should be


$img = $_POST ['banner_design'];
// or
$img = $_POST ["banner_design"];


If that's the exact code you are using, I'm not sure how you are getting any output other than PHP code. This is missing the <?php tags required to start the PHP engine.

Moving the header will only make a difference if its before all output. If error's are being spit out before the header is sent, then it will fail to push the header as well. This has to be before any output whatsoever.

hddctr
07-20-2012, 03:59 PM
sorry your right i didnt include the <?php tags in the copy some forums don't allow it.

this is the exact code:

[CODE]
<?php
$img = $_POST['banner_design'];
if ( $img == "" ){ $img = "blank-banner"; }
//$banner_text = $_POST [ banner_text_content ];
//$banner_fontstyle = $_POST [ banner_fontstyle ];
//$banner_fontsize = $_POST [ banner_fontsize ];
//$banner_fontcolor = $_POST [ banner_fontcolor ];
//$banner_textx = $_POST [ banner_textx ];
//$banner_texty = $_POST [ banner_texty ];
$this_img = "images/free-banner/".$img.".png";
$my_img = imagecreatefrompng ( $this_img );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, $banner_text, $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );
header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $my_img, $background );
imagecolordeallocate( $my_img, $text_colour );
imagecolordeallocate( $my_img, $line_colour );
imagedestroy( $my_img );
//echo "<br /><br /><img src='".$this_img."'><br />".$banner_text."<br />".$banner_fontstyle."<br />".$banner_fontsize."<br />".$banner_fontcolor."<br />".$banner_textx."<br />".$banner_texty;
?>
[CODE]

i changed to [ICODE]$img = $_POST['banner_design'];[ICODE] but i am still getting the same result

Fou-Lu
07-20-2012, 04:14 PM
Enable your error reporting:


ini_set('display_errors', 1);
error_reporting(E_ALL);


For the first lines of the code, then run it. Does it pump errors for headers sent? If so, post those errors.

hddctr
07-20-2012, 04:21 PM
i placed it on the first line and removed the rem [ICODE]$banner_text = $_POST ['banner_text_content'];[ICODE] and got no errors but a lot of garbage

Fou-Lu
07-20-2012, 06:11 PM
Comment out the imagepng(). Keep the error reporting enabled, and run it again.
What is the output you get?

hddctr
07-20-2012, 06:18 PM
believe it or not i "just" resolved that problem but have created a new one...

im using javascript to direct the image to a certain <div> container

[CODE]

$(document).ready(function(){
$('#banner_creator_form').ajaxForm({
target: '#created_banner',
beforeSubmit: function(){
if(!design_chosen) {
alert('Please select a banner design.');
return false;
}
var pos = $("#banner_text").parent().position();
var left = pos.left;
var top = pos.top;

$("#banner_textx").val(left);
$("#banner_texty").val(top);
//alert(left);
//alert(top);
$('#created_banner').php('<img src="images/free-banner/loading2.gif" />');
},
success: function(){}
});
$("#banner_text").draggable({
stop: function(event, ui){
alert("ASDSA");
}
});
});
[CODE]

and had .html where .php is now...

but my new problem is that the image is not displaying in the #created_banner <div> its displaying on a parent page by itself... got any ideas?

DrDOS
07-20-2012, 06:32 PM
I just see tons of things that are either wrong, or could be greatly simplified. Whenever you use a form, and post or get, you should wrap the working code on the download page in an isset($_POST['some input name here'] statement. If you have defaults you need to have an !isset ( not isset ) statement too. For one your first input has no declared value. If you're using javascript to gather data and fill out the form, it's still a good idea to have default values. You can make all your inputs an array like this: name="myins[]".

Forms are one of the things that are really important to learn in php, and php really does them well. If you want something important to learn and practice on in php you can't do better than forms. And arrays, too, I use them wherever I can because php does them well too. I've reached the point where I can write a form and submission page from the top down, using arrays as much as I can to simplify my code, and with very few errors, and that would be a good goal for anyone who wants to learn php.

hddctr
07-20-2012, 06:49 PM
i appreciate your comments DrDos, and i'm sure you are quite the coder but your reply doesn't get me any closer to solving my issue, but i will be sure to ask for you when i have issues writing a form and submission page with arrays in fact if you'll write the book i'll be sure and pick up a copy.

DrDOS
07-20-2012, 07:19 PM
i appreciate your comments DrDos, and i'm sure you are quite the coder but your reply doesn't get me any closer to solving my issue, but i will be sure to ask for you when i have issues writing a form and submission page with arrays in fact if you'll write the book i'll be sure and pick up a copy.There are a lot of people here who are more proficient that I, I consider my level a sort ot minimum, but it's important to reach that minimum. Your issue isn't just one, it's several, no one here is likely going completely rewrite your form, since none of us quite entirely know how you are using it.

hddctr
07-20-2012, 07:57 PM
sir, being the dumb *** that i am i might not be able to write the code that someone of your caliber can but i am learning and part of learning is asking questions and asking someone else to look at a problem so hopefully someone who knows or has had the same issue in the past can point me in the right direction. i really don't remember asking if someone could re-write my code just simply looking for pointers which Fou-Lu was so kind to do (thank you for your help). i chose this forum because it looked like a group of coders that really didn't mind helping others out. are you a moderator? if you are then your the type of moderator that gives "help forums" a bad reputation, if not then your on this forum for the same reason i am, you got stuck on a problem and had to ask someone else. this issue is closed.

DrDOS
07-20-2012, 08:10 PM
sir, being the dumb *** that i am i might not be able to write the code that someone of your caliber can but i am learning and part of learning is asking questions and asking someone else to look at a problem so hopefully someone who knows or has had the same issue in the past can point me in the right direction. i really don't remember asking if someone could re-write my code just simply looking for pointers which Fou-Lu was so kind to do (thank you for your help). i chose this forum because it looked like a group of coders that really didn't mind helping others out. are you a moderator? if you are then your the type of moderator that gives "help forums" a bad reputation, if not then your on this forum for the same reason i am, you got stuck on a problem and had to ask someone else. this issue is closed.No, I'm not a moderator, and I just help people out too, but you sort of have too many problems all at once, not just few. Just because I'm telling you something that you don't want to hear doesn't make me bad person. You can look on the side panel and see how many posts I've made and how many people bothered to thank me. I'm being kind too because I'm telling you something you need to know, even though you may not appreciate it now.

Fou-Lu
07-20-2012, 08:10 PM
Everyone be calm yes. Thanks :]

I can't really help you with the JS aspect I'm afraid; providing that in the JS forum will let the Client Side developers take a looksee. You may need to give a live link for it.

DrDOS is right in the sense that you should start into good habits early.
So with PHP code, you always need to do a check if it exists (but don't choose a submit button as your check as IE does not provide this as successful if the form has <= 1 input element within it). So that's a simple branch in your code:


if (isset($_POST['banner_design']))
{
$img = $_POST['banner_design'];
// ... rest of processing here.
}
else
{
// Given the purpose of this script, I'd put a default image to show in here.
}

That gets around a notice that you receive when you attempt to access an offset that doesn't exists. So if the form wasn't submitted to get here for example, it will throw tons of errors unless it is checked.

Same is done for some of the function calls. Using things like imagecreatefrom* you want to check that it successfully creates the image (whilst suppressing errors):


if ($im = @imagecreatefromjpg($sImagePath))
{
// if your here, then it successfully created the image from the path
}
else
{
// default image (assuming this script serves as an image)
}


Hope that helps!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum