Hello. This is a user avatar update script. Taking a photo, reducing the size, creating a folder, saving it under a certain name. Also here, right away is the formation of a post for the wall, that the user has updated the avatar. All this is shoved into the base. And the image is also saved in the folder with the posts of this user. It works for a long time ... I would really like to do better. I will be glad to any advice or criticism.
else if($user->step == 5 AND $post = $this->request->post('add_avatar')) { if($_FILES['avatar']['name'] !== "") { $last_post = ORM::factory('Post') ->limit(1) ->order_by('id', 'DESC') ->find(); $last_post = $last_post->id; $last_post++; $path = 'public/images/user/'.$user->id.'/'; $dir = 'public/images/post/'.$user->id; $post = 'public/images/post/'.$user->id.'/'.$last_post.'/'; if (file_exists($dir)) { if (file_exists($dir.'/'.$last_post)) { function dirDel ($dir) { $d=opendir($dir); while(($entry=readdir($d))!==false) { if ($entry != "." && $entry != "..") { if (is_dir($dir."/".$entry)) { dirDel($dir."/".$entry); } else { unlink ($dir."/".$entry); } } } closedir($d); rmdir ($dir); } dirDel($dir.'/'.$last_post); mkdir($post, 0700); } else { mkdir($post, 0700); } } else { mkdir($dir, 0700); mkdir($post, 0700); } if($user->avatar == 1) { unlink($path.'avatar.jpg'); } $size = 1000; $src = imagecreatefromjpeg($_FILES['avatar']['tmp_name']); $w_src = imagesx($src); $h_src = imagesy($src); $w = $size; if($w_src >= $w) { $ratio = $w_src/$w; $w_dest = round($w_src/$ratio); $h_dest = round($h_src/$ratio); $dest = imagecreatetruecolor($w_dest, $h_dest); imagecopyresampled($dest, $src, 0, 0, 0, 0, $w_dest, $h_dest, $w_src, $h_src); imagejpeg($dest, $path.'avatar.jpg'); imagejpeg($dest, $post.'main.jpg'); imagedestroy($dest); $model = ORM::factory('Post'); $model->values(array( 'post_id' => $last_post, 'user_id' => $this->user->id, 'text' => 'обновил фотографию на странице', 'img' => '1' )); if(!$model->save()) { $errors = 'Не удалось добавить запись. Попробуйте снова.'; } $count = ORM::factory('Profile') ->where('user_id', '=', $user->id) ->find(); $count_post = $count->count_post; $count_post++; $data = array('count_post' => $count_post); ORM::factory('Profile', $count->id) ->values($data) ->save(); } else { $dest = imagecreatetruecolor($w_src, $h_src); imagecopyresampled($dest, $src, 0, 0, 0, 0, $w_src, $h_src, $w_src, $h_src); imagejpeg($dest, $path.'avatar.jpg'); imagejpeg($dest, $post.'main.jpg'); imagedestroy($dest); $model = ORM::factory('Post'); $model->values(array( 'post_id' => $last_post, 'user_id' => $this->user->id, 'text' => 'обновил фотографию на странице', 'img' => '1' )); if(!$model->save()) { $errors = 'Не удалось добавить запись. Попробуйте снова.'; } $count = ORM::factory('Profile') ->where('user_id', '=', $user->id) ->find(); $count_post = $count->count_post; $count_post++; $data = array('count_post' => $count_post); ORM::factory('Profile', $count->id) ->values($data) ->save(); } imagedestroy($src); $this->redirect("/id".$user->id); } else { $errors ='Не была загружена картинка'; } }