Jump to content

Can You Do This With An Image In Php?


TKW22
 Share

Recommended Posts

im trying to rewrite an scr of an image to something like this

<img src="http://somedomain.com/images/?image=1" />

in sted of this

<img src="http://somedomain.com/images/1.png" />

so i can make it so some people cant see it in tell they pay with points.if this is possible thanks.

Link to comment
Share on other sites

If you have a PHP script in the images directory to get those requests and get the correct image then yes, it's possible.
if possiable could you show me what would the code i have to use look like?
Link to comment
Share on other sites

It would look much the same as other code that uses $_GET. After you get the image filename or whatever is being sent, you can either just redirect to the image, if you don't care if the user sees the URL of it, or you can output the image directly. If you want to redirect you send a location header:header('Location: ' . $_GET['image'] . '.png');exit();If you want to output the image, you need to send headers that set the correct mime type and then send the image data. Assuming all of the images are PNG images, it might look like this:

$fname = $_GET['image'] . '.png';header('Content-type: image/png');header('Content-length: ' . filesize($fname));echo file_get_contents($fname);exit();

Link to comment
Share on other sites

It would look much the same as other code that uses $_GET. After you get the image filename or whatever is being sent, you can either just redirect to the image, if you don't care if the user sees the URL of it, or you can output the image directly. If you want to redirect you send a location header:header('Location: ' . $_GET['image'] . '.png');exit();If you want to output the image, you need to send headers that set the correct mime type and then send the image data. Assuming all of the images are PNG images, it might look like this:
$fname = $_GET['image'] . '.png';header('Content-type: image/png');header('Content-length: ' . filesize($fname));echo file_get_contents($fname);exit();

k thanks but how can i get a number for each image would i have to use an array? say an image has its normal url of this1.pnghow could i switch it to this?image=23
Link to comment
Share on other sites

I don't understand what your question is. If you're sending the filename in the URL, then you don't really need to do anything special. If you're not sending the filename, if you're just sending a number that maps to an arbitrary file, then you need to keep a list of which numbers map to which filenames. An array is one way to do that.

Link to comment
Share on other sites

I don't understand what your question is. If you're sending the filename in the URL, then you don't really need to do anything special. If you're not sending the filename, if you're just sending a number that maps to an arbitrary file, then you need to keep a list of which numbers map to which filenames. An array is one way to do that.
yea that what im trying to do so the real url isn't showing.sorry for being unclear.
Link to comment
Share on other sites

If you have list list of files, this is how you would select one from it:

$files = array("image_1.png","a_Random-image.png","1000.png");$first_image = $files[0]; //the value of $first_image is now "image_1.png"

therefore, if you have the array of files you would want something like this:

$files = array("image_1.png","a_Random-image.png","1000.png");if(!isset($_GET['image'])){  //incase no number is provided	echo "No image Selected."; }elseif($_GET['image'] > (count($files) - 1)) { //so that you dont get a number that is not in the array	echo "Invalid Image."; }else{	   $fnumber = $_GET['image'];	   $fname = $files[$fnumber];	   header('Content-type: image/png');	   header('Content-length: ' . filesize($fname));	   echo file_get_contents($fname);	   exit();}

that should work assuming I typed that right (it might not be the best way but i believe it will work)Of course, that means that all of the images are mapped to the number that corresponds to the images location in that array, so you will not be able to map any arbitrary number to the file name without some extra coding.

Link to comment
Share on other sites

$files = array("image_1.png","a_Random-image.png","1000.png");

Remember that the first image in the array will be indexed as array element "0", so adjust for that.

$files = array(1 =>"image_1.png","a_Random-image.png","1000.png");

http://ca2.php.net/types.array

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...