Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:19:33+00:00 2026-05-25T19:19:33+00:00

i have a user profile in php and i want to give users the

  • 0

i have a user profile in php and i want to give users the choice of changing their profile picture. But when they submit their new picture via $_POST i want the picture to be resized to:

height:110px | width:relevant to the height (if the width is bigger than height)

width:110px | height:relevant to the width (if the height is bigger than width)

when the resize is done, i want to crop the picture so it becomes 110px x 110px but i want it to be centered.

For example if the user uploads a picture with 110px width and 200px height (dimensions after the resize) the new image after crop will be 110×110 cropped by 90px from right. What i want is to cropped 45px from left and another 45px from right so it will be centered

the function will accept .png, .gif and .jpg images and will save the new image only in jpg format no matter what the initial format was.

I searched a lot to create such a function and i found an answer but any time i atempt to change some minor detail everything stop working properly.

My code so far:

<?php

$userfile_name = $_FILES["sgnIMG"]["name"];
$userfile_tmp = $_FILES["sgnIMG"]["tmp_name"];
$userfile_size = $_FILES["sgnIMG"]["size"];
$filename = basename($_FILES["sgnIMG"]["name"]);
$file_ext = substr($filename, strrpos($filename, ".") + 1);
$large_image_location = $target_path . $filename;
$ext = '';

if ($file_ext == 'jpg') {
    $ext = 1;
} else if ($file_ext == 'gif') {
    $ext = 2;
} else if ($file_ext == 'png') {
    $ext = 3;
} else {
    $ext = 0;
}

$target = $target_path . basename($_FILES["sgnIMG"]["name"]);

if (move_uploaded_file($userfile_tmp, $target)) {
    $newImg = resize110($target, $ext);
    if (isset($_POST['imupd']) && ($_POST['imupd'] == 'up')) {
        $sql = "UPDATE users SET avatar='" . str_replace('im/users/', '', $newImg) . "' WHERE id=" . $_SESSION['sesID'] . "";
        $result = mysql_query($sql);
        if ($result) {
            echo '<img src="' . $newImg . '" width="110" title="' . $file_ext . '"/>';
        } else {
            echo '<img src="im/avatars/px.png" width="110" title="' . $file_ext . '"/>';
        }
    }
} else {
    
}

function getHeight($image)
{
    $sizes = getimagesize($image);
    $height = $sizes[1];
    return $height;
}

function getWidth($image)
{
    $sizes = getimagesize($image);
    $width = $sizes[0];
    return $width;
}

function resize110($image, $ext)
{
    chmod($image, 0777);
    $oldHeight = getHeight($image);
    $oldWidth = getWidth($image);
    if ($oldHeight < $oldWidth) {
        $newImageHeight = 110;
        $newImageWidth = ceil((110 * $oldWidth) / $oldHeight);
        imagecopyresampled($newImage, $source, -ceil(($newImageWidth - 110) / 2), 0, 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
    } else {
        $newImageHeight = ceil((110 * $oldHeight) / $oldWidth);
        $newImageWidth = 110;
        imagecopyresampled($newImage, $source, 0, -ceil(($newImageHeight - 110) / 2), 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
    }
    $newImage = imagecreatetruecolor(110, 110);
    chmod($image, 0777);
    return $image;
    switch ($ext) {
        case 1;
            $source = imagecreatefromjpeg($image);
            break;
        case 2;
            $source = imagecreatefromgif($image);
            break;
        case 3;
            $source = imagecreatefrompng($image);
            break;
    }

    imagejpeg($newImage, $image, 90);
    return $image;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:19:34+00:00Added an answer on May 25, 2026 at 7:19 pm

    I looked around a lot and combined different parts of code i found. So this script will take a jpg,gif of png image, resize it to 110px width if width is greater of 110px height if height is greater. The ascpect ratio will remain so the remaining pixels will be divided by 2 will be used to center the image.

    for a different size just change 110 everywhere.

    ==================================================================================

    <?php
    
    // pfpic  ->  the name of the <input type="file" name="pfpic"/> where user chooses file
    
    $target_path = "im/users/";                                // the directory to store the uploaded and then resampled image
    $userfile_name = $_FILES["pfpic"]["name"];                  // the name that the image file will have once uploaded
    $userfile_tmp = $_FILES["pfpic"]["tmp_name"];                   // the temporary name the server uses to store the file
    $userfile_size = $_FILES["pfpic"]["size"];                  // the size of the file that we want to upload
    $filename = basename($_FILES["pfpic"]["name"]);             // the full name of the file
    $file_ext = substr($filename, strrpos($filename, ".") + 1);  // the file extension
    $large_image_location = $target_path.$filename;                  // the full path to the file
    $ext='';
    
    
    if($file_ext=='jpg')
    {
        $ext=1;
    }
    else if ($file_ext=='gif')
    {
        $ext=2;
    }
    else if ($file_ext=='png')
    {
        $ext=3;
    }
    else
    {
        $ext=0;
    }
    
        $target = $target_path.basename(sha1($_SESSION['sesID']).'.'.'jpg');
        if($ext!=0)
        {
            if(move_uploaded_file($userfile_tmp,$target))
            {
                $newImg=resize110($target,$ext);
                echo '<img src="'.$newImg.'"/>';
            }
            else
            {
                echo 'the file could not be uploaded, please try again';
            }
        }
        else
        {
            echo 'this file extension is not accepted, please use "jpg", "gif" or "png" file formats';
        }
    
        function getHeight($image) 
        {
            $sizes = getimagesize($image);
            $height = $sizes[1];
            return $height;
        }
    
        function getWidth($image) 
        {
            $sizes = getimagesize($image);
            $width = $sizes[0];
            return $width;
        }
    
    
        function resize110($image,$ext) 
        {
            chmod($image, 0777);
            $oldHeight=getHeight($image);
            $oldWidth=getWidth($image);
            switch ($ext)
            {
                case 1;
                    $source = imagecreatefromjpeg($image);
                break;
    
                case 2;
                    $source = imagecreatefromgif($image);
                break;
    
                case 3;
                    $source = imagecreatefrompng($image);
                break;
            }
            $newImage = imagecreatetruecolor(110,110);
            $bgcolor = imagecolorallocate($newImage, 255, 255, 255);
            imagefill($newImage, 0, 0, $bgcolor);       // use this if you want to have a white background instead of black
    
    
            // we check tha width and height and then we crop the image to the center
            if($oldHeight<$oldWidth)
            {
                $newImageHeight = 110;
                $newImageWidth = ceil((110*$oldWidth)/$oldHeight);
                imagecopyresampled($newImage,$source,-ceil(($newImageWidth-110)/2),0,0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
            }
            else
            {
                $newImageHeight = ceil((110*$oldHeight)/$oldWidth);
                $newImageWidth = 110; 
                imagecopyresampled($newImage,$source,0,-ceil(($newImageHeight-110)/2),0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
            }
    
            //we save the image as jpg resized to 110x110 px and cropped to the center. the old image will be replaced
            imagejpeg($newImage,$image,90);
    
            return $image;
    
        }
    

    ?>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want that every user has a profile-url like this: www.example.com/username But my php
I have a website in PHP, and I want that when user logs in
I have a table for users. But when a user makes any changes to
I have a user profile page on my web app which has contact information.
I have a user and nested profile class as follows: class User < ActiveRecord::Base
Dear all, I have a question about Facebook Page: ( NOT user profile page,
I have the following models: User: hasOne Profile, Page hasMany Comment Comment: belongsTo User,
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I have many many small images that are displayed on a user's profile and
In my CakePHP 1.2.5 app, I have a Profile model that belongsTo a User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.