четверг, 3 мая 2012 г.

Resize image


function resize($upload_data, $w, $h, $path)
{
$config['source_image'] = $upload_data['full_path'];
$config['maintain_ratio'] = TRUE;
$config['new_image'] = $path;
$config['width'] = $w;
$config['height'] = $h;

if ($upload_data['image_width']/$w < $upload_data['image_height']/$h)
$config['height'] = round($upload_data['image_height']*$w/$upload_data['image_width']);
else
$config['width'] = round($upload_data['image_width']*$h/$upload_data['image_height']);

$this->image_lib->initialize($config);

if ( ! $this->image_lib->resize())
{
   $this->data['error'] .= $this->image_lib->display_errors();
echo $this->data['error']; die();
}
else
{
$config['source_image'] = $config['new_image'];
$config['maintain_ratio'] = FALSE;
if ($config['width'] > $config['height'])
$config['x_axis'] = round(($config['width']-$w)/2);
else
$config['y_axis'] = round(($config['height']-$h)/2);

$config['width'] = $w;
$config['height'] = $h;
$this->image_lib->initialize($config);

$this->image_lib->crop();

}
}

EXAMPLE OF USE


function save_photo ($upload_data, $i, $id)
{

$path_to_image_directory = './uploads/items/'.$id;

$path = $path_to_image_directory.'/';
$path_th = $path_to_image_directory.'/thumb/';


$path_to_orig_image = $path.$i.'_orig.jpg';

if (!file_exists($path_to_orig_image))
copy($upload_data['full_path'], $path_to_orig_image);

if ($upload_data['image_width'] < 639 || $upload_data['image_height'] < 426)
{
$this->data['error'] .= 'Small image(img' . $i . ')';
$this->data['err_upl_img'][$i] = 'Small image(img' . $i . ')';
}

if ($upload_data['image_width'] < 639 && $upload_data['image_height'] < 426)
{
copy($upload_data['full_path'], $path.$i.'.jpg');
}
else
{
$this->resize($upload_data, 639, 426, $path.$i.'.jpg');
}

$this->resize($upload_data, 40, 40, $path_th.$i.'.jpg');

if($i == 0)
$this->resize($upload_data, 134, 89, $path_th.$i.'_main.jpg');

unlink (  $upload_data['full_path'] );  //-> copy and rename

return true;
}