- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
function addimagesave()
{
if (!$this->Login->isLogged())
{
$this->loginto();
return;
}
else $this->Login->Update();
$error = '';
$recordExists = false;
$post_name = trim($this->input->post('name'));
if ($post_name != '') $insert['file_name'] = $post_name;
// now need to get from db if id_all in i_pics
$this->db->where('id_all',$this->input->post('id_all'));
$this->db->limit(1);
$query = $this->db->get('i_pics');
if ($query->num_rows() == 1)
{
$row = $query->row();
$recordExists = true;
$file_name = $row->file_name;
$id_pics = $row->id_pics;
}
if (isset($_FILES['upload']) && ($_FILES['upload']['error'] == 0)) // is file loaded?
{
$ext = getExtensionOnly($_FILES['upload']['name']);
if ($post_name != '') $_FILES['upload']['name'] = $post_name.'.'.$ext;
else $post_name = delExtensionWithDot($_FILES['upload']['name']);
$_FILES['upload']['name'] = translitIt(delExtensionWithDot($_FILES['upload']['name'])).'.'.$ext;
$config['upload_path'] = './pics/';
$config['allowed_types'] = 'gif|jpg|png|bmp';
$this->load->library('upload', $config);
if ($this->upload->do_upload('upload'))
{
$this->General->setEmes(array('roster'=>"Картинку загружено."), true);
$data = $this->upload->data();
$insert['file_name'] = $data['file_name'];
// resize now
$config1['image_library'] = 'gd2'; // выбираем библиотеку
$config1['source_image'] = 'pics/'.$data['file_name'];
$config1['create_thumb'] = TRUE; // ставим флаг создания эскиза
$config1['maintain_ratio'] = true; // сохранять пропорции
$config1['width'] = 250; // и задаем размеры
$config1['height'] = 180;
// pre crack image for resizing! )))
$this->load->model('Image');
$this->Image->smartsigninto('pics/'.$data['file_name'], 250, 180);
$this->load->library('image_lib', $config1); // загружаем библиотеку
$this->image_lib->resize(); // и вызываем функцию
// now rename and owerwrite original image
$ext = getExtensionOnly($data['file_name']);
$fol = "pics/";
if (is_file($fol.$data['raw_name'].'_thumb.'.$ext))
rename($fol.$data['raw_name'].'_thumb.'.$ext, $fol.$data['file_name']);
else $this->General->setEmes(array('roster'=>"Файл *_thumb не был создан!"));
// now set new file size
$insert['size'] = round(filesize($fol.$data['file_name'])/1024 , 2);
}
else $this->General->setEmes(array('roster'=>$this->upload->display_errors()));
}
else // we need to try to rename assigned file if name is not empty and file exists
{
if ($recordExists && is_file('pics/'.$file_name) && ($post_name != ''))
{
$ext = getExtensionOnly($file_name);
$fol = "pics/";
$newName = translitIt($post_name).'.'.$ext;
if (is_file('pics/'.$newName))
{
$this->General->setEmes(array('roster'=>"Имя уже существует"), true);
$insert['file_name'] = $file_name;
}
else
{
rename($fol.$file_name, $fol.$newName);
$insert['file_name'] = $newName;
}
}
}
// prepare data to be ins or upd
$insert['lang'] = 'ru';
$insert['title'] = $this->input->post('title');
$insert['alt'] = $this->input->post('alt');
$insert['id_all'] = $this->input->post('id_all');
if ($recordExists)
{
$this->db->where('id_pics',$id_pics);
$this->db->update('i_pics', $insert);
$this->General->setEmes(array('roster'=>"Данные обновлены"), true);
}
else $this->db->insert('i_pics', $insert);
$this->redirect('roster/'.$this->input->post('type'));
}
Функция - контроллер сохранения, resize на лету картинки для новости или прочей байды. Есть все проверки. Любая картинка которая приходит, становится размером 250, 180 , причем без растяжения. Это задача типа вписать прямоугольник в прямоугольник, которую я сейчас ночью выполнил - даже если картинка меньше оно впишет. I must be proud about this functionality! Заметим, что в интернете все и умеют что вырезать квадраты из картинки, а произвольный размер - НЕТ такого. Код выложен чтобы вы посмотрели, нравятся ли вам имена переменных, логическое мышление, форматирование, коментарии (на русском - то не мои коментарии). Код CodeIgniter powered.