-3

I'm having a hard time to make my web-application work properly. I have been trying to make my avatar not feel pushed and make it resize automatically when uploaded. I'm not the best with PHP hence why I can't do this on my own. I would appreciate if someone could help me out. That's the code of the file that is the avatar.

Best regards!

if (isset($_FILES['myfile']) && !empty($_FILES['myfile']['name'])) {

                $name           = $_FILES['myfile']['name'];
                $tmp_name       = $_FILES['myfile']['tmp_name'];
                $allowed_ext    = array('jpg', 'jpeg', 'png', 'gif' );
                $a              = explode('.', $name);
                $file_ext       = strtolower(end($a)); unset($a);
                $file_size      = $_FILES['myfile']['size'];        
                $path           = "avatars";

                if (in_array($file_ext, $allowed_ext) === false) {
                    $errors[] = 'Image file type not allowed';  
                }

                if ($file_size > 2097152) {
                    $errors[] = 'File size must be under 2mb';
                }

            } else {
                $newpath = $user['image_location'];
            }

1 Answers1

0

You'll require to store Uploaded Picture on your webserver

For that:

You'll Require:

move_uploaded_file()

and for resizing purpose you can use:

imagecopyresampled()

as follows:

function resizeIMG($o_img // original image
, $target_image // Resized New Image
, $newWidth, $newHeight){
// Get Original Dimensions as follows:
$original_width=imagesx($o_image); //Returns width of image
$original_height=imagesy($o_image); //Returns height of image
$tmp = imagecreatetruecolor($newWidth, $newHeight); // Create New temp. image with new dimensions
imagecopyresampled($tmp_img, $o_img, 0, 0, 0, 0, $newWidth, $newHeight, $original_width, $original_height); // Resize original image to temporary Image
imagejpeg($tmp, $target_image, $quality); // Copy temp Image to Target File for JPG images
imagedestroy($tmp); // Destroy Temporary Image.
/* Use
imagepng() instead of imagejpeg() for PNG images
imagegif() instead of imagejpeg() for GIF images
*/
}

For all these functions mentioned you'll need to enable gd_drivers for your php. By simply removing ; before line extension=php_gd2.dll if you're using windows or See this for others.

Or you can Use image_magick for php.

also do have a look at: Resize images in PHP without using third-party libraries?

hope it helps.. cheers :).

Community
  • 1
  • 1
Vedant Terkar
  • 4,553
  • 8
  • 36
  • 62