/ Published in: PHP
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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Store uploaded file 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; // 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; } elseif($width < $height) { $san = $height / $width; $san2 = $width / $wished_output_width; } 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; } elseif($width < $height) { $san = $height / $width; $san2 = $width / $wished_output_width; } else { // If someone uploads a square image if($wished_output_width > $wished_output_height) { $san = $height / $width; $san2 = $width / $wished_output_width; } elseif($wished_output_width < $wished_output_height) { $san = $width / $height; $san2 = $height / $wished_output_height; } 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(); }