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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:20:22+00:00 2026-05-14T02:20:22+00:00

BACKGROUND I have a script to upload an image. One to keep the original

  • 0

BACKGROUND
I have a script to upload an image. One to keep the original image and one to resize the image.
1. If the image dimensions (width & height) are within max dimensions I use a simple “copy” direct to folder UserPics.
2. If the original dimensions are bigger than max dimensions I want to resize the width and height to be within max.
Both of them are uploading the image to the folder, but in case 2, the image will not be resized.

QUESTION
Is there something wrong with the script?
Is there something wrong with the settings?

SETTINGS
Server: WAMP 2.0
PHP: 5.3.0
PHP.ini: GD2 enabled, Memory=128M (have tried 1000M)
Tried imagetypes uploaded: jpg, jpeg, gif, and png (same result for all of them)

SCRIPT

if (isset($_POST['adduserpic'])) {  
    // Check errors on file  
    if ($_FILES["file"]["error"] > 0) {  
        echo $_FILES["file"]["error"]." errors<br>";  
    } else {  
        $image =$_FILES["file"]["name"];  
        $uploadedfile = $_FILES["file"]["tmp_name"];  
    //Uploaded image  
    $filename = stripslashes($_FILES['file']['name']);  

    //Read filetype  
    $i = strrpos($filename,".");  
    if (!$i) { return ""; }  
    $l = strlen($filename) - $i;  
    $extension = substr($filename,$i+1,$l);  
    $extension = strtolower($extension);  

    //New picture name = maxid+1 (from database)  
    $query = mysql_query("SELECT MAX(PicId) AS number FROM userpictures");  
    $row = mysql_fetch_array($query);  
    $imagenumber = $row['number']+1;  

    //New name of image (including path)   
    $image_name=$imagenumber.'.'.$extension;    
    $newname = "UserPics/".$image_name;  

    //Check width and height of uploaded image  
    list($width,$height)=getimagesize($uploadedfile);  

    //Check memory to hold this image (added only as checkup)   
    $imageInfo = getimagesize($uploadedfile);   
    $requiredMemoryMB = ( $imageInfo[0] * $imageInfo[1] * ($imageInfo['bits'] / 8) * $imageInfo['channels'] * 2.5 ) / 1024;  
    echo $requiredMemoryMB."<br>";  

    //Max dimensions that can be uploaded  
    $maxwidth = 20;  
    $maxheight = 20;  

    // Check if dimensions shall be original  
    if ($width > $maxwidth || $height > $maxheight) {  
        //Make jpeg from uploaded image  
        if ($extension=="jpg" || $extension=="jpeg" || $extension=="pjpeg" ) {  
            $modifiedimage = imagecreatefromjpeg($uploadedfile);  
        } elseif ($extension=="png") {  
            $modifiedimage = imagecreatefrompng($uploadedfile);  
        } elseif ($extension=="gif") {  
            $modifiedimage = imagecreatefromgif($uploadedfile);  
        }   
        //Change dimensions  
        if ($width > $height) {  
            $newwidth = $maxwidth;  
            $newheight = ($height/$width)*$newwidth;  
        } else {  
            $newheight = $maxheight;  
            $newwidth = ($width/$height)*$newheight;  
        }  

        //Create new image with new dimensions  
        $newdim = imagecreatetruecolor($newwidth,$newheight);  
        imagecopyresized($newdim,$modifiedimage,0,0,0,0,$newwidth,$newheight,$width,$height);  
        imagejpeg($modifiedimage,$newname,60);  

        // Remove temp images  
        imagedestroy($modifiedimage);  
        imagedestroy($newdim);  
    } else {  
        // Just add picture to folder without resize (if org dim < max dim)  
        $newwidth = $width;  
        $newheight = $height;  
        $copied = copy($_FILES['file']['tmp_name'], $newname);  
    }

    //Add image information to the MySQL database  
    mysql_query("SET character_set_connection=utf8", $dbh);  
    mysql_query("INSERT INTO userpictures (PicId, Picext, UserId, Width, Height, Size) VALUES('$imagenumber', '$extension', '$_SESSION[userid]', '$newwidth', '$newheight', $size)") 
  • 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-14T02:20:23+00:00Added an answer on May 14, 2026 at 2:20 am

    Have you checked that the image resizing block is actually being used? Consider some debug output in there as well. I can’t see anything obviously wrong

    if ($width > $maxWidth || etc...) {
       echo "Hey, gotta shrink that there image";
       ... do the resizing ...
       $resizedImage = getimagesize($newname);
       var_dump($resizedImage); // see if the new image actually exists/what its stats are
       etc....
    } else {
       echo "Woah, that's a small picture, I'll just make a straight copy instead";
    }
    

    You might also want to round off the $newheight/$newwidth to integer values – In almost all cases, you’ll get some fractional result and images don’t have fractional pixels.

    As an aside, you’ve got a race condition with your ID number generator:

    $query = mysql_query("SELECT MAX(PicId) AS number FROM userpictures");  
    $row = mysql_fetch_array($query);  
    $imagenumber = $row['number']+1; 
    

    Consider the case of two uploads completing at almost the same time. They’ll both get the same ID number (say, 25). And then whichever of the uploads takes longer to process will “win” and overwrite the faster one.

    Consider rewriting the database portion to use a transaction, using the following logic:

     1. start transaction
     2. insert skeleton record into the db and get its ID
     3. do image processing, copying, saving, etc...
     4. update record with the new image's stats
     5. commit the transaction
    

    This way, the transaction will “hide” the record as it’s not committed yet, there’s no way for two or more simultaneous uploads to get the same ID number, and if anything fails during image processing (insufficient ram, disk space, corrupt source image, etc…) you just roll back the transaction and clean up the mess.

    I

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

Sidebar

Related Questions

Problem background I have a post-commit script for my SVN repository which archives &
i have the following script css: img { background-image:url(man.png); background-repeat:no-repeat; padding-top:6px; padding-bottom:60px; padding-left:10px; padding-right:10px;
I have a script that switches background image on hover: $('div#example').hover(function(){ $(this).css('background',url('images/bg_2.png') no-repeat bottom);
i have a script that creates divs with some background image then moves them,
A little background: I have a perl script which is performing a number of
I have a script to change the background of a page when I hover
I have a bash script activated by crontab and running in background. I would
I have a python script that I want always to run in the background.
Suppose we have a BASH script running some commands in the background. At some
I have a script which uploads an image to the server under the id

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.