You really should have a script that generates static thumbnails so you don't have to do it on the fly, if you don't already, in my opinion. Let me know if you (a) decided to do so and (b) are confused; I'd be happy to help.
KermMartian wrote:
You really should have a script that generates static thumbnails so you don't have to do it on the fly, if you don't already, in my opinion. Let me know if you (a) decided to do so and (b) are confused; I'd be happy to help.
^this

If you can do it on the fly it should be easy enough to just make it save the output in a "thumbs" directory and have the function callable from an Admin control panel of sorts, or at least something that is password protected to avoid the possibility of someone spamming it to DoS your host.
My concern with that is that I'm not sure how often I'll need to do perform that task. Once every few months I add a few photos to the portfolios as a whole. It's a matter of saving it as two different sizes then uploading those images to their respective folders. A reason why I haven't opted for it yet, is that I prefer my name on the image when viewing but not in the thumbnail.

When I did have it generating the thumbnail from the full-size images, there was a white line where my name was. Lastly, I'm not quite ready to upload my photos without my name on them. I know I could upload them then overlay a watermark, but then that would expose the non-watermarked images.

Putting two and two together, I *could* upload the images to a directory and have the script create both a thumbnail and a watermarked version, saving both in their respective directories. Sure, let's go about this! It sounds fun and fitting.

To name off some things I'd like to have in it:

  • Fetch an image, create a thumbnail and a full-view image. If the starting image is larger than the full-view, resize before continuing with watermark.
  • Able to watermark the image with text or a second image.
  • Save the resulting images to their appropriate directories.
  • Keep track of images created and those that need to be processed.


I'm assuming that's all the script would have to do. Now, as TheStorm pointed out, I'd need an ACP or some sort of security to prevent abuse. This seems like the perfect opportunity to jump into MySQL as well, but I', going to shy away unless it's heavily recommended here. I'd like to stick with flat files, just for the ease of manual editing. I've been trying to get OpenOffice to connect to one of my test MySQL DB's without success for the last few years (albeit my attempts are few and far between).

Some things the ACP would take care of are:
  • Display number of images watermarked
  • Display number of new images to be processed.
  • Watermark adjustment (image upload/locate/URL, or text input).
  • Directory locate (source, destinations)


As far as "Watemark adjustment" I assume I could use the text box for both URL and text input? Just have a check for "http://www." and if it's not found then use it as text instead of an image? If it is an image, save it to a directory and source it from my web server instead of from an external host?

Judging from the above, what kind of functions am I looking at?

Also, by way of testing, are my variables sanitized? I don't think they are by typing in "../" enough times.
I've been looking at the functions I'd need, these are ones I've pulled out of PHP.net. I'm planning to create a function include file starting with this script. I haven't broken down the last script into custom functions, but I'd like to start working with them.



I can't find a way to save an image once it'd been setup with the watermark. I've found iptcprase which would be nice, since I embed copyright IPTC data into the images, so I could hopefully read the copyright name from the IPTC and apply it to the image as text with imagefttext(). I've come across some other functions which could be useful:



I can likely use exif_thumbnail to display thumbnails, without saving them. I might even be able to watermark the full-size and display a thumbnail without the watermark, but I'm not positive and just being hopeful since a thumbnail would be a smaller copy of the whole image as saved I assume.

I haven't looked into what I'd need for some sort of ACP. Is it possible to build the script then build an ACP that plugs into this later with minimal implementation, or would I need to code ACP checks/authority in as I go along? Such as a define('IN_THIS', true);? Much like phpBB.
To save an image use the image<format>() functions, e.g. imagejpeg().
Ah, good to know!
I'm trying to get my custom functions to work, and they work as long as everything is kept local (inside each php file, and not defined outside of the include). I thought it'd be as easy as defining a variable like what follows below, but it wasn't the case.


Code:
$d = dir('this/and/here');
list_images()


Got an error, just nothing else returned. So I looked at some examples on php.net and decided to change the following.


Code:
list_images($d); # In the main php file
function list_images($d) # In the function php file


Ended up getting an error, "Warning: Missing argument 1 for list_images(), called in /home/comicidi/public_html/thumbnails.php on line 17 and defined in /home/comicidi/public_html/res/inc/functions.php on line 18". Below is my source for the setup and the function.


Code:
$d = dir('res/img/portfolio/full/portrait/');

list_images($d); #line 17
print_r($gallery_files);



Code:
function list_images($d) { #Start function definition, line 18

   while (false !== ($f = $d->read())) {
       if (preg_match('/\.(jpg|jpeg|png|gif)$/i', $f)) {     
           $gallery_files[] = $f;
         }
   }

} #End function definition


When I bypass the error by leaving $d out of the function listi_mages($d), and define $d inside, I get an error with the function read().
PHP's variable scoping is a bit weird. A variable defined in a file (not in a class/function or similar) is effectively global, but to access it within a function you need to use global:


Code:
$d = dir('this/and/here');
list_images();

function list_images() {
    global $d;
    # do something with $d
}


This is not particularly good design, however (relying on global variables with particular names to exist before calling functions can soon get rather messy) and explicitly passing the directory each time is a bit cleaner. Your code looks right and I can't see how that would trigger the error so please double-check that the code you're running is what you think it is (you haven't forgotten to upload it or similar). I would probably also include a call to $d->rewind() at the start of the function so you can call the function multiple times with the same $d.

To avoid having to pass $d to lots of different functions you might want to start looking into putting together a gallery class.
comicIDIOT wrote:
My concern with that is that I'm not sure how often I'll need to do perform that task. Once every few months I add a few photos to the portfolios as a whole. It's a matter of saving it as two different sizes then uploading those images to their respective folders. A reason why I haven't opted for it yet, is that I prefer my name on the image when viewing but not in the thumbnail.
But why resize them and apply your personal watermark before uploading when you can just upload the large images and have the script thumbnail all the images and watermark the large images and thumbnails? Smile

As Benryves says, your problem is with variable scoping: unlike most languages such as C, where variables outside any function are considered global and are visible in every function, you must explicitly declare the variables you want to share between the body of the script and functions as global. I don't mind using $d as a parameter for a few functions, but again as Benryves says, if it starts to get too unwieldy with several variables that you need to pass to every of a set of functions, you're better off switching to a class.
What if I declare arguments of the function as $a and send $d?


Code:
list_images($d); # In the main php file
function list_images($a) # In the function php file


Not sure how'd I'd return the value just yet. I'll look into variable scoping and gallery classes.
You use the name of the variable as declared in the function declaration, so inside the list_images function you use $a.
Indeed, would that bypass the global requirement? I'm certain I'd need to make it global to return $a, though.
No, just return $a;
You wouldn't need to make it global since you aren't accessing the same variable across many functions, you are simply returning it. Then, you could do someting like $var=list_images($d) to store the returned value into the variable $var.
I didn't get much work done on this today, but I'll have some more time around my shift at work in the coming days. Benryves, I tried to bypass the global var requirement initially by making $d variable of the function above (duplicated below). But it didn't seem to work. I'm assuming it's bad practice to send the same variable that it intends to "overwrite" with basically the same value of the variable? If I had list_images($a) for the first line, would the it be alright?


Code:
list_images($d); # In the main php file
function list_images($d) # In the function php file


souvik1997 wrote:
You wouldn't need to make it global since you aren't accessing the same variable across many functions, you are simply returning it. Then, you could do someting like $var=list_images($d) to store the returned value into the variable $var.


If I had multiple variables, such as list_images($d,$e,$f) and did $var=list_images($d,$e,$f), would that return an array?
I think it would be clearer if you posted your code. Smile

Functions don't implicitly return anything, so if you wanted a function to return an array, then return an array:

Code:
function foo($bar, $baz) {
    return array($bar + 100, $baz + 200); # Or whatever the function is supposed to do.
}
To expand on that example, if you're trying to return, say, two values, you can even do this:


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

function foo($bar, $baz) {
    return array($bar + 100, $baz + 200); # Or whatever the function is supposed to do.
}
Very handy. When I step in front of my desktop today I'll upload the source of the two files to my desktop after making adjustments from the posts above.

Wouldn't I need $var=array(...);? I also don't understand the first line. Is that putting variables into a global "setting?"
You don't need to assign the return value to a variable, though you could do


Code:
$var = foo($barbar, $bazbaz);
# Use $var[0] and $var[1] as the two return values


if you'd prefer. list() is a bit of an oddity - it's not a function, but a language construct. It allows you to pull values from an array into individual variables without having to copy them one by one - see the documentation for list for more examples.
Oh! I see how list() works now. Another factor to my confusion was that I thought foo() was an array. Ha.

How does list work in conjunction with the function? How's it different than foo($scarlar1, $scarlat2)?
  
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 4 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