How display img without the file extension - php

I want to show an img like this:
http://picviewer.umov.me/Pic/GetImage?id=92013681&token=ee103380297bbb2df0d8855949d791df
How should i use php to show the img with dynamic parameters?

You need to set the proper content type headers and then stream the binary data.
$imagepath = '/path/to/file';
header("Content-type: image/jpeg");
echo file_get_contents( $imagepath );

First i'll explain what does that URL do: it's configured to point to a script, or class->function, that manages the searching in the database, for the real path, the path you want to hide for the user, then returns the image to the request.
That said, in two steps, what you have to do is the following.
Check how to customize your url, this could be a start:
How to create friendly URL in php?
create your custom url pointing to a script or class/function like this:
Return a PHP page as an image
Thats, all... Assuming that you already have the images/database covered of course. if not, well you'll have to make the necessary database and tables...

Related

Programmatically (PHP) save image with no extension

I am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.

Read multiple image files using php readfile

I am trying to read multiple image files from a folder (.htaccess protected) and display in a HTML page using php readfile().
The problem is I can see only the first image is read and the next is not shown in the browser. The code is as below
<?php
$image1 = 'files/com_download\256\50\www\res\icon\android\icon-36-ldpi.png';
$image2 = 'files/com_download\256\50\www\res\icon\android\icon-48-mdpi.png';
$imginfo = getimagesize($image1);
header("Content-type: ".$imginfo['mime']);
readfile($image1);
$imginfo = getimagesize($image2);
header("Content-type: ".$imginfo['mime']);
readfile($image2);
?>
I could see the first image 'icon-36-ldpi.png' successfully read and displayed in the browser and the second image is not read and not displayed in the browser.
Am I missing something? Any advice please.
Sorry if I am doing stupid but the requirement is to read multiple image files and render in the browser like a grid view. I cannot use img tag because of security reasons.
You can't dump both images out at once. Why not make two images in your html so the browser makes two calls to your script. Then use a GET param to pass the filename you want to display.
---Edit---
Important Security Note
There is an attack vector which you open up when doing soething like this. Someone could easily view your source html and change the parameter to get your image script to output any file they want. They could even use "../../" to go up directories and search for well known files that exist. e.g. "../../../wp_config.php". Now the attacker has your wordpress database credentials. The correct way to prevent against this is to always validate the input parameter properly. For example, only output if the file name ends with ".jpg"

How to get current URL in a PHP image?

I want to get current URL inside a PNG generated by PHP (header('Content-Type: image/png');)
But when i use http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];,
The URL output is the file's location (http://test.com/png.php) but not the page which get the img with <img> tag.
What can i do to get current URL?
Thanks!
Sounds like you're looking for $_SERVER['HTTP_REFERER'], not REQUEST_URI.
I'm not sure I understood you, but I'll give it a go. If you serve an image via PHP, it's URL will always point to a PHP script. If you want it to look like it's really a .png image, you have 2 choices:
1) Create a rewrite rule which will redirect some .png URL to your PHP script and then serve that URL to users.
2) Stick with the current URL, but also add an (non-standard!) Content-Disposition header:
header('Content-Disposition: attachment; filename="my_image.png"');
This way the URL will still point to a PHP script, but when a user tries to save the image, the suggested name won't be the PHP script, but the name specified in Content-Disposition.

PHP: set image as background without revealing path

The following problem I can't really wrap my mind around, so really if you guys can't be bothered to supply the entire code some tips leading in the right direction would be great!
So, I have a script where users can upload images to a server. PHP takes care of validating the file and saving it using a new filename in another folder, neither known by the client. Now, the client should be able to see the uploaded image, in html simply:
style="background-image:url('testimagegif.gif');
But preferably the client should not be able to see the path nor the file name of the image saved on the server. I know about using header('Content-type: ... for forcing the client browser to download files, but I do not see how this, nor any similar solution could be applied to this case. Same goes for readfile. If I use it the browser simply downloads the image, not placing it in the html.
You should probably be moving the files into a publicly readable folder on your webserver if you want to serve them.
Otherwise, you'll need something like readfile()
There are two options for this, you could use the data protocol, which would embed the whole image into the URL of the background ( this isn't recommended if the image is bigger than a few kb. ) or you can use a script to present the image by encoding or recording a unique key for the image, eg bg.php?id=4323-34442-3432-4532 which checks a db for the id to retrieve the file path then echoes the content with the right content type.
Some examples;
based on the Data URI wikipedia page
Data URI Method
Assuming a function like this;
function data_uri($fileID) {
$fRecord = mysql_fetch_array(
mysql_select("SELECT filePath, mimeType from fileTable WHERE fileID = " $fileID . ";")
);
$contents = file_get_contents($fRecord['filePath']);
$base64 = base64_encode($contents);
return "data:$fRecord['mimeType'];base64,$base64";
}
Then in your html/php page you'd have the following snippet
style="background-image:url('<?php echo data_uri($fileID);?>'
PHP Image Dump
Assuming a function like this;
// Given a filename and a mimetype; dump the contents to the screen
function showDocumentContent($fileID){
$fRecord = mysql_fetch_array(
mysql_select("SELECT filePath, mimeType from fileTable WHERE fileID = " $fileID . ";")
);
header( 'Content-Encoding: none', true );
header( 'Content-Type: ' . $fRecord['mimeType'], true );
echo readfile( $fRecord['filePath'] );
}
Then in your html page you'd have this;
style="background-image:url('image.php?fileID=123')
In the first case, images larger than a few KB will result in equally large HTML pages, and may not be supported in browsers consistently. In the second case, you'd effectively have created a php script that is pretending to be an image. In both cases, the real path to the binary files on your server is abstracted away by storing a mapping in a database.
If you store the paths to the files somewhere like a database or a file, you can use readfile() to output the file once you retrieve the path.
Combine that with the content-type header, and set the background-image URL to the PHP script with the correct query string like so:
style="background-image:url('script.php?img=30382');"
You must expose some path to the client, because their browser has to access the file. You can use your webserver config to serve at an indirected location, or serve the image with PHP and have the real path in a call to readfile()

Get image source from given URL with javascript or PHP

What I want to do is following:
User takes screenshot with the application like jing. ok!
Pastes link that Jing returned back. ok!
Server processes the link that user entered, and extracts images source url. But, I have no idea how server will get "clean" image source URL. For example, this is the link that Jing returned after sharing screenshot http://screencast.com/t/zxBzNNkcg but real url of image looks like http://content.screencast.com/users/TT13/folders/Jing/media/c25ec5c6-bc6a-413c-a50b-ada95fac4ed2/2012-07-25_0221.png
Server returns back image source URL. no idea!
Is there any possible way to get image url with Javascript or PHP?
you could use Simple PHP DOM Parser to retrieve the image from the url without considering the url for as long as it contains and image inside, like so:
foreach($html->find('div[class=div-that-contain-the image]') as $div) {
foreach($div->find('img') as $img){
echo "<img src='" . $img->src . "'/>";
}
}
That is my solution.
You can retrieve the page containing the image using a DOM library like Query Path.
Using that you can extract the URL to the image.
So in your step 3:
Get source of shared screenshot page (maybe use file_get_contents)
Extract screenshot's image src, using Query Path.
Return image src URL to user
Yes. If you right click on the image and go Copy Image Location, you'll see it's http://content.screencast.com/users/TT13/folders/Jing/media/c25ec5c6-bc6a-413c-a50b-ada95fac4ed2/2012-07-25_0221.png
If you were to do it programmatically, you would use cURL and simplehtmldom.sourceforge.net to parse the outputed HTML for the actual link.
Javascript answer:
Will the output image always have the same class, embeddedObject?
If so, how about something like:
myVar = document.getElementsByClassName('embeddedObject');
myVar[0].getAttribute("src");
The myVar[0] reference of course assumes that there is only ever one image on the page with the embeddedObject class, otherwise you'd need to sort, or know which index to reference each time.
Also, this sadly doesn't seem to be supported in IE8 (which browser do you need to support?):
http://caniuse.com/getelementsbyclassname

Categories