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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:42:42+00:00 2026-06-03T02:42:42+00:00

I have this function here….. function create_thumbnail($source,$destination, $thumb_width) { $size = getimagesize($source); $width =

  • 0

I have this function here…..

function create_thumbnail($source,$destination, $thumb_width) {
        $size = getimagesize($source);
        $width = $size[0];
        $height = $size[1];
        $x = 0;
        $y = 0;
        if($width> $height) {
            $x = ceil(($width - $height) / 2 );
            $width = $height;
        } elseif($height> $width) {
            $y = ceil(($height - $width) / 2);
            $height = $width;
        }
        $new_image = imagecreatetruecolor($thumb_width,$thumb_width)or die('Cannot Initialize new GD image stream');
        $extension = get_image_extension($source);
         if($extension=='jpg' || $extension=='jpeg') 
            $image = imagecreatefromjpeg($source); 
        if($extension=='gif') 
            $image = imagecreatefromgif($source); 
        if($extension=='png') 
            $image = imagecreatefrompng($source);   

        imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
        if($extension=='jpg' || $extension=='jpeg') 
           imagejpeg($new_image,$destination); 
        if($extension=='gif') 
            imagegif($new_image,$destination); 
        if($extension=='png') 
            imagepng($new_image,$destination); 
    }

And what this does it takes an image and resizes, but its not resizing the way I expected to, I was expecting it would take a wide image or a tall image or a normal size image and cut it off so it fits to that size, it does the resizing but it cuts off most of my images…I have been struggling with this for days and I cant seem to find a way to resize my images without cutting them off….I hope I can find some help and it would greatly appreciated…so, so tired….

For an example I have this image….

enter image description here

and when I ran that function for that image it returns this…

enter image description here

What I am expecting is that same image, just smaller.

I changed this part of my code…

imagecopyresampled($new_image,$image,0,0,0,0,$thumb_width,$thumb_width,$width,$height);

changed the $x and $y to ‘0’ and ‘0’ and this is what came up…

enter image description here

I close to what I am looking for but the full image is not there…it still gets cut off.

  • 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-03T02:42:44+00:00Added an answer on June 3, 2026 at 2:42 am

    For what you want you can use this function:

    function create_thumbnail($source, $destination, $thumbWidth)
    {
        $extension = get_image_extension($source);
        $size = getimagesize($source);
        $imageWidth  = $newWidth  = $size[0];
        $imageHeight = $newheight = $size[1];
    
        if ($imageWidth > $thumbWidth || $imageHeight > $thumbWidth)
        {
            $newWidth  = $newHeight = $thumbWidth;
        }
    
        $newImage = imagecreatetruecolor($newWidth, $newHeight);
    
        switch ($extension)
        {
            case 'jpeg':
            case 'jpg':
                $imageCreateFrom = 'imagecreatefromjpeg';
                $store = 'imagejpeg';
                break;
    
            case 'png':
                $imageCreateFrom = 'imagecreatefrompng';
                $store = 'imagepng';
                break;
    
            case 'gif':
                $imageCreateFrom = 'imagecreatefromgif';
                $store = 'imagegif';
                break;
    
            default:
                return false;
        }
    
        $container = $imageCreateFrom($source);
        imagecopyresampled($newImage, $container, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
        return $store($newImage, $destination);
    }
    var_dump(create_thumbnail('sample.jpg', 'sample_thumb_no_ratio.jpg', '255'));
    

    However if you want to preserve the image ratio, you could use something like this:

    function create_thumbnail_preserve_ratio($source, $destination, $thumbWidth)
    {
        $extension = get_image_extension($source);
        $size = getimagesize($source);
        $imageWidth  = $newWidth  = $size[0];
        $imageHeight = $newheight = $size[1];
    
        if ($imageWidth > $thumbWidth || $imageHeight > $thumbWidth)
        {
            // Calculate the ratio
            $xscale = ($imageWidth/$thumbWidth);
            $yscale = ($imageHeight/$thumbWidth);
            $newWidth  = ($yscale > $xscale) ? round($imageWidth * (1/$yscale)) : round($imageWidth * (1/$xscale));
            $newHeight = ($yscale > $xscale) ? round($imageHeight * (1/$yscale)) : round($imageHeight * (1/$xscale));
        }
    
        $newImage = imagecreatetruecolor($newWidth, $newHeight);
    
        switch ($extension)
        {
            case 'jpeg':
            case 'jpg':
                $imageCreateFrom = 'imagecreatefromjpeg';
                $store = 'imagejpeg';
                break;
    
            case 'png':
                $imageCreateFrom = 'imagecreatefrompng';
                $store = 'imagepng';
                break;
    
            case 'gif':
                $imageCreateFrom = 'imagecreatefromgif';
                $store = 'imagegif';
                break;
    
            default:
                return false;
        }
    
        $container = $imageCreateFrom($source);
        imagecopyresampled($newImage, $container, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
        return $store($newImage, $destination);
    }
    var_dump(create_thumbnail_preserve_ratio('sample.jpg', 'sample_thumb_with_ratio.jpg', '255'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
I have this code here: var Person = (function() { var name; var PersonConstructor
Here is the situation. I have some javascript that looks like this: function onSubmit()
Here's the story... I have a jQuery function that does something, this function is
I have this tail recursive function here: def recursive_function(n, sum): if n < 1:
I have this function that is set a width to my inputs and ads
So I have this function here: function ShowMessage() { var themessege = document.getElementById(Form).textarea1.value; var
I have this script here: function MainAssistant(argFromPusher) { } MainAssistant.prototype = { setup: function()
I have this code here: public class clsDataLayer { // This function saves the

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.