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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:17:18+00:00 2026-05-30T11:17:18+00:00

Well I have a small problem. My script goes through a directory based on

  • 0

Well I have a small problem. My script goes through a directory based on url query. While doing this it creates thumbnails for those images. Now the problem is that it creates PNG thumbnails but instead of transparent or at-least white background it puts in standard black.

Here is the code can any one point out my issue in the code, or possibly something I may have missed

<?php

$folder = $_GET['folder']; //POST if from form, GET if from URL
$thisdir = getcwd();
if(!file_exists($thisdir ."/"."$folder/thumbs")) {
    mkdir($thisdir ."/"."$folder/thumbs" , 0777);
}

function returnimages($dirname="") {

    $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
    $handle  = opendir($dirname);
    while(false !== ($filename = readdir($handle))) {
        if(eregi($pattern, $filename)){ //if this file is a valid image
            $files[] = $filename;
        }
    }
    if (count($files)<>0) {
        sort($files);
    }

    $curimage=0;

    while($curimage !== count($files)){
        $cropfile=$dirname.'/'.$files[$curimage];echo '<br>'.$cropfile;

         if(preg_match('/[.](jpg)|(jpeg)$/', $cropfile)) {
             $source_img = imagecreatefromjpeg($cropfile);
         } elseif(preg_match('/[.](png)$/', $cropfile)) {
             imagealphablending($source_img, false);
             imagesavealpha($source_img, true);
             $source_img = imagecreatefrompng($cropfile);
         } elseif(preg_match('/[.](gif)$/', $cropfile)) {
             $source_img = imagecreatefromgif($cropfile);
         } else {
             echo "Code 43: Unable to read file type.";
             exit(0);
         }

        if (!$source_img) {
            echo "could not create image handle";
            exit(0);
        }
        $new_w = 480;
        $new_h = 480;

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

        $w_ratio = ($new_w / $orig_w);
        $h_ratio = ($new_h / $orig_h);

        if ($orig_w > $orig_h ) {//landscape from here new
            $crop_w = round($orig_w * $h_ratio);
            $crop_h = $new_h;
            $src_x = ceil( ( $orig_w - $orig_h ) / 2 );
            $src_y = 0;
        } elseif ($orig_w < $orig_h ) {//portrait
            $crop_h = round($orig_h * $w_ratio);
            $crop_w = $new_w;
            $src_x = 0;
            $src_y = ceil( ( $orig_h - $orig_w ) / 2 );
        } else {//square
            $crop_w = $new_w;
            $crop_h = $new_h;
            $src_x = 0;
            $src_y = 0;
        }
        $dest_img = imagecreatetruecolor($new_w,$new_h);
        imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h); //till here
        if(imagejpeg($dest_img, $dirname."/thumbs/".$files[$curimage], 80)) {
            imagedestroy($dest_img);
            imagedestroy($source_img);
        } else {
            echo "could not make thumbnail image";
            exit(0);
        }
        $curimage++;
    }
}
returnimages($name=$folder);

?>
  • 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-30T11:17:19+00:00Added an answer on May 30, 2026 at 11:17 am

    This is because you’re using imagejpeg as an output function for every possible type of images. I changed your code in a while loop to make it work:

    // Add these defines somewhere at the top
    define('TYPE_JPEG', 0);
    define('TYPE_PNG', 1);
    define('TYPE_GIF', 2);
    
    // ... skipping some code. Here goes while loop iterating over every image
    while($curimage !== count($files)){
        $cropfile=$dirname.'/'.$files[$curimage];echo '<br>'.$cropfile;
    
         // Determine type of the image
         if(preg_match('/[.](jpg)|(jpeg)$/', $cropfile)) {
             $source_img = imagecreatefromjpeg($cropfile);
             $type = TYPE_JPEG;
         } elseif(preg_match('/[.](png)$/', $cropfile)) {
             $source_img = imagecreatefrompng($cropfile);
             $type = TYPE_PNG;
         } elseif(preg_match('/[.](gif)$/', $cropfile)) {
             $source_img = imagecreatefromgif($cropfile);
             $type = TYPE_GIF;
         } else {
             echo "Code 43: Unable to read file type.";
             exit(0);
         }
    
        if (!$source_img) {
            echo "could not create image handle";
            exit(0);
        }
        $new_w = 480;
        $new_h = 480;
    
        $orig_w = imagesx($source_img);
        $orig_h = imagesy($source_img);
    
        $w_ratio = ($new_w / $orig_w);
        $h_ratio = ($new_h / $orig_h);
    
        if ($orig_w > $orig_h ) {//landscape from here new
            $crop_w = round($orig_w * $h_ratio);
            $crop_h = $new_h;
            $src_x = ceil( ( $orig_w - $orig_h ) / 2 );
            $src_y = 0;
        } elseif ($orig_w < $orig_h ) {//portrait
            $crop_h = round($orig_h * $w_ratio);
            $crop_w = $new_w;
            $src_x = 0;
            $src_y = ceil( ( $orig_h - $orig_w ) / 2 );
        } else {//square
            $crop_w = $new_w;
            $crop_h = $new_h;
            $src_x = 0;
            $src_y = 0;
        }
        $dest_img = imagecreatetruecolor($new_w,$new_h);
        $dest_path = $dirname."/thumbs/".$files[$curimage];
        switch ($type) {
            case TYPE_JPEG:
                imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
                $success = imagejpeg($dest_img, $dest_path, 80);
                break;
    
            case TYPE_PNG:
                // Preserve alpha
                imagesavealpha($dest_img, true);
                // Create transparent color
                $color = imagecolorallocatealpha($dest_img, 0, 0, 0, 127);
                // Fill in background
                imagefill($dest_img, 0, 0, $color);
                // Copy from source
                imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
                $success = imagepng($dest_img, $dest_path);
                break;
    
            default:
                $black = imagecolorallocate($dest_img, 0, 0, 0);
                // This will make the background transparent
                imagecolortransparent($dest_img, $black);
                // Copy from source
                imagecopyresampled($dest_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
                $success = imagegif($dest_img, $dest_path);
                break;
        }
        if($success) {
            imagedestroy($dest_img);
            imagedestroy($source_img);
        } else {
            echo "could not make thumbnail image";
            exit(0);
        }
        $curimage++;
    }
    

    As you can see, depending on the filetype we’re using different output functions: imagejpeg, imagepng and imagegif. PNG and GIF files require some extra actions in order to preserve transparency. I’ve tested it on PHP 5.2.13 under Windows using JPEG, GIF and PNG (transparent and regular) files and seems like everything is working. You can tweak additional parameters, for example imagepng takes optional $quality and $filters parameters.

    Also, regarding the code:

    • The most important rule: do not trust user input. You’re creating directories depending on the value of $_GET, this may be unsafe, because user can pass something like ‘/../../../../etc’
    • Some cleanup won’t hurt. Use foreach instead of while with loop counter. I think glob can retrieve list of files. And returnimages($name=$folder); call doesn’t make sense to me. Do you really need to assign and call in one line?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that loops through many url based XML files to compare
Well, I have a really small problem. How can I access an ASP.NET Drop
So I keep having this small problem where I have something like func ::
Well, my problem is what the title says. I have build a small application
Well. I have done a small project. But the problem is some of my
I have a small web application with ASP.NET AJAX running well under the Cassini
well i have this messages table with sample values like these: msg_id recipient_id read
Well I have this MySQL stored procedure that I wrote and if I run
well i have most probably an extremly stupid problem but could not figure it
I have a small problem, but I'm a bit stumped. I am using 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.