Skip to content

Instantly share code, notes, and snippets.

@sineld
Forked from evanpurkhiser/gist:2651032
Created May 10, 2012 05:59
Show Gist options
  • Save sineld/2651308 to your computer and use it in GitHub Desktop.
Save sineld/2651308 to your computer and use it in GitHub Desktop.
<?php
class Model_Student_Profile extends ORM {
/**
* Model validation rules
*/
public function rules()
{
return array(
'photo' => array(
array(array($this, 'validate_photo'), array(':validation', ':field', ':value', 200, 200)),
),
);
}
/**
* Validate a photo path OR an uploaded file array.
* This ensure the photo is the correct type and size.
*
* Also checks that a photo is large enough
*
* @param Validation $validation The validation object
* @param string $field The name of the upload field
* @param string|array Eaither a new uploaded photo or a photo path
* @param int $min_height The minimum height of the photo
* @param int $min_width The minimum width of the photo
* @return mixed
*/
public function validate_photo($validation, $field, $photo, $min_height, $min_width)
{
// Check if we JUST uploaded a new photo file the $_FILES array
if (Upload::valid($photo))
{
$photo = $photo['tmp_name'];
}
// Don't worry about it if there's no photo
if (empty($photo))
return;
try
{
try
{
// Get the width and height from the uploaded image
list($width, $height) = getimagesize($photo);
}
catch(Exception $e)
{
// Ignore file read exceptions
}
// Check the uploaded photo is indeed a valid photo
if (empty($width) OR empty($height))
{
$validation->error($field, 'invalid_file');
throw new Kohana_Exception("Invalid image file");
}
// Check that the photo is big enough
if ($height < $min_height AND $width < $min_width)
{
$validation->error($field, 'invalid_size', array($min_height, $min_width));
throw new Kohana_Exception("Invalid image size");
}
}
catch(Exception $e)
{
// Set photo field to it's old value so not to display a broken image
$this->_object[$field] = $this->_original_values[$field];
}
}
/**
* Override the save method to save an uploaded
* image into the proper directory along with
* resizing the photo down to a more manageable size
*/
public function save(Validation $validation = NULL)
{
// Ignore the photo if it wasn't set via an upload
if ( ! Upload::valid($this->photo))
return parent::save($validation);
// Validate the fields
$this->check($validation);
// Ensure that the upload was successful
if ($this->photo['error'] === UPLOAD_ERR_OK)
{
$image = Image::factory($this->photo['tmp_name']);
// Crop the image if it isn't square
if ($image->width != $image->height)
{
if ($image->width > $image->height)
{
$image->crop($image->height, $image->height);
}
else
{
$image->crop($image->width, $image->width);
}
}
// Scale the image down to 200x200
$image->resize(200, 200);
// Generate a file name and path for the image
$path = sprintf('uploads/profiles/%s.jpg', Text::random(NULL, 16));
// Ensure the directory exists in the file system
if ( ! file_exists($directory = dirname(DOCROOT.$path)))
{
mkdir($directory, 0775, TRUE);
}
// Delete the old file if there is one
if (is_file($original = DOCROOT.$this->_original_values['photo']))
{
unlink($original);
}
// Save the image into the given path
$image->save(DOCROOT.$path);
// Store the image path in the database
$this->photo = $path;
}
// Unsuccessful, reset the photo field
else
{
unset($this->_changed['photo']);
$this->_object['photo'] = $this->_original_values['photo'];
}
return parent::save($validation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment