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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:51:24+00:00 2026-06-07T09:51:24+00:00

We have some fairly old code which uploads, resizes and then saves an image

  • 0

We have some fairly old code which uploads, resizes and then saves an image so it can be used in our CMS.

It all works fine unless we upload a 24bit PNG, as for some reason its converting it to an 8bit PNG, anyway of fixing this?

Attached is the function in question:

function resize_png( $origfile,$newfile,$endh,$endw,$imagequal,$crop,$watermrkimg=false) {

    $imagequal = floor($imagequal/10);          //needed to give a quality of between 0-9 as required by php5 (php4 allowed 0-99)

$return_val = 1;
if (!$return_val = ( ($img = ImageCreateFromPNG ( $origfile )) && $return_val == 1 ) ? "1" : "0" ) {
    quit_on_error ("Your picture is corrupt, please try resaving it or uploading another picture","Image Error",'1');
}

// 1. get transparent colour
$colorTransparent = imagecolortransparent($img);

$origw = imagesx ($img);                                                            // Original image width
$origh = imagesy ($img);                                                            // Original image height

$ratiow = $origw / $endw;                                                   //get ratios of current dimension against min dimension
$ratioh = $origh / $endh;

if ($ratiow == $ratioh) {                                                       //if image is already correctly proportioned
    $neww = round($origw / $ratiow);
    $newh = round($origh / $ratioh);
    $offsetw = '0';
    $offseth = '0';
}
elseif ($ratioh < $ratiow) {                                                    // if image is wide

    if ($crop == '1')
    {
        $neww = round($origw / $ratioh);                                        // this will be too wide
        $newh = round($origh / $ratioh);                                        // this will be perfect
    }
    else {
        $neww = round($origw / $ratiow);                                        // this will be perfect
        $newh = round($origh / $ratiow);                                        // this will be to short
    }

    $offseth = '0';                                                             // as height perfect
    $offsetw = round(($neww - $endw) / 2);                                      // horizontally centred
}
else  {                                                                         // if image is tall
    if ($crop == '1')
    {
        $neww = round($origw / $ratiow);                                            // this will be perfect
        $newh = round($origh / $ratiow);                                            // this will be too tall
    }
    else
    {
        $neww = round($origw / $ratioh);                                            // this will be too thin
        $newh = round($origh / $ratioh);                                            // this will be perfect

    }
    $offsetw = '0';                                                             // as width perfect
    $offseth = round(($newh - $endh) / 2);                                      // vertically centred
}

if($crop != '1')            // If original is smaller then don't resize at all, then quality will be better
{                           // ... unless of course we are cropping for a thumbnail, then we want to be resized up ...
    if (($origw < $neww) && ($origh < $newh))
    {
         $neww = $origw;
        $newh = $origh;
    }
}

$resized_id = ImageCreate( $neww , $newh );                                     // create an image to resize the image proportionally

// 2. Set transparent colour
imagepalettecopy($resized_id, $img);
imagefill($resized_id,0,0,$colorTransparent);
imagecolortransparent($resized_id,$colorTransparent);

ImageCopyResampled( $resized_id, $img,                                          // resize image - no cropping, so may be too big in one dimension
                0,0,                                                            // dst x,y
                0,0,                                                            // src LR,UD
                $neww, $newh,
                $origw, $origh );

if ($crop == '1')
{
    $resized_cropped_id = ImageCreate( $endw , $endh );                             // create an image to crop the oversized dimension

    // 2. Set transparent colour for cropped image
    imagepalettecopy($resized_cropped_id, $img);
    imagefill($resized_cropped_id,0,0,$colorTransparent);
    imagecolortransparent($resized_cropped_id,$colorTransparent);

    ImageCopyResampled( $resized_cropped_id, $resized_id,                               // crop image - so right size
                    0,0,                                                            // dst x,y
                    $offsetw,$offseth,                                              // src LR,UD
                    $endw, $endh,
                    $endw, $endh);


    $return_val = ( $full = ImagePNG( $resized_cropped_id, $newfile, $imagequal )   // save jpeg to destination
                 && $return_val == 1 ) ? "1" : "0";
     ImageDestroy( $resized_cropped_id );
}
else
{
    $return_val = ( $full = ImagePNG( $resized_id, $newfile, $imagequal )   // save jpeg to destination
                 && $return_val == 1 ) ? "1" : "0";

}

ImageDestroy( $resized_id );                                                    // wipe memory for temp images
ImageDestroy( $img );

return ($return_val) ? TRUE : FALSE ;

}

Thanks guys I have now fixed it by using get_png_imageinfo & imagecreatetruecolor!

  • 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-07T09:51:26+00:00Added an answer on June 7, 2026 at 9:51 am

    imagecreate creates a pallette-based image, using a colour table with a maximum size of 256 colours.

    imagecreatetruecolor has no such limit, but of course comes with the downside of being many times bigger in filesize.

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

Sidebar

Related Questions

I have some fairly old code that runs just fine in Excel versions before
We have a couple of sites which have some memory problems and fairly often
I have a fairly simple layout with an image, and some textviews wrapped in
I have bunch of strings, some of which are fairly long, like so: movie.titles
I have some old C# plugin code that was implemented strictly with Reflection. In
I have some fairly straightforward code to open up files using a Process object:
I have some fairly complex routing rules, that are only achievable with custom code,
I have a fairly basic question regarding python lists/dictionaries which I would like some
There are some fairly powerful tools like SIMBL or Airfoil/Instant Hijack which use code
I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out

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.