Need help resizing profile image in php

I have two questions.

  1. Right now, a user can upload a profile image in “profile” folder. If the user uploads a new profile image, it does that. However the old image is still stored in “profile” folder. How do I make sure that the old image is always deleted if a new image is uploaded?

  2. I need to scale/resize the profile image. I can easily do it in css but that’s not the right way. I have searched and found different ways to do it. However I am little lost on how it would fit in my code.

Below is my php code.

loggedin page
[php]<?php
if (isset($_FILES[‘profile’]) === true) {
if (empty($_FILES[‘profile’][‘name’]) === true) {
echo “Please choose a file!”;
} else {
$allowed = array(‘jpg’, ‘jpeg’, ‘gif’, ‘png’);

        $file_name = $_FILES['profile']['name'];
        $file_extn = strtolower(end(explode('.', $file_name)));
        $file_temp = $_FILES['profile']['tmp_name'];
        
        if (in_array($file_extn, $allowed) === true) {
            change_profile_image($session_user_id, $file_temp, $file_extn);
            header ('Location: index.php');
            exit();
        } else {
            echo "Incorrect file type. Allowed: ";
            echo implode(', ', $allowed);
        }
    }
}
if (empty($user_data['Profile']) === false) {
    echo '<img src="', $user_data['Profile'], '" alt="', $user_data['Name'], '\'s Profile Image">';
}[/php]

function for loggedin page
[php]function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = ‘images/profile/’ . substr(md5(time()), 0, 10) . ‘.’ . $file_extn;
move_uploaded_file($file_temp, $file_path);
mysql_query(“UPDATE users SET Profile = '” . mysql_real_escape_string($file_path) . "’ WHERE user_id = " . (int)$user_id);
} [/php]

So how do i go on about adding the image resize code?

Here is the updated function. I was following a tutorial that does the resizing. Though it doesn’t work for me. Perhaps you can see what’s wrong with it.

[php]function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = ‘images/profile/’ . substr(md5(time()), 0, 10) . ‘.’ . $file_extn;
move_uploaded_file($file_temp, $file_path);

if (file_exists($profile) === true) { 
	$src_size = getimagesize($profile);
	
	if ($src_size['mime'] === 'image/jpg') {
		$src_img = imagecreatefromjpg($profile);	
	} else if ($src_size['mime'] === 'image/jpeg') {
		$src_img = imagecreatefromjpeg($profile); 
	}	else if ($src_size['mime'] === 'image/gif') {
		$src_img = imagecreatefromgif($profile); 
	}	else if ($src_size['mime'] === 'image/png') {
		$src_img = imagecreatefrompng($profile); 
	} else {
		$src_img = false;
	}
	if ($src_img !== false) {
		$thumb_width = 200;
		
		if ($src_size[0] <= $thumb_width) {
			$thumb = $src_img;
		} else {
			$new_size[0] = $thumb_width;
			$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
			
			$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
			imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
		}
		
	}
}
mysql_query("UPDATE users SET Profile = '" . mysql_real_escape_string($file_path) . "' WHERE user_id = " . (int)$user_id);

} [/php]

Never mind, I found a different way to make this work. However I am still looking for a way to replace the existing profile image with new one.

Sponsor our Newsletter | Privacy Policy | Terms of Service