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 ='Не была загружена картинка'; } } 
  • 2
    You insert a progress bar on the page and you're done! It does not slow down, it processes :) - Vasily Koshelev
  • @Vasily Koshelev is a good option, but I would still like to solve problems in a different way :) - Dmitry
  • "It works for a long time" - a loose concept. It depends on many factors, such as the speed of the Internet, the speed of work, which reduces the size. You tested in ideal conditions - did you perform all the actions on one server? - Vasily Koshelev
  • @VasilyKoshelev on the local and on the hosting. I just have such thoughts that I do too many actions. Can I somehow reduce the code or queries to the database? Or maybe I don’t use any functions as necessary ... I’m just learning PHP, so here. - Dmitry
  • @VasilyKoshelev And more, I also use this code when updating the cover of a user profile. Just the same piece of code, only on the condition that another form was submitted. It's all in one controller. Could this slowdown be due to these conditions? In general, I thought to put this piece into a separate function ... but I don’t know how to do it better - Dmitry

0