Post to server with php and not explode the respond - php

I need to post values to a server. The server will respond with a webpage. I do not want to read the response and echo to a page. I only want to post and let the server display by itself. "header(location:bla bla)" is a "get" I can't used it. With Curl or pear, I will need to explode the response and display it (is a no,no). Any other suggestions?

what about automatically submitting a form (method="POST") with javascript?

Your question is hard for me to parse, although I tried my best and I also edited it for others.
I think what you may be looking for is the following. For example, if you wanted to display a plaintext file directly in the browser:
// Do processing above...
header('Content-Type: text/plain');
echo $text;
exit;
This will have your browser output the data as if it were a text document, not an HTML page.
But, say you want to do something more exotic? Like output a PNG image?
header('Content-Type: image/png');
echo $pngData;
exit;
I hope that helps. Search online for MIME Types to find more of these values for other file types.

Related

Mask Image URL and make it look like its on your web server

How would i go about making a page called banner.php on my site and have that page look up a banner id in my db (already know how to look it up and fetch the url), but how would i go about making it seem like its the actual image destination if i wanted to use myurl.com/banner.php?bid=3 in a tag on someone else's website.
Regards,
Jarrod
Anyone who can help on how to do this its appreciated!
In your banner.php you have to actually load the image from its real server and output it again in your banner.php. Be sure to send the correct Content-Type header, so the browsers take your PHP-file as an image.
The quickest code for your banner.php I can imagine for a jpeg-image may look like this:
<?php
$imageContents = file_get_contents('http://example.com/real-banner.jpg');
header('Content-Type: image/jpeg');
echo $imageContents;
When a user then calls http://your-domain.com/banner.php it is shown as an image in the browser, not knowing where its original source was.
Hints:
Change the header Content-Type to image/png or image/gif depending on the image type.
If you are using file_get_contents(), be sure that your server support fopen wrappers, otherwise using URLs in file_get_contents() won't work. See the Notes-Section of file_get_contents()
Another method for getting content from another server would be the PHP curl library
Edit:
If you want to output the same header the original image has, you can iterate through the variable $http_response_header which gets autofilled with the headers after a file_get_contents call. Search for the Content-Type header and output it the same.
<?php
$imageContents = file_get_contents('http://example.com/real-banner.jpg');
// get the content type header out of the file_get_contents request
foreach ($http_response_header as $header) {
if (strtolower(substr($header, 0, 13)) == 'content-type:') {
$origContentTypeHeader = $header;
break;
}
}
header($origContentTypeHeader);
echo $imageContents;

How do you return an image without the filename or extension on the url

So there is this webpage where this guy managed to return raw images from the server
just by typing the id as a parameter.
http://photos.iitm.ac.in/byid.php?id=008576
even if you right click the image and open in a new tab, only the php gets opened.
does anyone know how he managed to do this?
I need this kind of functionality for profile pics
It's pretty simple. You just have to send the data with the right Content-type. So if you want to show a gif-image just uste:
Header("Content-Type: image/gif");
and then send out the image data.
You can do like this :
Suppose example URL is :
http://www.example.com/getImage.php?id=20
So, on getImage.php page get user image information from database and provide the image source :
Code should be like this:
$userImageName="testImage.gif"; //FROM DATABASE FOR id=20, Just for example
ob_clean(); //CLEAN THE OUTPUT BUFFER
header("Content-Type: image/gif"); //SET PAGE HEADER FOR IMAGE
echo file_get_contents('imageDirectory/'.$userImageName);
die;
Although it's not a direct answer to the asked question, since it was already answered by other people, I think it's nonetheless worth mentioning.
You can also control the file name that appears when you try to save the picture! To do that, use following header:
header('Content-Disposition', 'inline;filename=my_image_title.jpg');
so that your script looks like this:
header('Content-Disposition: inline;filename=my_image_title.jpg');
header('Content-Type: image/jpeg');
readfile($pathToImage);
//or echo $imageContent
Just make sure that you didn't accidentally output anything before making first call to header(), otherwise you might get "headers already sent" error.

How to dynmically draw picture in php gd

Hi I have searched the web for 2 days but did not accomplish what I am looking for.
I have an apache server which will be accessed by 146 students. the user picks an angle from dropdown lets say 45 degress, then user clicks CALCULATE button. Then user clicks DIAGRAM button to see how the sine graph looks like.
Works like charm when i write the image to a file e.g: imagepng($img,"diagram.png");
Now the problem is that the diagram.png will always get overwritten by the last user. So for example if another user logs in and calculates the Sin 135. Both users will see Sine 135 because filename is hardcoded since there is conflict of filename.
I have searched the web on how to create the image dynamically instead of writing to a file and then reading the file. I have come across the following but not working:
base64_encode and decode
What would I have to do to my code of imagepng(...., ...) mentioned above to make use of base64 so I can actually draw the picture of already processed data. Let assume if I comment out the imagepng(..) code, then what do I replace it with. I hope I don't have to change my code a whole lot.
Please help
thanks
Amit
The filename argument to imagepng is optional. From the manual:
filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
You would just need to send a png header at the top of the script and you would get the image as output for that script.
It's hard to tell without seeing you code how it is structured
but if once the user submits the form all you do is show the image by itself, then you can do something like this.
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
If you embed the image into an html page as the result, then your best best would be to change the url of the image on the success page to something like this.
<img src="/path/to/file.php?deg=45" />
Then in the file.php
$deg = $_GET['deg'] + 0; // make sure it is a number
$img= function_render_graph($deg);
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
By using a GET request, rather then a POST request then the image will likely be cached by the browser, so it doesn't need to be rendered each time. (Given that you have a drop list of angles, there must be a limited number of graphs that can actually be drawn)
Draw_Resultant_Prism_Graph (parameters)
{
$img = imagecreatetruecolor(800,750);
....
....
...
the following lines captures the data from output buffer and displays on same screen
***some version of IE have some issues mostly the dumb terminals where IE update is ADMIN
***restricted
ob_start();
header("Content-type: image/jpeg");
imagepng($img);
$output = ob_get_contents();
ob_end_clean();
imagedestroy($img);
echo img src="data:image/jpeg;base64,'.base64_encode($output).'"
user tags around img above and semicolon af
}

Display image on strstr match

Trying to display an image only for Android users, but I don't want to use any redirection for it.
So far I have this, but it uses header() which redirects.
if (strstr($_SERVER['HTTP_USER_AGENT'],"Android")) {
header("Location: app.png");
}
You can just output the image in your page, using readfile. Make sure to send the correct headers as well.
For example:
if(strstr($_SERVER['HTTP_USER_AGENT',"Android"))
{
header('content-type: application/png'); //Let the client know its a png.
readfile('app.png');
}
This way any request made to your page using Android will result in the raw image being returned. If you want to 'force' the client to download the image send the content-disposition header as well.
It depends on what you want to do.
If you want to display the image inside an HTML document, why not use this kind of code?
if (strstr($_SERVER['HTTP_USER_AGENT'],"Android")) {
echo '<img src="app.png"/>';
}

Outputting an image from the same PHP file which also receives & sends JSON

Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code.
Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. Javascript then refreshes the image on screen.
This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards.
My ideal solution would be something like:
header('Content-Type: image/gif');
echo $this->canvas;
// Save user file
$this->canvas->writeImage( $this->userFile = 'user_img.gif' );
$this->canvas->destroy();
// encode everything and send to browser
echo json_encode(array('misc data back to the browser'));
(I still need to send data back to browser via JSON)
And in my HTML I would have the image laid out like this:
<img src='json-processing-script.php' />
But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers.
In your example, the json would be added to the gif, messing up your image. If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image.
See for example this question.

Categories