Jump to content

Help: Image as a variable


diablokicks

Recommended Posts

I am extremely new to php and trying to learn by doing.    I would like to have a php file that contains variables as images ex: $specialDeals = "images/p7.jpg";    and i would like to  be able to call that special deals in another php page.  I have googled for 2 hours and have had no luck. 

 

<img src="/sockspecials.php?id=specialDeals/" alt="" title="" border="0" /> <!--</a>-->

 

That is what i have in my other page, but i'm getting the red X. 

 

Any help would be appreciated. 

 

Thank you,

Patrick

Link to comment
https://forums.phpfreaks.com/topic/199812-help-image-as-a-variable/
Share on other sites

If you want to use a php file as an image, you need to use a header to change the content type, and grab the contents of the image

 

$file = '/path/to/image/image.jpg';

header('Content-type: image/jpeg'); //Tell the browser it's now an image
if(file_exists($file)) //if the file exists in our server
{
   echo file_get_contents($file); //Output the image
}

 

You can control the value of $file with $_GET and other things.

If you want to use a php file as an image, you need to use a header to change the content type, and grab the contents of the image

 

$file = '/path/to/image/image.jpg';

header('Content-type: image/jpeg'); //Tell the browser it's now an image
if(file_exists($file)) //if the file exists in our server
{
   echo file_get_contents($file); //Output the image
}

 

You can control the value of $file with $_GET and other things.

 

Can you use one PHP file for several images?  So having $image1=image2.jpg; $image2=image2.jpg; ... etc

 

What i am trying to do is have 1 php file that i can go to and change the image and have it apply to all of my other pages.

Whatever you specify $file as, that is the image that will appear. You can change the $file variable manually, or make it change via get variable.

 

So if you have 40 pages linking to image.php with that code, and you changed $file to a different image location, everyone of those pages would then point to the new file.

 

But if you're doing all this manually, why don't you just overwrite the .jpg file?

If you had two images say

 

/images/special_product.jpg

 

and

 

/images/product.jpg

 

 

And those were the only things you were going to change, and manually. All you'd need to do is replace those images with the new image using the same filename, and your pages would use the new image.

 

You only need to use php to generate images if you need something like the image to be random, changed at a certain time, or grabbed from a database. Or in extreme cases, have content generated onto them using GD

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.