I have a link in my localhost, which points to a jpg image. I want to keep using that same link, but I want to eventually replace the image with a word file. Or a pdf file. Or a gif file. Or any other extension that isn't a jpg image.
(Trust me, it makes sense in context, but the context is not important to the question, so I'll leave it out.)
How can I create a link to those files without having the file extension at the end? I could create a php page and have the image (or another file type) embedded there, but one requirement that I have is that the url needs to be the same as the image url. So specifically for jpg, png and gif files only (don't worry about other file types), I want to be able to right click it when I view it with my browser, go to "Copy image address" and get the same exact link as in the address bar of my browser, as if I'm viewing a regular image.
Is something like that possible?
To reiterate:
- I need to generate a link that can be any file type
- If the link points to a file that is an image (jpg, png, gif), the link you get from "Copy image address" should be the same as the link you're using to view it in your browser
Example:
This is the image I have saved on my PC:
C:\xampp\htdocs\my_projects\my_image.jpg
I can view it directly in my browser using this link:
localhost/my_projects/my_image.jpg
What I really want, however, is something like this:
localhost/my_projects/my_image
So I could replace the original jpg file with a png file for example:
C:\xampp\htdocs\my_projects\my_image.png
And, after editing whatever file is responsible in generating "localhost/my_projects/my_image" to point to the png image instead of the jpg image, I can still use the same link as before and get the png image this time:
localhost/my_projects/my_image
What is important to me is the following:
Hopefully this will make my question more clear.
If you know the directory or location of the file.
You can fetch file name with it's extension. you don't need to give file name in front-end.
$directory = 'folder';
if($handle = opendir($directory.'/')){
echo 'looking inside \''.$directory.'\':<br>';
while($file = readdir($handle)){
if($file!='.'&&$file!='..'){
echo ''.$file.'<br>';
}
}
}
either list them or download them.
You need to Rewrite Rules.
Create a .htaccess file in your directory.
Then add this code inside your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.png-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.png
RewriteCond %{REQUEST_FILENAME}.jpg-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.jpg
RewriteCond %{REQUEST_FILENAME}.gif-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.gif
I hope this helps.
Related
I want to generate various image dimensions with PHP on the fly without saving all variations.
Now I upload an image and generate various dimensions and save all those files on the server. The path to the images is as follows:
/20151217/124043_205037_x.jpg
The first part is the folder
The second part is the file name.
The 3rd part is the _x, _s, _l flag (original,small,large etc)
I want to call the PHP script and return the image like:
/imageManipulationScript.php?folder=20151217&file=124043_205037&flag=x
So I want to make a mod_rewrite rule to call:
<img src="https://somecdn.com/20151217/124043_205037_x.jpg">
and server will run this:
/imageManipulationScript.php?folder=20151217&file=124043_205037&flag=x
I will call this img src through cdn so the image will be cached and executed only once per image/dimension.
I don't want to change the current path and structure of the images because then I'll have to change the implementation on my site
Any thoughts?
You might want to tweak the 3 regex's to fit your allowed characters but something along these lines should work.
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)_([A-Za-z0-9-]+).jpg$ imageManipulationScript.php?folder=$1&file=$2&flag=$3 [NC]
I've noticed in Facebook's source code, that images are links to a PHP file, safe_image.php (or rsrc.php; it changes every now and then), with the name of the selected file appended to the end, such as:
https://external-lax3-2.xx.fbcdn.net/safe_image.php?imagename1234
Or sometimes they're the usual JPEG files with a random token appended to the end:
https://scontent-lax3-2.xx.fbcdn.net/v/t1.0-9/17353408_410522555967800_2778489440067836960_n.jpg?oh=3e00f84c6767364c9304b34f8751114d&oe=5954DA1E
What, I'm wondering is how they get a custom image viewer on their website. Usually, it's just a white background, with the selected image in the top left corner. However, they have it set in the middle with a grey-ish background.
Not only that, the linked image is direct back to the viewed PHP file; how is this possible, and how do they do it?
Cheers.
EDIT: I've also noticed if you change the img src to an invalid link, it will print an error to the page:
The image " Insert image link here " cannot be displayed because it contains errors.
Jpeg files, to no ones surprise, do not take in arguments. However, PHP does. So, what is most likely Facebook did is use a rewrite rule to 'map' their .jpg?= URL to a PHP file, which can process the arguments. That PHP file then fetches the image data from a MYSQL (like) table. If you're wondering, yes you can have the .jpg file extension display in the URL, load data from PHP, and have the image display properly in browser.
This can be achieved via PHP and .htaccess.
Firstly, let's setup our .htaccess inside whatever folder we want to have our /img.jpg?= inside of:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^img.jpg(.*)$ imageBackground.php$1 [NC]
Yay, now we are mapping our img.jpg?image=bill to imageBackground.php?image=bill.
And then we are going to write our imageBackground.php. I wrote a very simple one, but basically all your doing is setting up headers (for which image format you're using) as well as display the image data. Obviously in practical application this would be more complicated, like maybe you're dynamically grabbing image data from a database (like Facebook).
<?php
if ($_GET['image'] == "bill") {
header("Content-type: image/pjpeg");
echo file_get_contents("bill.jpg");
}
?>
I need to fake/hide the real image location.
Using this;
RewriteRule ^/photos/(.+).jpg$ /images/$1 [R]
Will only change /photos/ to /images/
Is there anyway to use encoding (like base64) or something else to hide/fake real image location?
How can I save a php-built-png image as a png file on my server?
For example let's say my script image-builder-png.php generates a blank box png image with the parameters passed to it:
/image-builder-png.php?color=orange&width=100&height=100
If I were simply citing this image in HTML this would render fine:
<img src="/image-builder-png.php?color=orange&width=100&height=100">
But I need to pass this image to another server script that only accepts standard image files like orange-box.png.
So how can I "save" the rendered image from /image-builder-png.php?color=orange&width=100&height=100 as orange-box.png.
If serving on Apache, just use mod_rewrite to mask the PHP script parameters into a full path, and alter your script to figure out the dynamic parts.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/image-builder/(.*)$ /image-builder-png.php?path=$1 [L,QSA]
Now an example request of "/image-bulder/100x100/orange-box.png" will be mapped to "/image-builder-png.php?path=100x100/orange-box.png". A simple preamble at the top of your PHP script should be able to extract the information, and correct the dynamic options. Something like ...
if(isset($_GET['path']))
{
$parts = explode('/',$_GET['path']);
foreach($parts as $part)
{
if(preg_match('!^(?<width>\d+)x(?<height>\d+)$!',$part,$matches))
{
$_GET['width'] = $matches['width'];
$_GET['height'] = $matches['height'];
}
if(preg_match('!^(?<color>\w+)-box\.png$!',$part,$matches))
{
$_GET['color'] = $matches['color'];
}
}
}
I'm trying to dynamically rename a jpeg image file as it's displayed to the user.
I have a file image_generate.php with the following code:
$file = $_GET['file'];
$imagepath = "path/to/image.jpg";
$image = imagecreatefromjpeg($imagepath);
header('Content-Type: image/jpeg');
$filename = "[site.com]_some_image_name_here";
header('Content-Disposition: attachment; filename="' . $filename . '.jpg"');
imagejpeg($image, NULL, 100);
imagedestroy($image); // Free up memory
And it's called by an html image tag like so:
<img src="image_generate.php?file=imagenamehere" />
So far, the output has the following results:
When I right-click on the image, and click "View Image," a download dialogue pops up and asks if I'd like to save the file "[site.com]_some_image_name_here.jpg" (I want this to happen)
If I right-click on the image, and click "Save Image As," or "Save Image," the filename that's to be saved shows up as the original filename (whatever the variable $file was able to fetch).
How can I fix the second part? I'd like to modify the filename of the image even when the user clicks "Save Image As" or "Save Image."
I DID try to change the code to this:
...
$rename = $filename . ".jpg";
imagejpeg($image, $rename, 100);
imagedestroy($image); // Free up memory
But the image fails to show up on the page with the html tag (shows up as a broken image).
I'm not very familiar with html headers and Content-Dispositions. I'm guessing there's an error in there somewhere..?
Any ideas?
Thank you for reading!
Edit: .htaccess below
<IfModule mod_rewrite.c>
RewriteEngine On
# Sends image download request to image_generate.php for parsing
RewriteRule ^path/from/img/tag/downloads/(.*).jpg$ path/to/image_generate.php?file=$1 [NC,L]
RewriteRule ^path/to/image_generate.php$ /home/path/to/image_generate.php [NC,L]
</IfModule>
Different browsers will have different behaviors.
For example, Internet Explorer doesn't try to fetch any header information before displaying the save file dialog when doing a Right Click > Save Image As (It automatically assumes you want to save the result of the request, regardless of what the result is). Neither does Firefox. Therefore, for IE & Firefox, it will be impossible to specify the filename by specifying it in the headers.
A more elegant and UA-compatible way of doing this would be by using Apache's mod_rewrite to do URL rewriting. URL rewriting allows you to reroute a request made to a URL towards another.
To set this up, you need to create a .htaccess file in the directory where image_generate.php is located containing the following:
RewriteEngine On
RewriteRule ^generated/([a-z0-9-]+)\.jpg$ generated_image.php?file=$1 [L]
Then, simply modify your HTML to point to your rewritten URL.
<img src="generated/imagenamehere.jpg" />
This also have the advantage of making your image generation completely seamless to the user (URL wise, it looks simply like a static image).
More information about mod_rewrite can be found here:
mod_rewrite - Apache HTTP Server
mod_rewrite Cheat Sheet (V2) - Added Bytes by Dave Child