Jump to content

error help - Dynamic Image


CK9

Recommended Posts

I've been working on making my site less cluttered in the directories and more secure lately.  In an attempt to prevent bandwidth theft from other people using images, I decided to switch all my site images to a dynamic image file.  I've written a small test code to iron out as many bugs as I can BEFORE I impliment it fully.

 

Code:

<?php
if ($_SESSION['i'] != "yes"}
  {
   $im = "theft";
  }
else
  {
   $im = $_GET['i'];
  }
switch($im)
  {
   case 'theft":
    $image = imageCreateFromJPG('/theft.jpg');
    break;
   case 'logo':
    $image = imageCreateFromJPG('/logo.jpg');
    break;
  }
imageJPG($image);
imageDestroy($image);
?>

 

When using it in the test copy of my site's index page (calling for the logo image) it works fine.

 

When I try opening the page (with the GET['i'] set to logo, theft, and not set at all), I get the following error:

Parse error: syntax error, unexpected T_IF in /home/ck9/public_html/media/images/site.php on line 3

 

I tried setting the current If...else as a long comment and re-writting it another way, but that didn't fix the issue.  I tried looking up the error in the PHP manual, but the information presented wasn't helpful to me.

 

Thanks in advance for any help you guys can provide (and the help here has always been great :) )

Link to comment
Share on other sites

Don't use the GD functions to simply read and output an image. That will take up a huge amount of unnecessary server resources.

 

You also need to output the correct content-type header before you output the image data.

 

To do what you are trying (use a php script to output your images that are stored in a location that will not accept direct http requests) all you need to do is output the correct content-type header, then read and output the actual image file. The readfile function is normally used to read and output a file.

Link to comment
Share on other sites

*facepalm* Why do I keep missing these small errors?

Thanks atomic, but that doesn't clear the error :(

 

(and before someone comes along and mentions it, I did use session_start, but I forgot to type it in my original post, heh)

 

PFM, not quite sure what you're saying.  Most of what I know of PHP coding I've learned through reading the tutorials and experimenting with the functions they give me...this readfile() wasn't in the ones I read.  Do you have a link that will explain it as well as showing an example of its use?

Link to comment
Share on other sites

From what I read, you can use that to:

  • force a download
  • tell how many lines there are and what the last one is

 

I don't see how that can be used to prevent other people from linking my site content to their site...Is there an example of it being used in this manner that I can look at and see if I can understand it better?

Link to comment
Share on other sites

Programming requires a great amount of inference ( http://en.wikipedia.org/wiki/Inference ).

 

Here is the code from the readfile() example, with only those things which I wrote above that are needed (content-type header and readfile() function) -

<?php
$file = 'your_file.jpg';

if (file_exists($file)) {
    header('Content-Type: application/octet-stream');
    readfile($file);
}
?>

 

Here is the content-type header that matches your file type from the imagecreatefromjpeg/imagejpeg example (what your existing code seems to be based on) in the php manual -

 

header('Content-Type: image/jpeg');

 

Inferred code -

<?php
$file = 'your_file.jpg';

if (file_exists($file)) {
    header('Content-Type: image/jpeg');
    readfile($file);
}
?>

 

You would of course need to add the logic to get the proper file name and path to where the files are actually located into the $file variable.

 

Link to comment
Share on other sites

I think I understand now.  Just to confirm:

 

1) it looks at what the file is

2) it makes sure the file is at "localhost"

3) if it is, it displays the file.

 

Is that the gist of it?

 

Does this go in the coding for the page that has the image on it, or another file like a dynamic image is?

 

Sorry for how difficult it is to explain this to me, because I've learned mostly through experimenting with the coding I don't always understand the terminology associated with it.

Link to comment
Share on other sites

I think I understand now.  Just to confirm:

 

1) it looks at what the file is

2) it makes sure the file is at "localhost"

3) if it is, it displays the file.

 

Is that the gist of it?

 

Does this go in the coding for the page that has the image on it, or another file like a dynamic image is?

 

Sorry for how difficult it is to explain this to me, because I've learned mostly through experimenting with the coding I don't always understand the terminology associated with it.

 

if ($_SERVER['HTTP_HOST'] == "mysite.com") {
  $image = readfile('./image.jpg');
  header('Content-type: image/jpeg');
  imagejpeg($image);
  imagedestroy($image); //free resources
} else {
  $image = readfile('./theft.jpg');
  header('Content-type: image/jpeg');
  imagejpeg($image);
  imagedestroy($image);
}

 

Like this? Very simple method, will only echo the actual image if the host is your own.

EDIT: Fixed code a bit

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.