Return to Snippet

Revision: 26435
at April 28, 2010 09:11 by polarbear


Initial Code
// Store uploaded file data
$data = array('userfile' => $this->upload->data());
// Set source file path
$source_file = $data['userfile']['file_path'].$data['userfile']['file_name'];
// Set resized file name
$resized_file = $data['userfile']['file_path'].'sm_'.$data['userfile']['file_name'];
// Set cropped file name
$cropped_file = $data['userfile']['file_path'].'thumb_'.$data['userfile']['file_name'];

// Load in the image manipulation library
$this->load->library('image_lib');

// Determine final width and height
$wished_output_width = 160;
$wished_output_height = 130;

// Parameters for file resize
$config['source_image'] = $source_file;
$config['new_image'] = $resized_file;
$config['image_library'] = 'gd2';
$config['create_thumb'] = false;

list($width, $height, $type, $attr) = getimagesize($source_file);

// Check to see if we want a square image
if($wished_output_width == $wished_output_height)
{
	if($width > $height) {
	    $san = $width / $height;
	    $san2 = $height / $wished_output_height;
	    $new_width = round($width / $san2);
	    $new_height = round($new_width / $san);
	}
	elseif($width < $height) 
	{
	    $san = $height / $width;
	    $san2 = $width / $wished_output_width;
	    $new_height = round($height / $san2);
	    $new_width = round($new_height / $san);
	} 
	else 
	{
		$new_height = $wished_output_height;
		$new_width = $wished_output_width;
	}
}
else
{
	// If it's not square
	// run some numbers to determine the right dimensions
	if($width > $height) {
	    $san = $width / $height;
	    $san2 = $height / $wished_output_height;
	    $new_width = round($width / $san2);
	    $new_height = round($new_width / $san);
	}
	elseif($width < $height) 
	{
	    $san = $height / $width;
	    $san2 = $width / $wished_output_width;
	    $new_height = round($height / $san2);
	    $new_width = round($new_height / $san);
	} 
	else 
	{
		// If someone uploads a square image
		if($wished_output_width > $wished_output_height)
		{
			$san = $height / $width;
		    $san2 = $width / $wished_output_width;
		    $new_height = round($height / $san2);
		    $new_width = round($new_height / $san);
		}
		elseif($wished_output_width < $wished_output_height)
		{
			$san = $width / $height;
	    	$san2 = $height / $wished_output_height;
	    	$new_width = round($width / $san2);
	    	$new_height = round($new_width / $san);
		}
		else
		{
			// ...be completely baffled...
		}
	}
}

// ...more parameters for file resize
$config['width'] = $new_width;
$config['height'] = $new_height;

// Initiate file resize
$this->image_lib->initialize($config);
$this->image_lib->resize();

// Determine if file is already the right size
if($new_width == $wished_output_width && $new_height == $wished_output_height) {
    // Do nothing because it's already the right dimensions
}else {
	// Determine where to set crop offset for x and y
    $x = (($new_width - $wished_output_width) > 0) ? ($new_width - $wished_output_width)/2 : 0;
    $y = (($new_height - $wished_output_height) > 0) ? ($new_height - $wished_output_height)/2 : 0;
    
    // Parameters for file cropping
    $config['width'] = $wished_output_width;
    $config['height'] = $wished_output_height;
    $config['x_axis'] = $x;
    $config['y_axis'] = $y;
    $config['maintain_ratio'] = FALSE;
    $config['source_image'] = $resized_file;
    $config['new_image'] = $cropped_file;
    
    // Initiate file cropping
    $this->image_lib->initialize($config);
    $this->image_lib->crop();
}

Initial URL


Initial Description
I know this code isn't perfect (and there's a spot where it kind of repeats itself), but I figured I'd share anyway.

NOTE: This code assumes that you are programming in CodeIgniter and you have uploaded a file and posted to a Controller which then contains this chunk of code.

Initial Title
Automatic Resize/Crop Image - CI

Initial Tags
codeigniter

Initial Language
PHP