Functions (usually) take inputs and return outputs, like this:

Code:
$out = foo($in);

The difference is that foo($barbar, $bazbaz) passes $barbar and $bazbaz in to the function. The function returns a value, which can be assigned to a variable (e.g. $result = foo($barbar, $bazbaz)). As the example function returns an array $result would be an array in this instance; however, using list() you can quickly extract values from an array:

Code:
list($scalar1, $scalar2) = foo($barbar, $bazbaz);


Code:
$result = foo($barbar, $bazbaz);
list($scalar1, $scalar2) = $result;


Code:
$result = foo($barbar, $bazbaz);
$scalar1 = $result[0];
$scalar2 = $result[1];


The above three code snippets are functionally equivalent.
That's what I was doing wrong, I was never defining a variable to save the function output to. The source for the pages is below.

thumbnails.php and functions.php

Results in, a return! Awesome. For now I'm going to use my full-size images that I have used on my portfolio, after it works I'll try it out on some larger and non-watermarked images.

It turns out I won't have as much time as I'd thought during this week, so I'll be throwing something together this weekend.
benryves wrote:
To save an image use the image<format>() functions, e.g. imagejpeg().


This is how I save a physical image to the directory? Everytime I do that, imagejpeg($img, thisisit.jpg) it returns an error that the image doesn't exist. I've shortened the function a bit (the image should be a string not as it is).

Also, How do I separate arrays? I have $iptc, which outputs, the following for Portrait-01:


Code:
array(2) { ["2#000"]=> array(1) { [0]=> string(2) "" } ["2#116"]=> array(1) { [0]=> string(14) "Alex Glanville" } } Array ( [2#000] => Array ( [0] =>  ) [2#116] => Array ( [0] => Alex Glanville ) )


http://comicidiot.com/thumbnails.php

If I want to grab my name from that variable, how'd I go about doing that?
imagejpeg works for me:

Code:
$img = imagecreatetruecolor(100, 100);
imagejpeg($img, 'img.jpg');
imagedestroy($img);

Make sure your directory permissions are set up so that PHP can write to the directory.

I don't know much about IPTC, but according to this page you're after the copyright code (2#116). You can retrieve a value from an array using its index, $iptc['2#116'].

It seems that the data is encoded as an array, so I guess you'd need to do something like this:

Code:
if (isset($iptc['2#116'])) { # Is there copyright information?
    # Join all elements of the copyright information together.
    # If there is more than one element they will have a space between them.
    $copyright = implode(' ', $iptc['2#116']);
    # If there is a copyright notice, display it.
    if (strlen($copyright) > 0) {
        echo '<p><strong>Copyright:</strong> ' . htmlspecialchars($copyright) . '</p>';
    }
}
It's been a while since I worked on this. Last time I worked on my galleries I did something with thumbnails and now my portrait gallery won't load at all. This happened after I started working on generating thumbnails on the server side. A bit of digging didn't reveal that anything had changed with the structure of my files. Which, was odd. The page as it is creates a folder called thumb in "res/img/portfolio/full/portrait/" and an image titled "img.jpg" at the base level. Neither of those showed.

Directory refresh after refresh. Disconnect and Reconnect after each other. Application restarter after restart. Then, several days later. Still nothing. So, I started making changes in my script that would error on purpose. My code didn't error. I ensured I wasn't placing the code in a statement that was being ignored due to conditions. Still no error. Then it dawned on me.

I was editing and viewing my locally cached web site. I publish the changes, and I get errors. I switch over to Server view and I locate both the missing directory and the image I generated. My portfolio images stopped displaying because of the new directory in the portfolio. The script saw the directory, "recognized" it as a sub-portfolio and tried to locate a mirrored directory in "res/img/portfolio/thumb/portrait/," which it didn't find.

The script is set up so that if there's a directory in the portfolio it will only display the images in sub-portfolios, whether that "sub-portfolio" has a counterpart or not. So, I'm going to try coding in a check first, so if there's no matching counter directory the images in the "portfolio" (not the sub) are displayed, and the "sub-portfolio" is ignored. Once the sub-portfolio has a counterpart directory, will the portfolio images be ignored and the sub-portfolio images be displayed.

Hopefully I'll be able to do this without any aide, I'll post my success or cries of help later on Smile
I had some issues with the code recently, I forget what the first error was. The last error was unmatched criteria. I have a square photo and it didn't fit in the conditions I set up.


Code:
if($height>$width)  echo '<a href="?portfolio=' . $URLpath . '&file=' . htmlspecialchars($f) . '"><img src="' . $loc . $f . '" class ="t" /></a>';

if($height<$width)  echo '<a href="?portfolio=' . $URLpath . '&file=' . htmlspecialchars($f) . '"><img src="' . $loc . $f . '" class ="w" /></a>';


A little work and confusion later (because it wasn't immediately evident) I added an $height == $width conditional. All is good <3

I'll make a new topic in a few days about creating a quick user system with MySQL. Going to research into and see what I'll need to get started.
Haha, that's a silly error. Smile Glad to hear you'll be adding a user system, and I look forward to (us|me) helping you out with that.
KermMartian wrote:
Haha, that's a silly error. Smile Glad to hear you'll be adding a user system, and I look forward to (us|me) helping you out with that.


Indeed. For now it'll be a method for me to refresh the portfolios and for setting variables without jumping into Coda, my FTP program to update variables. I'd set the directories for source and destination images, as well as the image/text for watermarking. I'll also likely add support for users with less access for practice with restrictions, sessions and registration. Then, later down the road I should be able to code the client features with out modifying too much or working the restrictions in at that point in time.

Basically, I know I'll need to understand Sessions and Restrictions. I'm going to play with logged in and logged out states of my website mock, and see if it's something I'd want to display.
The script works wonderfully with my EyeFi card! I'm amazed. Can't wait to start work on a customizable version for my photo portfolios. In the mean time, I've started separating the Sub-Portfolio version of the script into functions. It's a bit difficult but I'm getting there. I'm actually progressing nicely and it's working so far. I've been encountering bugs that are my fault but I'm not sure I'm escaping them correctly.

Quote:
Notice: Undefined variable: gallery_files in /home/comicidi/public_html/res/inc/func_portfolio.php on line 73


As it is, I get the above error on this page. I know why It's happening, I have return $gallery_files; at the end. The error is happening because $gallery_files wasn't created, but I'm returning it anyways. I escaped a similar error by typing the following:


Code:
   if($dcount != 0) {natcasesort($gallery_dirs);
   return $gallery_dirs; } else { return; }


$dcount gets incremented if there are directories within a portfolio, called Sub Portfolios. So, if it isn't 0, $gallery_dirs must exist. If it is, then just return. I call this function like so:


Code:
   # Fetch the Sub Portfolios, if applicable.
   $gallery_dirs = sportlist($dir, $tpath);


Is this alright? Should I instead exclude the if( blah ) { from the function and make it part of the index, so I don't need to hackishly escape the undefined variable?

How would I return multiple variables from a function?
There are a couple of ways you could do that. Instead of just saying return, you could return a blank array (would probably be my first choice. It scares me to see a function that sometimes returns something and sometimes doesn't.) You could also, if you wanted to return multiple values, return a hash of values. (~lucky php hash). Out of curiosity, why are you changing everything into functions? Is the current source too hard to manage, or would it be useful elsewhere, like in another script, or just thinking for the future or practicing? If it all works as it is now, and none of the other things are happening, there shouldn't be a need to change everything Smile Though that might not be The Right Answer Razz
I assume your code currently does something like this (grossly simplified):

Code:
function foo() {
    if ($bar) {
        $gallery_files = array();
        $gallery_files[] = 'baz';
        $gallery_files[] = 'bat';
    }
    return $gallery_files;
}

i.e. it only assigns something to $gallery_files if some other conditions are met. I usually work around this issue by explicitly setting any variables to sensible default values first, to ensure that something sensible is always returned:

Code:
function foo() {
    $gallery_files = array();
    if ($bar) {
        $gallery_files[] = 'baz';
        $gallery_files[] = 'bat';
    }
    return $gallery_files;
}
Oddly enough. I'm not. All the arrays I need are created on the index page, then added onto in the function. I'm not sure why it's returning an undefined variable when that's the case but I assume I can't leave an empty array be a variable. The start of my functions look fairly cut & copy.


Code:
# Store all filenames in an array.
$gallery_files = array();
$gallery_dirs = array();
$main_gallery = array();



Code:
function portlist($mlist) {

   $dcount = 0;

   # Since we need a list of every portfolio, let's make that now!
   if ($handle = opendir($mlist)) {
      while (false !== ($file = readdir($handle))) { 
              
            if ($file != "." && $file != "..") {
             if (is_dir($mlist . $file)==TRUE) { $dcount++; $main_gallery[$dcount] = $file; }
         }
      }
   }
   
   return $main_gallery;
}


_player1537 wrote:
Out of curiosity, why are you changing everything into functions? Is the current source too hard to manage, or would it be useful elsewhere, like in another script, or just thinking for the future or practicing?


I appreciate the question. In my two column design that I'm likely going to go for in v3 of my website (linked above), I'll be moving the Portfolio name and sub portfolio to the left hand side, instead of above. It's poorly styled in as of now. Also, I'm moving the next, home and previous buttons to the left column.

I distinguish the left column in header file so I need to basically run the script from the header then print out down below. I figured I'd save some work (since it seemed like more work) and just render the next and previous buttons on it's own then everything else in the body. But as I type this I realize I'll have to run - pretty much - the whole script to get the next and previous buttons. So, it'd actually make sense to do all that first the output everything where needed.

I'm also playing with functions for growth of knowledge. But with the recent epiphany I think I'll worry about functions later.
Variable scoping doesn't work like that in PHP - all variables are implicitly local inside functions, so $main_gallery doesn't exist inside the portlist function. If you intended portlist to append to $main_gallery then you would need to add global $main_gallery; to the top of the function; if you intended it to populate $main_gallery in its entirety it would probably be sensible to just stick $main_gallery = array(); at the top.
Good to know!

I just took the time to comment the entire PHP script for the single gallery (not the later script tailored for my portfolios). I managed to view my source and it's all pretty neat except for one section. Is there anyway to start a new line for the php output so the source is much easily read, as you'll see in div#body. The header and footer are almost strictly structured with HTML, which the PHP seems to ignore in the main body.


Code:
<div id="header">
         <a href="/" id="site-logo">Alex Glanville :&nbsp;Photography</a>
         
         <br>
         
         <div id="menu">
            <a id="fmi" href="/">Portfolio</a>
            <a id="mmi" href="about.php">About</a>
            <a id="lmi" href="/?portfolio=contact">Contact</a>
         </div>
      </div>
      
      <div id="body"><div id="body"><h2>EyeFi Card Upload Gallery</h2><div class="gallery"><p> There are 5 photos in this upload.<br><div class="ic"><a class="photon">1</a><a href="?file=IMG_7512.JPG"><img src="upload/IMG_7512.JPG" class ="tall" /></a></div><div class="ic"><a class="photon">2</a><a href="?file=IMG_7516.JPG"><img src="upload/IMG_7516.JPG" class ="tall" /></a></div><div class="ic"><a class="photon">3</a><a href="?file=IMG_7518.JPG"><img src="upload/IMG_7518.JPG" class ="wide" /></a></div><br><div class="ic"><a class="photon">4</a><a href="?file=IMG_7519.JPG"><img src="upload/IMG_7519.JPG" class ="wide" /></a></div><div class="ic"><a class="photon">5</a><a href="?file=IMG_7520.JPG"><img src="upload/IMG_7520.JPG" class ="wide" /></a></div></div><br><br></div>      </div> <!-- <div#body> -->
      <div id="footer">
         <a>Content <a class="history" href="/copyright.php">© Alex Glanville</a><br>
         <a class="history" href="/history/">v3</a> Design by Alex Glanville</a><br>
         <a>Login</a>
      </div>
Try something like:


Code:
echo "<span>My line of HTML goes here</span>\n";
print("<div>Or do it this way instead\n");


Backslash n is the newline character in many things.
That's what I thought, it looks like the double quotes are mandatory. It wasn't working with single quotes and when I did a new echo, echo '\n'; I got a UNICODE error on a different line. I had the echo on 170, but the unicode error would appear on 105, but only with that echo present. It was odd.

I've added in \n within double quotes in certain cases and it works wonderfully! Thanks.

My code is no wonderfully structured when viewing the source! Makes my life easier. Just need to do the same for the portfolios now. Also, added almost 100 lines of comments Surprised

I'm not the best at remembering things so I left plenty of notes, also credits and the URL to this topic. I really appreciate the continued help!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 5 of 5
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement