Display image on strstr match - php

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"/>';
}

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;

Post to server with php and not explode the respond

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.

how to send png image from server to display in browser via ajax

I have been having a hard time with what must be an incredibly normal task. I upload and save images to my web server and save the path to the file in MySQL data base (this is all working). The thing that doesn't work is fetching an image file from the server and displaying it on the page via ajax.
Originally I was trying to just retrieve the path from the database, and update an tag's src attribute with the path to the image. This was working, but this way all the images are in a folder on the server where people all have access to them. This is not good. I can only have the pictures that belong to certain users accessible by these users.
In order to restrict access to these photos I added an Apache directive on that folder, which successfully restricted access. The problem then became that I could not display the images in the browser by setting the src attribute to that path. See my post: https://serverfault.com/questions/425974/apache-deny-access-to-images-folder-but-still-able-to-display-via-img-on-site
Finally I have learned that I have to use PHP to read the image data directly from the server and send this data to the browser. I have used the file_get_contents() function, which works to convert the image file (PNG) on the server into a string. I return this string to the browser in an ajax call. The thing I can't get is: how to convert this string back into an image using JavaScript?
See this code:
$.ajax({
url: get_image.php,
success: function(image_string){
//how to load this image string from file_get_contents to the browser??
}
});
You could display a default "no access" image to users who are forbidden to access the image:
<?php
$file = $_GET['file']; // don't forget to sanitize this!
header('Content-type: image/png');
if (user_is_allowed_to_access($file)) {
readfile($file);
}
else {
readfile('some/default/file.png');
}
And, on the client side:
<img src="script.php?file=path/to/file.png" />
Alternatively, if you really really want or need to send the data via Ajax, you can Base64 encode it:
<?php
echo base64_encode(file_get_contents($file));
And place the response in an img tag using the Data URI scheme
var img = '<img src="data:image/png;base64,'+ server_reponse +'"/>';
Given that the Base64 encoded data is significantly larger than the original, you could send the raw data and encode it in the browser using a library.
Does that make sense to you?
Instead of getting get_image.php through AJAX, why not just use:
<img src="get_image.php" />
It's practically the same thing. You can just use AJAX to update the <img> dynamically.
You can't do it via ajax.
You could do something like this:
<img src="script.php?image=image_name" />
Then use JavaScript to change the query string.
You can actually embed image data inside the img tag in the browser, therefore ajax code could look like this:
$.ajax({
url: get_image.php,
success: function(image_string){
$(document.body).append("<img src='data:image/gif;base64," + base64_encode(image_string) + "' />);
}
});
Note that you will need to write this base64_encode function. Have a look at this question - the function is given there.

PHP - send GET request and get picture in return

I need to send a GET request to my page pic.php, and I want to get a real picture in return.
For now I implemented this idea like this:
<?php
if ((isset($_GET['pic']))&&(isset($_GET['key']))){
$pic = $_GET['pic'];
$pic=stripslashes($pic);
header('Location: /content/'.$pic);
}
?>
But it's not really what I want - it redirects to image directly. What I want is to keep the same URL, but get a needed file depending on what values were submitted.
What is the best way to do that?
thx.
This example code snippet should do what you ask. I've also included code to only strip slashes if magic quotes is enabled on the server. This will make your code more portable, and compatible with future versions of PHP. I also added use of getimagesize() to detect the MIME type so that you output the proper headers for the image, and do not have to assume it is of a specific type.
<?php
if(isset($_GET['pic']))
{
//Only strip slashes if magic quotes is enabled.
$pic = (get_magic_quotes_gpc()) ? stripslashes($_GET['pic']) : $_GET['pic'];
//Change this to the correct path for your file on the server.
$pic = '/your/path/to/real/image/location/'.$pic;
//This will get info about the image, including the mime type.
//The function is called getimagesize(), which is misleading
//because it does much more than that.
$size = getimagesize($pic);
//Now that you know the mime type, include it in the header.
header('Content-type: '.$size['mime']);
//Read the image and send it directly to the output.
readfile($pic);
}
?>
I can see you doing this in two ways:
1) Return the URL to the image, and print out an image tag:
print '<img src=' . $img_url . ' />';
2) Alternatively, you could just pull the data for the image, and display it. For instance, set the header appropriately, and then just print the image data.
header("content-type: image/png");
print $img_data;
This assumes that you have the image data stored in a string $img_data. This method will also prevent you from displaying other things on the page. You can only display the image.
You can load the image, send the image headers, and display the image as such:
header('Content-Type: image/jpeg');
readfile('/path/to/content/pic.jpg');
Obviously the headers would depend on the filetype, but that's easy to make dynamic.
Not sure if I understand what you're after, but guessing that you want to load the picture in an img tag?
If I'm right you just do:
<img src=http://www.domain.com/pic.php?"<?php echo image here ?>" />
Basically you just make the source of the image the webpage you get directed to where the image is.

How do I load an image in PHP

I want code that loads an image to a PHP server and then send it to browser.
For example I want sample.php to send an image to browser once it is requested.
in other words, I want to create a PHP file that acts like a proxy for an image.
why are you doing this?
why don't deliver the image directly?
if you are trying to display a random image you may as well just redirect to the image using
header("Location: address-of-image");
for delivering the file to your clients from your server and not from its original location you can just do. however your php.ini settings need to allow external file opens
readfile("http://www.example.com/image.jpg")
correct headers are not required if you are going to display the image in an img tag,
altough i would recommend it. you should check the filetype of the image or in most cases just set an octet-stream header so the browser doesnt assume an incorrect type like text or something and tries to display binary data.
to do so just do
header("Content-type: application/octet-stream")
one more thing to consider may be setting correct headers for caching...
You need to use
$image = fopen("image.png");
Modify the headers(not sure exacly if it's correct)
headers("Content-type: image/png");
And then send the image
echo fread($image, file_size("image.png"));

Categories