Awesome q2a theme

As you are adding the images to reduce it?

0 like 0 dislike
34 views
Hello, here is the code
<?php// ограничение размера файла$limit_size = 1*1024*1024; // 1 Mb// корректные форматы файлов$valid_format = array("jpeg", "jpg", "gif", "png");// путь до нового файла$path_file = "resource/img/avatars/";// имя нового файла$rand_name = md5(time() . mt_rand(0, 9999));if($_FILES){if($_FILES["avatar"]["size"] > $limit_size){ echo 'Error!  Make sure that the picture is not bigger than 1MB.'; }else{ $format = end(explode(".", $_FILES["avatar"]["name"])); if(!in_array($format, $valid_format)){ echo 'Error!  Make sure that the picture format (png,jpg,jpeg,png).'; }else{ if(is_uploaded_file($_FILES["avatar"]["tmp_name"])){ echo 'Super!  Now you have a new avatar.'; move_uploaded_file($_FILES["avatar"]["tmp_name"], $path_file . $rand_name . ".$format"); mysqli_query($link,"UPDATE `users` SET `avatar`= '/$path_file$rand_name.$format' WHERE id=$server"); }else{ echo 'Error!  The error loading.'; } } } } ?>


Can peredelat to the photo shrank when adding
or throw off the old or existing code just to make it ("jpeg", "jpg", "gif", "png")
by | 34 views

2 Answers

0 like 0 dislike
by
0 like 0 dislike
Well, if handles, it can be something like this:
$src_path = $_FILES["avatar"]["tmp_name"]; $dest_path = $path_file . $rand_name . ".$format"; // load the picture $type = exif_imagetype($src_path); switch ($type) { case IMAGETYPE_JPEG: $src_img = imagecreatefromjpeg($src_path); break; case IMAGETYPE_PNG: $src_img = imagecreatefrompng($src_path); break; case IMAGETYPE_GIF: $src_img = imagecreatefromgif($src_path); break; case IMAGETYPE_BMP: $src_img = imagecreatefrombmp($src_path); break; default: // ??? // throw new \\Exception("Unknown image type $type of file '$src_path'", 1); break; } // decrease in Ramer 200x200 (right scale need to take into account the aspect ratio) $dest_img = imagescale($src_img, 200, 200, -1, IMG_BICUBIC_FIXED); // save the result in JPEG if (!imagejpeg($dest_img, $dest_path, 85)) { // ??? // throw new \\Exception("Can't save JPEG image '$dest_path'", 1); } // free the memory imagedestroy($src_img); imagedestroy($dest_img);

Ripped from a real project. This code is line move_uploaded()
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users