Atilla Ördög

Personal site of Atilla Ördög AKA Gadratil

ImageHandler, a small and useful tool

Usually every CMS and Framework has its own image handling class. The problem is between, however, because in a CMS image handling is part of a media library that is big and complex.

In a Framework image handling is not done at all, it just has some classes for helping the developer.

This gap is what I tried to fill by creating a small tool for handling images.

This tool is very lightweight, easy to use and covers a large area in image handling.

It is created in a way that a developer can easily change the storage and validation classes if needed. Currently data is saved into DB using Mysqli and validation checks basic aspects.

Configuration can be passed using the Config class and the config file in the base folder, but config params can be passed upon initialization, too.

The image handling is done by Kohana`s Image library, surely it is a bit modified to fit my needs. This tool uses GD or Imagick library, so you will have to enable that if you want to handle images.

What you will need to run this code: PHP 5+, mysqli, GD or Imagick

Here is a full sample of the usage of this library

include_once('ImageHandler/autoload.php');

// Initialize the class, it loads config from the config file, but configs can be overwritten from here
$imageHandler = new ImageHandler(
array(
'sizes' => array(
'normal' => array('width' => 50, 'height' => 50)
),
'db' => array(
'server' => 'localhost',
'user' => 'root',
'pass' => '',
'database' => 'test',
'table_name' => 'images'
),
'base_path' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'test_images'
)
);

// Check if storage exists and create it if does not
if ( !$imageHandler->storage_exists() )
{
$imageHandler->install_storage();
}

// Check if we have to delete
if ( array_key_exists('delete_image', $_GET) )
{
$imageHandler->delete_image_by(array('object_id' => 1, 'object_type' => 'test'));
}

// Handle POST
if ( $_POST )
{
if ( array_key_exists('image', $_FILES) )
{
// Validate and save image
if ( $imageHandler->validate_image($_FILES['image']) )
{
$imageHandler->save_image($_FILES['image'], 1, 'test', 'test_images/1');
}
else
{
var_dump($imageHandler->get_errors());
}
}
}

echo 'Current image:';

$images = $imageHandler->get_image_by(array('object_id' => 1, 'object_type' => 'test'));

var_dump($images);

You can find this library here: https://github.com/atillaordog/ImageHandler

Advertisement

Easy Pagination helper

One of the first things I wrote when I started working in PHP was a nice little Pagination helper class.

It is very easy to use and made my work easier many times.

So here it is a code sample of how to use it:

// First we have to get the total elements

// Create the limit, offset and other useful data first
$pag = Pagination::paginate($total_nr, $current_page, 20);

// Here we get our data using limit, offset

// Build the pagination
$pagination = new Pagination(array(
'total' => $total_nr, // The nmber of all elements
'items_per_page' => $pag['pagination'], // How many items will there be on the page
'base_url' => $url,  // The base url used, this can contain $_GET elements too, they will not be overwritten
'current_page' => $pag['current'], // Current page we are on
'links_to_show' => 3, // How many links to show between the 3 dots
'page_name' => 'sm_page', // Name of the $_GET parameter to use
'first_string' => '<<', // String that is used for the FIRST link
'prev_string' => '<', // string used for the PREVIOUS link
'dots_string' => '...', // String used for the dots between numbers
'next_string' => '>', // String used for the NEXT link
'last_string' => '>>' // String used for the LAST link
));

// Generate the array of pagination links
$pagination_links = $pagination->render();

You can also set the mentioned variables after initialization like:

$pagination->base_url = '';

Note that you have to set all variables before calling render()

 

It can be found here:

https://github.com/atillaordog/Pagination

Validation Wall

I re-imagined data validation in PHP. Why did I do this? I found that current validation techniques are too strict and cannot be used alone.

So, I thought the following: validation is like a filter. A wall that has different doors, each door specific to a set of data. Data comes and in order to reach its destination, it has to go over the door. Now the door has rule-sets, these are the rules specific to a part from the data.

These rule sets are built from rules, that are the basic rules found in any validation system.

A wall can be built easily by simply grouping the rules to fit our needs.

Here is an example:

$title_ruleset = new MainRuleset('title', array( new NotEmpty() ));
$description_ruleset = new MainRuleset('description', array( new NotEmpty() ));
$categories_ruleset = new MainRuleset('categories', array( new NotEmpty() ));

$door = new Door(array($title_ruleset, $description_ruleset, $categories_ruleset));

$vw = new ValidationWall($door);

$post_passes = $vw->pass($post);

In this example we use the NotEmpty rule to check the title, the description and the category.

 

To use ValidationWall, you have to include the autoload file like:

include_once route/to/ValidationWall/folder/autoload.php';

Also you have to use the namespaces like:

use ValidationWallDoorMainDoor as Door;
use ValidationWallRuleSetMainRuleset as MainRuleset;
use ValidationWallRuleNotEmpty as NotEmpty;
use ValidationWallRuleNumeric as RuleNumeric;

After you initialized ValidationWall, you can start building your wall to fit your needs.

Main benefits:

  • It is totally stand-alone, usable anywhere in any script
  • Logically more understandable than general validation
  • Rules are custom, meaning you can build any rule you want, you just have to implement the interface or extends the abstract class
  • Build validation like you are playing Lego

You can find this little code here:

https://github.com/atillaordog/ValidationWall

Paypal Yearly Subscription

Check out my latest WordPress plugin.

I had to make a plugin for a very simple yearly subscription, since all the plugins out there are overcomplicated.

This plugin uses PayPal ExpressCheckout method for payment and has a simple setting page to change everything you need.

It uses shortcodes to handle the pay button, the return url and one for enclosing content that requires subscription.

Sreenshot:

screenshot

You can find this plugin here:

https://wordpress.org/plugins/paypal-yearly-subscription/