NextGEN Gallery Thumbnail Generation Error

Thumbnail Generation PHP Memory Limit NextGEN Gallery

If you are having trouble generating thumbnails with NextGEN Gallery, this could be down to your web hosts default memory usage allocation for PHP applications.

Increasing the memory usage for PHP Applications

You can increase the memory usage allocation for an individual PHP applications, such as NextGEN gallery, which require more memory usage than other applications. You can do this by using @ini_set:

@ini_set('memory_limit', 'x'); where x is the memory.

In the case of NextGEN Gallery:

  1. Open /wp-content/plugins/nextgen-gallery/lib/gd.thumbnail.inc.php with a text or code editor.
  2. Search for the text @ini_set(‘memory_limit’, ‘128M’); which should be commented out.
  3. Remove the // from in front of the @ini_set(‘memory_limit’, ‘128M’);
  4. Save the changes and upload the changed file to your server (replace the gd.thumbnail.inc.php file in /wp-content/plugins/nextgen-gallery/lib/ with this new saved file)
  5. Go back to your WordPress admin site and in NextGEN select all the images that do not have thumbnails and recreate them.

You should now see that the thumbnails have been generated. 128M is usually enough in most cases from our experience, but if you find that thumbnails are still not being created then try increasing the memory limit to  @ini_set(‘memory_limit’, ‘256M’);

Remember – if you update NextGEN Galley automatically from within WordPress you will have to repeat this process.

Database Connections Outside of Webroot

For extra security many often store their database connection credentials outside of the web root. This is fairly easy to achieve without to much work.

For those of you who are unsure how to store database connection credentials in a separate file please refer to my article on Connecting to MySQL with PHP

Once you have your connections in a php file (this is also relevant to other languages such as asp etc) you need to store it somewhere on your server outside of the web root. In this example we will call the file MyCat.php.

Linux Systems

Put this file outside of the web root eg in /home/myuser/ the web root for example (where all your web files are stored) may well be /home/myuser/htdocs/

Now to include this file into your script use the following:-

include("/home/myuser/MyCat.php");

This will call the file from outside of the webroot.

Windows Systems

This is very similar to above. Place the MyCat.php file in the root of the C drive in a directory called MyInclude eh C:MyInclude

Now to include this file into your script use the following:-

include("C:MyIncludeMyCat.php");

Getting The Last Auto Increment Field With PHP

On many occasions you need to find the value of the last auto increment field inserted into a MySQL table. For example within my Content Management System if someone creates a new web page and saves it the user is taken back to edit this new page. In order to do this I must retrieve the value of the last auto increment field inserted into the database.

To do this I use the php mysql_insert_id(); function.

You can add this value to a variable by using the following code:-

$MyValue = mysql_insert_id();

So with this variable you could select an item from a database eg:

$MyQuery = "SELECT * FROM MyTable WHERE UniqueField = $MyValue";

Of course you should always check that $MyValue is numeric for security so we don’t break the query. For this we could use the php intval function.

intval($MyValue);

Detecting Mobile Devices with PHP

I have been using a great function written by Andy Moore on one of my personal sites that I run for my friends to detect Mobile Phones with php which worked a treat until a friend of mine bought himself a nice shiny iTouch and found the site still redirected him to the sites dedicated mobile pages. After the jealously of finding out my friend had a nice iTouch subsided I set to work to implement the detect iPhone function.

I used the following in my header file to detect mobile and iPod devices:

// detect and redirect mobile browsers
if(detect_mobile_device()){
header('Location: /mobile/'); // Direct Mobiles to Mobile Site
exit;
}

//send iphone to specific site
if(detect_iphone()){
  header('Location: /'); //Direct iPhone to normal Site
  exit;
}

What I got was a continuous loop as both the detect_mobile_device() function and the detect_iphone() function were both returning true – Ouch!

I used this work around to great effect: –

// Send ihone to specific home
  if(eregi('iPhone',$_SERVER['HTTP_USER_AGENT'])
or eregi('iPod',$_SERVER['HTTP_USER_AGENT']))
    {
        $set = 1;
    }

if ($set !=1)
    {

        if(detect_mobile_device())
            {
                  header('Location: /mobile/');
                  exit();
            }
    }

This worked a treat with iTouch and iPhones being left to go to my homepage whilst mobile browsers being pushed to the mobile page. This works because $set is only equal to 1 if an iPhone or iTouch is detected in which case the detect_mobile_device() is not run. If the device is not a iPhone or an iTouch then detect_mobile_device() is called to test for a mobile browser.

Connecting to MySQL with PHP

Connecting to a mysql database is pretty easy using php as it gives you all the commands you need in order to do it. This example is a basic way to do, there are other methods but I found this way useful when I was just starting out!

Here is an example of connecting and quering a mysql database


I usually store my connection attributes in another file and use a php include to call them. For added security I store my database connection details outside the web root.

The Include File

Create a new php file and call it something obscure as this, I always do this from habit, I never call my include file DatabaseConnect.php for example, if someone was browsing it would give the game away!

For now create the file MyCat.php and save it outside of your document root, for example in windows you could store it in c: and on a linux system in /home/me/ for example.

The MyCat.php file



Now when connecting and querying a database you would include the above file



This is now easier to manage as well, if you have multiple scripts that connect to a database then there is no need to keep adding your database connection information to the start of each script, you just have the one line to include it!

Basic PHP – Assigning Values to Variables

Adding values to php variables is simple as the following examples shows

<?php
$name = "Bobby";
$age = "30";
?>

The example code above will add the value Bobby to the variable $name and 30 to the variable age. You can print the value of the variable out to the browser by using the echo statement as follows.

<?php
echo $bobby;
?>

Further to this we could add this html to a page.

Hi my name is <?php echo $name;?> and my age is <?php echo $age;?>

This will print out Hi my name is Bobby and my age is 30

Basic PHP – The Hello World Program

This is a simple program to start you off using php scripting. This small program prints out “Hello World” to your browser using php rather than plain html.

Type the following into a text editor and save the file as test.php

<?php
echo "hello World";
?>

If you point your browser to this file you will see Hello World! displayed in your browser, well done you have just completed your first php program!

PHP Common Errors!

PHP scripts open with a <?php and close with ?> these are often forgotten and missing them out will cause a failure!

All php statements end with a semi colon (;) PHP does not notice white space so it will continue to read until it encounters a ; or a php closing tag ?> You will often see the following error if you leave a semi colon out of your php statement.

Parse error: expecting `’,” or `’;” in /test.php on line 6

If you see this error go back through your code and look for that missing semi colon!

PHP Variables

Variables are bits of memory that are used to store information. All PHP variables begin with a dollar sign ($)other rules of PHP variables are:

  • As mentioned php variables begin with a $. This tells php that we are declaring a variable
  • PHP variables can be any length although you should name variables so you can remember then!
  • Variables can contain letters, numbers and underscores
  • PHP variables cannot begin with a number
  • Be careful of capitals!, The variable $Myvariable is different from a variable called $myvariable

You may also be interested in my Article about adding values to php variables

Whilst learning php I found the PHP for dummies book invaluable and I highly recommend it! PHP & MySQL® For Dummies® (For Dummies)