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

  • Home
  • SEARCH
  • 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 7173475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:49:53+00:00 2026-05-28T15:49:53+00:00

im trying to complete this function, it seems to be working great except for

  • 0

im trying to complete this function, it seems to be working great except for 1 small problem – it seems to position the image too far to the left and then fills the rest of the space with black.

what i’m trying to do is get this function to resize the image to the specified $thumb_w, and if the height ends up being bigger than then $thumb_h after being resized, it just crops out the bottom.

heres my function code:

function resize_upload ($tmp, $thumb_w, $thumb_h, $img_name, $img_ext, $img_path)
{
    if ($img_ext == 'jpg' || $img_ext == 'jpeg' || $img_ext == 'png' || $img_ext == 'gif')
    {
        if ($img_ext == 'jpg' || $img_ext == 'jpeg')
            $source_img = imagecreatefromjpeg($tmp);
        else if ($img_ext=='png')
            $source_img = imagecreatefrompng($tmp);
        else 
            $source_img = imagecreatefromgif($tmp);

        $orig_w = imagesx($source_img);
        $orig_h = imagesy($source_img);

        $w_ratio = ($thumb_w / $orig_w);
        $h_ratio = ($thumb_h / $orig_h);

        if ($orig_w > $orig_h ) 
        {
            $crop_w = round($orig_w * $h_ratio);
            $crop_h = $thumb_h;
            $src_x = ceil( ( $orig_w - $thumb_w ) / 2 );
            $src_y = 0;
        } 
        elseif ($orig_w < $orig_h ) 
        {
            $crop_h = round($orig_h * $w_ratio);
            $crop_w = $thumb_w;
            $src_x = 0;
            $src_y = ceil( ( $orig_h - $thumb_h ) / 2 );
        } 
        else 
        {
            $crop_w = $thumb_w;
            $crop_h = $thumb_h;
            $src_x = 0;
            $src_y = 0;
        }

        $thumb_img = imagecreatetruecolor($thumb_w,$thumb_h);

        imagecopyresampled($thumb_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);

        imagejpeg($thumb_img, $img_path.'/'.$img_name.'.'.$img_ext, 100);

        imagedestroy($thumb_img);
        imagedestroy($source_img);
    }
}

heres how i call the function:

resize_upload ($_FILES['image_main']['tmp_name'], 556, 346, $img_name, $img_ext, '../wp-content/themes/my-theme/images/projects');

heres what the image ends up looking like after the function does its thing:

enter image description here

see the black on the right-hand side? its probably some math problem i can’t figure out. any help would be greatly appreciated.

  • 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-28T15:49:55+00:00Added an answer on May 28, 2026 at 3:49 pm

    Using the same variables from your post $thumb_w = 556, $thumb_h = 346, let’s assume that the image that was sent in was the exact same dimensions, and thus wouldn’t need resized (556×346).

        $orig_w = 556;
        $orig_h = 346;
    
        $w_ratio = 1;
        $h_ratio = 1;
    
        if (556 > 346) //true
        {
            $crop_w = round(556 * 1); // 556
            $crop_h = 346;
            $src_x = ceil( ( 556 - 346 ) / 2 ); // ceil( 210 / 2 ) == 105;
            $src_y = 0;
        } 
    
        ...
    
        $thumb_img = imagecreatetruecolor(556, 346);
    
        imagecopyresampled($thumb_img, $source_img, 0, 0, 105, 0, 556, 346, 556, 346);
    

    So your code is starting at x = 105 of your source image, and attempting to go 556 pixels to the right of that, but only 451 pixels exist past that point. So if I had sent in a 556×346 image, it would copy the width of pixels 105 through 556 for the horizontal part of the image, and then 0 through 346 for the vertical. Thus, the entire vertical piece of the image shows up, but not all of the width.

    I’m sure if we did these same calculations with an image which had a greater height than width, we’d have the same problem with it having black space at the bottom of the image instead.

    Tip: When writing formulas and other things that require a lot of calculations, go through them with the simplest numbers first. If those don’t work, you obviously did something wrong.

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

Sidebar

Related Questions

I trying to complete this exercise; Write a Lisp function that takes as input
I am trying to do this: List<Parent> test = new List<Child>(); The complete code
im trying to getting auto complete working and i can do so fine when
I'm trying to add a function in my jQuery script that constructs a complete
I have been trying to days now to get this website completed ... however,
I trying to complete a RPG game for a project, but have no idea
I'm trying to complete a practice question from a book on generics but the
I am currently trying to complete a project using Qt4 and C++. I am
I'm trying to complete a regular expression that will pull out matches based on
Hi I am am trying to complete an assignment, where it is ok 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.