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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:37:07+00:00 2026-06-11T11:37:07+00:00

I have an existing image upload script (below) which works fine but I would

  • 0

I have an existing image upload script (below) which works fine but I would like to add a crop function to it, so each uploaded photo maintains the right aspect ration, but is cropped to, say, 200 x 200px.

I have looked at other questions on SO relating to this but ideally I’d like to add cropping to my script, rather than implement a whole new one, if that makes sense.

Could anyone help?

Thanks as always.

mkdir("images/$user_id");
$saveto = "images/$user_id/$user_id.jpg";
move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
$typeok = TRUE;

switch($_FILES['image']['type'])
{
    case "image/gif":   $src = imagecreatefromgif($saveto); break;

    case "image/jpeg":  // Both regular and progressive jpegs
    case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;

    case "image/png":   $src = imagecreatefrompng($saveto); break;

    default:            $typeok = FALSE; break;
}

if ($typeok)
{




    list($w, $h) = getimagesize($saveto);
    $max = 200;
    $tw  = $w;
    $th  = $h;

    if ($w > $h && $max < $w)
    {
        $th = $max / $w * $h;
        $tw = $max;
    }
    elseif ($h > $w && $max < $h)
    {
        $tw = $max / $h * $w;
        $th = $max;
    }
    elseif ($max < $w)
    {
        $tw = $th = $max;
    }

    $tmp = imagecreatetruecolor($tw, $th);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
    imageconvolution($tmp, array( // Sharpen image
                            array(-1, -1, -1),
                            array(-1, 16, -1),
                            array(-1, -1, -1)
                           ), 8, 0);
    imagejpeg($tmp, $saveto);
    imagedestroy($tmp);
    imagedestroy($src);


}

EDIT: I have found the following script which works fine when its on its own page, however I’m having trouble implementing it either into or after my existing upload script – I get a few ‘failed to open stream: No such file or directory’ errors – however the path to the image is correct (i’ve echoed it out to be sure):

$filename = 'images/$user_id/$user_id.jpg';

// Get dimensions of the original image
list($current_width, $current_height) = getimagesize($filename);

// The x and y coordinates on the original image where we
// will begin cropping the image
$left = 25;
$top = 25;

// This will be the final size of the image (e.g. how many pixels
// left and down we will be going)
$crop_width = 200;
$crop_height = 200;

// Resample the image
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, $filename, 100);

Could someone help me put the two together?

Thanks

  • 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-06-11T11:37:08+00:00Added an answer on June 11, 2026 at 11:37 am

    Thanks to everyone for there responses – I got my script working by changing

    $filename = 'images/$user_id/$user_id.jpg';
    

    to

    $filename = "images/$user_id/$user_id.jpg";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested form in which I would like the form fields to
I have an ajax upload script that returns an image that should swap out
I have an existing site that requires a background image on the home page.
I have existing Linux shared object file (shared library) which has been stripped. I
I have existing code with their own makefiles which I want to load into
I have an existing working iPhone project inside XCode 4.2. I localized an image
I have an image upload facility in my asp.net project, when uploaded a thumbnail
After some hardwork I have some code that uploads an image to Facebook, but
I have a function that loads all image files it finds in the wordpress
I'm currently modifying an existing module from being able to upload one image to

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.