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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:03:39+00:00 2026-05-21T23:03:39+00:00

Okey so i am writing a image upload where we want to force the

  • 0

Okey so i am writing a image upload where we want to force the images to be 795x440px. They can be rezised but they have to keep their aspect ratio so they can be cropped also.

The new images comes out in the right size, but the cropped image from original file has the wrong ratio, tried some diffrent ways but can’t get it right.

The image i am testing right now, original file

http://image.bayimg.com/jaiflaada.jpg

The result from cropping

http://image.bayimg.com/jaifmaada.jpg

How do i get this right, so the image always gets the best size and crops the rest?

list($width, $height) = getimagesize($save_dir);

$prop = (795 / $width);
$height = floor($height * $prop);


$new_image = imagecreatetruecolor(795, 440);

$bgColor = imagecolorallocate($new_image, 255,255,255) or die("Couldn't allocate color");
imagefill($new_image , 0,0 , $bgColor) or die("Couldnt fill with color");


imagecopyresampled($new_image,$source_image,0,0,0,0,795,440,795,$height);


imagejpeg($new_image,$new_directory,100);
  • 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-21T23:03:40+00:00Added an answer on May 21, 2026 at 11:03 pm

    I do it like that:

    public function cropImage($nw, $nh, $source, $stype, $dest) {
        list($w, $h) = getimagesize($source);
    
        switch($stype) {
            case 'gif':
                $simg = imagecreatefromgif($source);
            break;
            case 'jpg':
            case 'jpeg':
                $simg = imagecreatefromjpeg($source);
            break;
            case 'png':
                $simg = imagecreatefrompng($source);
            break;
        }
        $dimg = imagecreatetruecolor($nw, $nh);
        $white = imagecolorallocate($dimg, 255, 255, 255);
        imagefill($dimg, 1, 1, $white);
        $wm = $w/$nw;
        $hm = $h/$nh;
        $h_height = $nh/2;
        $w_height = $nw/2;
    
        if($w > $h) {
             $adjusted_width = $w / $hm;
             $half_width = $adjusted_width / 2;
             $int_width = $half_width - $w_height;
             imagecopyresampled($dimg, $simg, -$int_width, 0, 0, 0, $adjusted_width, $nh, $w, $h);
        } elseif(($w < $h) || ($w == $h)) {
             $adjusted_height = $h / $wm;
             $half_height = $adjusted_height / 2;
             $int_height = $half_height - $h_height;
             imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
        } else {
             imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
        }       
    
        if(imagejpeg($dimg, $dest, 70))
            return true;
        else
            die("cropImage: error.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okey. Here's what I want to do: I have a windows application that sorts
Okey, I've done all the business, followed all the steps, but still can't get
Okey so I want to add metadata for the search engines but I only
Okey i just dont understand what can be wrong here i have this app
Okey here is this Very good jquery slider. http://srobbin.com/jquery-plugins/pageslide and what i have done
Okey, so the question has probably been asked before, but I haven't found an
Okey, here it goes. I have a block of code that repeats it self
I have 2 fields that can be use in row area. One of them
I'm working on my project now but I have encountered some problems :/ My
Okey, this is my problem. I have one service class where Ive managed 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.