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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:22:02+00:00 2026-06-12T01:22:02+00:00

I have this PHP script that receives an uploaded image. The uploaded image is

  • 0

I have this PHP script that receives an uploaded image. The uploaded image is saved in a temp folder, and then this script resamples the image and saves it to the correct folder. A user can upload either JPG, PNG or GIF files. This script only caters for JPG files though.

How would I modify this script to resize both PNG’s and GIF’s without losing transparency?

$targ_w = $targ_h = 150;
$jpeg_quality = 90;

$src = $_POST['n'];
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

$new_src = str_replace('/temp','',$_POST['n']);

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);

imagejpeg($dst_r,$new_src,$jpeg_quality);
  • 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-12T01:22:03+00:00Added an answer on June 12, 2026 at 1:22 am

    JPEG images can’t have transparent background.

    Instead you can make the image based on imagesavealpha():

    $targ_w = $targ_h = 150;
    $newImage = imagecreatetruecolor($targ_w, $targ_h);
    imagealphablending($newImage, false);
    imagesavealpha($newImage, true);
    $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
    imagefilledrectangle($newImage, 0, 0, $targ_w, $targ_h, $transparent);
    
    $src = $_POST['n'];
    $img_r = imagecreatefromstring(file_get_contents($src));
    $img_r_size = getimagesize($src);
    
    $width_r = $img_r_size[0];
    $height_r = $img_r_size[1];
    if($width_r > $height_r){
        $width_ratio = $targ_w / $width_r;
        $new_width   = $targ_w;
        $new_height  = $height_r * $width_ratio;
    } else {
        $height_ratio = $targ_h / $height_r;
        $new_width    = $width_r * $height_ratio;
        $new_height   = $targ_h;
    }
    
    imagecopyresampled($newImage, $img_r, 0, 0, 0, 0, $new_width, $new_height, $width_r, $height_r);
    
    $new_src = str_replace('/temp','',$_POST['n']);
    imagepng($newImage, $new_src);
    

    It will make a PNG from both PNG and GIF (that have transparent background, and resize to 150×150.

    This is just an example, as it does not constrain proportions.

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

Sidebar

Related Questions

I have a php script that randomly generates an image. Something like this: <?php
I have a line of php code that looks like this: echo <script>$('#edit_errors').html('<h3><em>Please Correct
I have this small PHP script that takes a POST variable and writes it
This is driving me crazy. I have a PHP script that gets some data
I have PHP scrip that goes like this: if ($cost_frm < $cost){ echo <script
I have a PHP script that receives a file and returns a response. The
I have a PHP script that receives uploads and it works fine in mamp,
I have this ajax_update script that updates file.php every 60 seconds.. Now file.php outputs
I have this ajax_update script that updates file.php every 60 seconds.. Now file.php outputs
I have an Apache server running a PHP script that receives data via a

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.