Development Kohana PHP

Authored by Corey Ballou

Kohana Callback Function to Validate Uploaded Image Sizes

I wanted to share a very quick snippet of code I conjured up to demonstrate the use of Kohana’s validation library to verify the proper size of an uploaded image submitted from form input. The height and width class vars specify the height/width or max height/max width of the image in pixels depending on the boolean value of EXACT_SIZE. If EXACT_SIZE is false, we assume that both the height and width of the uploaded image must be less than or equal to the two constant sizes.

My add_rules statement is by no means required for this example but demonstrates built in Kohana image validation. I could have just as easily created a Validation Factory and chained the instantiation of the validation class along with it’s rules. One thing to note is the call to the callback function itself. Since the callback is contained within the current model, Test_Model, we must access it in the same manner you would with the php function call_user_func.

The function getimagesize will return an array of 7 variables by default. In our case we simply need the first two array indices to obtain the width and height respectively.

Author: Corey Ballou

Corey Ballou is the CEO of Whether you're a student, young professional, entrepreneur, startup, or small business, you can be up and online fast with your own custom domain, email, and webpage on POP. Corey is a professional PHP developer by trade, specializing in custom web applications development for startups, small businesses, and agencies. Follow Corey on Twitter @cballou.