LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2010, 03:23 PM   #16
fast-reflexes
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 36

Original Poster
Rep: Reputation: 16

Ahhhh! Yeah, that's it Xyro! And that was the solution I had in the beginning but I realize now that's the only way it can be done. Because the file from where it comes must be clean from everything that requires anything else than an image header, therefore, the only way to place it is in an outer .php script which is called the way stated earlier.

And yes, I was curious, it has been working the whole time for me but I wanted to know WHY the other thing didn't worked and if the solution I had was a proper one.. apparently it was I learnt a lot though and thanks a lot for helping me out! I guess I was unclear about the bit about the thumbnails being generated on-the-fly. It's so funny, because when you're deep inside your own thoughts, it's getting more and more evident that they are generated like this. So evident that once you try to explain it to someone, you just ASSUME that they will understand this highly circumstantial fact which they most often don't

Regaridng the require_once();, I think I'm missing something. Does it work like the following; when a file is included / required, it is in reality copy-pasted into the HTML source everytime a function shows up, whereas if you use inc./require_once, it's only copy-pasted once and therefore, if you use the same function multiple times, the parser doesn't have to copy-paste the same functions over and over again to the html source... that correct?

Thanks for the links as well! I had one of them but not the other... As soon as I grip this thing about require_once, I'll write a wrap-up to this thread and mark it solved.

Thanks everyone!
 
Old 08-25-2010, 07:58 PM   #17
Xyro
LQ Newbie
 
Registered: Aug 2009
Location: Canada
Distribution: Ubuntu 9.04
Posts: 22

Rep: Reputation: 19
Good to hear

Regarding the require/require_once/include/include_once functions:

They are all basically the same. All that occurs is the code "include('/path/to/file')" is replaced with the contents of the file at '/path/to/file' before the script is executed (note that /path/to/file/ can theoretically point to anything, but something with PHP or HTML is more likely to be useful). Same goes for require(), require_once() and include_once(). The differences are that the 'require's will kill the script execution if it cannot find the file and the 'include's will continue on oblivious to the fact that the include() actually did not happen (that is a bit of a lie, since error checking will indicate it). So for pages where you are adding essential functions to your script via include/require, require() can be better, especially for debugging. The '_once' variants just ensure that the same code isn't included (or require'd) more than once in the same script.
  • require() is good for essential code that you KNOW will be there (and if it is not, everything should die instantly)
  • include() is good for optional code (or that you can handle gracefully if missing) which may or may not exist (non-local includes?).

The *_once() variants just stop the inclusion of the same file multiple times in the same script.

Example:

inc1.php:
Code:
<?php
  function someFunc()
  {
    echo "<p>I am not that useful!</p>\n";
  }
?>
inc2.php:
Code:
<?php
  include('./inc1.php');
  function anotherFunc()
  {
    echo "<!--Even more useless!-->\n";
    someFunc();
  }
?>
index.php:
Code:
<?php
  include('./inc1.php');
  include('./inc2.php');
  include('./i_do_not_exist.php');
?>
<html>
  <head><title>Example</title><head>
  <body>
<php?
  anotherFunc();
?>
  </body>
</html>
When this script was executed, the actual script that is executed by the PHP engine would look similar to:
index.php
Code:
/* include('./inc1.php'); turns into: */
<?php
  function someFunc()
  {
    echo "<p>I am not that useful!</p>\n";
  }
?>
/* include('./inc2.php'); turns into: */
<?php
/*****************************************/
  /* include('./inc1.php'); turns into: */
  function someFunc()
  {
    echo "<p>I am not that useful!</p>\n";
  }
/*****************************************/
  function anotherFunc()
  {
    echo "<!--Even more useless!-->\n";
    someFunc();
  }
?>
<html>
  <head><title>Example</title><head>
  <body>
<php?
  anotherFunc();
?>
  </body>
</html>
minus those comments I added for clarity.

So you see, since inc2.php include()s inc1.php, and index.php include()s both, we have 2 replicate copies of the contents of inc1.php.
If we had used include_once(), the second instance of:
Code:
include_once('inc1.php');
would not be executed and we would only have one instance of someFunc() in your final PHP code.

My example is a bit silly, but when you start including many scripts that each include() the same basic script, then this problem will crop up. If you've done any C coding, this is why at the top of your header files you put:
Code:
#ifndef _SOME_HEADER_H
#define _SOME_HEADER_H

....
<code>
....

#endif
That way the C pre-processor only includes that header's contents the first time it comes across it. Any time after that (in the same compile process) _SOME_HEADER_H will have already been defined and it will skip over the contents, thus no redundant code is included.

Hope I haven't confused!

Also, if any of the php guru's notice something off in my advice/explanation, please do tell--I am by no means an expert!

Last edited by Xyro; 08-25-2010 at 08:08 PM. Reason: Forgot important part!
 
1 members found this post helpful.
Old 08-26-2010, 10:56 AM   #18
fast-reflexes
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 36

Original Poster
Rep: Reputation: 16
Great explanation Xyro, that was the way I had understood it from looking in the manual pages, only you showed me an example of a typical scenario which made me understand better, thanks again!
 
Old 09-09-2010, 06:05 PM   #19
fast-reflexes
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 36

Original Poster
Rep: Reputation: 16
Ok... here comes the wrap-up and summary.

Goal: To have a page show thumbnails of the images stored in a folder. Number of images is likely to change but they will always have the same format and resolution.

Means: To have a base HTML page calling a PHP script that counts the images in a specific folder and then returns the paths to these to another function that renders thumbnails of them on-the-fly. When you press a thumbnail, the real existing full-scale image is supposed to be shown to the right of the thumbnails.

Ideas: Constructing a script that counts the images in a folder was fairly easy. I used the following loop:

Code:
    
foreach(glob('images/'.$cat.'/*') as $img)
{
    $imgArray[]=$img;
}
When calling the scipt I sent a variable which completed the path as above so that the script knows which images to take.

Once $imgArray had all the images, it was time to convert them to thumbnails without storing them anywhere. This is a script I call thumbnail.php:

Code:
<?php	
	$src=$_GET["img"];
	$source_image = imagecreatefromjpeg($src);
	$virtual_image = imagecreatetruecolor(50,50);
	imagecopyresampled($virtual_image,     $source_image,0,0,0,0,50,50,260,260);
	imagejpeg($virtual_image);
	imagedestroy($virtual_image);
	imagedestroy($source_image);
?>
It works in a little strange manner though. It's part of the PHP GD extension and using it like a separate script and opening it in a browser generates a white page with an image and no HTML code (of course). I guess I think it's strange because it can't act in a context with other code, you don't SEND the image anywhere and tell someone what to do with it. As soon as you use the function 'imagejpeg' the script outputs a representation of an image. Therefore, echoing this script, returning 'imagejpeg' from this script or just executing this script should generally generate the same code since it ALWAYS outputs the image. Trying to output several images at once using the above script several times in the same file makes the browser just generate the first one. I guess I would have expected it to either show all three of them in a fast changing order or show them side by side but it's like wherever you use it, it just stops at the first imagejpeg. I must sound dumb but there's something I don't reall grip with this fairly easy to use script.... (Note: Using it separately in a browser requires including an image header at the top.)

My initial idea was to include this as a function in the script counting the images.

But how to include the image?

FIRST THOUGHT: The HTML written expects a source for the image. And the source should be a valid path which including this as a function could never make. Having the getThumbs('image') was hard to know where to put and my logical thought was to just put it inside of two <TD>-tags since including it in an IMG-tag would be illogical (what I supply is really not a path to an image but the image itself). Also, would it be correct to just put the function itself there or should I put 'echo' or 'return' in front of it? I mixed around a bit and the most common result was with a lot of strange letters inside the TD-tags at the place of the image and it all started with some familiar words containing 'JPEG Creator'. Apparently it was the image being processed in clear text. I tried to fix this by changing the headers for image (so that the browser would know to output it as an image) however this resulted in an error about "Headers already sent". During one output, for example when you send a HTML-file to a client, you can't change headers in the middle of output and that was what I was trying to do. From the moment my server sent out the first '<' in the opening <HTML> tag, that output was destined to be of type text/html and not image/jpeg which made it impossible to make the browser understand what I was trying to do.

SECOND THOUGHT: At first I thought that the above about output meant the PHP file's output to the HTML file which made me try a different approach. The approach involved the HTML page first being written with new code including info on the number of pictures it found, but ALSO new PHP code about how to get the thumbnails. My intention was that the web server would somehow feel that there were new PHP to execute and then execute that, now in a different output to the HTML file and thus rendering the images with the proper headers as it should.

Two mistakes though, first of all, the server literally thinks it's gonna send out the newly written PHP code so it doesn't execute that (like Xyro said) and second of all, I misunderstood the thing about output where output it actually the entire output from webserver to browser.

THIRD APPROACH: I constructed the function as a separate script that took a variable in the URL. This is the above posted script and it tells the script the path to the image it's supposed to make a thumbnail out of. I didn't like this method at first because it forced me to have one more file in my scripts folder (not that bad after all) and it also forced me to show that I have a script named thumbnail.php (I was being paranoid and not familiar with PHP, now I realize it's not that dangerous a script and also that PHP isn't THAT vulnerous).

However, when thinking about this, I realize it's the only way to do it.

FACT: Output has to be separate to the browser so it can render it as an image, therefore it cannot be included in the stream with the rest of the code and therefore it has to be a separate file and it has to take a variable as input.

CONCLUSION: I'm happy with the way of doing this now. Also, after applying some Javascript, it's now working super good. Only remaining question mark is about exactly how the 'imagejpeg' function works but I guess I can learn about that in the future Thanks all of you!
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yesterday PHP worked just fine and today it wants to serve up the php code. orsty9001 Linux - Server 10 12-12-2008 10:48 AM
php page displaying text that is supposed to be part of php code DragonM15 Programming 9 07-31-2008 04:58 PM
Postfixadmin PHP setup file - only displays PHP code davidmbecker Linux - Software 3 04-17-2008 10:33 AM
php mysql db code question radiodee1 Linux - Server 5 12-12-2006 11:15 AM
PHP code question latino Programming 2 08-26-2004 04:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration