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 3609212
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:35:54+00:00 2026-05-18T21:35:54+00:00

I wrote a function to resize and upload images…it works, but only for one

  • 0

I wrote a function to resize and upload images…it works, but only for one image. So if I call the function three times, I end up with 3 copies of the last image…..

function uploadImage($name,$width,$height,$size,$path='content/user_avatars/')
{
    //===================================================
    //Handle image upload
    $upload_error=0;
    //Picture
    $img = $_FILES[$name]['name'];
    if($img)
    {
        $file = stripslashes($_FILES[$name]['name']);
        $ext = strtolower(getExt($file));

        if($ext!='jpg' && $ext!='jpeg' && $ext!='png' && $ext!='gif')
        {
            $error_msg = "Unknown extension";
            $upload_error = 1;
            return array($upload_error,$error_msg);
        }

        if(filesize($_FILES[$name]['tmp_name'])>$size*1024)
        {
            $error_msg = "Max file size of ".($size*1024)."kb exceeded";
            $upload_error = 2;
            return array($upload_error,$error_msg);
        }

        $newFile = time().'.'.$ext;

        resizeImg($_FILES[$name]['tmp_name'],$ext,$width,$height);
        $store = copy($_FILES[$name]['tmp_name'],$path.$newFile);
        if(!$store)
        {
            $error_msg = "Uploading failed";
            $upload_error = 3;
            return array($upload_error,$error_msg);
        }
        else
        {
            return array($upload_error,$newFile);
        }
    }
}

//=========================================================================================

//Helper Functions
function getExt($str)
{
    $i = strpos($str,".");
    if(!$i)
    {
        return "";
    }
    $l = strlen($str)-$i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}

function resizeImg($file,$ext,$width,$height)
{
    list($aw,$ah) = getimagesize($file);
    $scaleX = $aw/$width;
    $scaleY = $ah/$height;

    if($scaleX>$scaleY)
    {
        $nw = round($aw*(1/$scaleX));
        $nh = round($ah*(1/$scaleX));
    }
    else
    {
        $nw = round($aw*(1/$scaleY));
        $nh = round($ah*(1/$scaleY));
    }


    $new_image = imagecreatetruecolor($nw,$nh);
    imagefill($new_image,0,0,imagecolorallocatealpha($new_image,255,255,255,127));
    if($ext=='jpg'||$ext=='jpeg')
    {
        $src_image = imagecreatefromjpeg($file);
    }
    else if($ext=='gif')
    {
        $src_image = imagecreatefromgif($file);
    }
    else if($ext=='png')
    {
        $src_image = imagecreatefrompng($file);
    }
    imagecopyresampled($new_image,$src_image,0,0,0,0,$nw,$nh,$aw,$ah);

    if($ext=='jpg'||$ext=='jpeg')
    {
        imagejpeg($new_image,$file,100);
    }
    else if($ext=='gif')
    {
        imagegif($new_image,$file);
    }
    else if($ext=='png')
    {
        imagepng($new_image,$file,9);
    }

    imagedestroy($src_image);
    imagedestroy($new_image);
}

I have a form with two upload fields, ‘face_pic’ and ‘body_pic’, and I want to upload these two to the server and resize them before storing. Any ideas?

  • 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-18T21:35:55+00:00Added an answer on May 18, 2026 at 9:35 pm
    md5(microtime())
    

    Try naming it like this.

    Or try something like this…

    function slikeAvatar($slika,$id = 0){
        copy($slika, "avatari/{$id}l.jpg");
        $gfx = new Thumbnail($slika, 200);
        $gfx->save("avatari/{$id}.jpg");
        unset($gfx);
        $gfx = new Thumbnail($slika, 75);
        $gfx->save("avatari/{$id}s.jpg");
        unset($gfx);
        slikeCrop("avatari/{$id}s.jpg","avatari/{$id}s.jpg"); 
    }
    

    slike = images

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

Sidebar

Related Questions

I have this code where I need to call a function multiple times. $(window).resize(function()
I am having a very interesting problem. The script I wrote below works, but
I am trying to write a javascript function to resize an image based on
I am trying to resize images if they are too big in my image
I wrote function onclick of a element <div class=left_collapsed>Որոնում</div> <div class=container style=display: none;> <ul>
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map
I wrote a function in Java to fetch 10 links by their IDs from
I wrote a function to zoom in a UIView using CGAffineTransformScale. After a 2x
I wrote this function to make columns sortable. I want to rearrange divs based
I wrote this function for filling closed loop, pixvali is declared globally to store

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.