Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1750 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Looking for transparency help

Thread title: Looking for transparency help
Closed Thread    
    Thread tools Search this thread Display Modes  
08-03-2008, 08:21 PM
#1
Jako is offline Jako
Jako's Avatar
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
 
Posts: 2,199
iTrader: 3 / 100%
 

Jako is on a distinguished road

  Old  Looking for transparency help

I have a script I kind of tweaked which would resize my images, jpg, gif, or pngs. The script uploads a copy of the source and then a resized thumbnail.

Only issue is that the thumbnail of a png has a black background and I cant figure out how to keep the original transparency.

PHP Code:
<?php

  
########################################
  # include the getNextFilename function #
  ########################################
  
include('getNextFilename5.php'); // use getNextFilename4.php on a PHP 4 server
  ########################################
  
  // define constants
  
define('THUMBS_DIR''/home/golden/public_html/images/clubs/thumbs/');
  
define('MAX_WIDTH'120);
  
define('MAX_HEIGHT'90);
  
  
// process the uploaded image
  
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
    
$original $_FILES['image']['tmp_name'];
    
// begin by getting the details of the original
    
list($width$height$type) = getimagesize($original);
    
// calculate the scaling ratio
    
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
      
$ratio 1;
      }
    elseif (
$width $height) {
      
$ratio MAX_WIDTH/$width;
      }
    else {
      
$ratio MAX_HEIGHT/$height;
      }
    
// strip the extension off the image filename
    
$imagetypes = array('/\.gif$/''/\.jpg$/''/\.jpeg$/''/\.png$/');
    
$name preg_replace($imagetypes''basename($_FILES['image']['name']));

    
########################################################
    # getNextFilename() requires the file type as a string #
    # so begin by converting it to a string assigned to $t #
    ########################################################
    
switch($type) {
      case 
1:
        
$t 'gif';
        break;
      case 
2:
        
$t 'jpg';
        break;
      case 
3:
        
$t 'png';
      }
    
################################
    # get new name for upload file #
    ################################
    
$newName getNextFilename5(UPLOAD_DIR$name$t);  // use getNextFilename4() on a PHP 4 server

    ################################################
    # use the new name instead of the existing one #
    ################################################
    // move the temporary file to the upload folder
    
$moved move_uploaded_file($originalUPLOAD_DIR.$newName);
    if (
$moved) {
      
$result "$newName successfully uploaded; ";
      
$original UPLOAD_DIR.$newName;
      }
    else {
      
$result 'Problem uploading '.$_FILES['image']['name'].'; ';
      }
    
################################################

    // create an image resource for the original
    
switch($type) {
      case 
1:
        
$source = @ imagecreatefromgif($original);
        if (!
$source) {
          
$result 'Cannot process GIF files. Please use JPEG or PNG.';
          }
        break;
      case 
2:
        
$source imagecreatefromjpeg($original);
        break;
      case 
3:
        
$source imagecreatefrompng($original);
        break;
      default:
        
$source NULL;
        
$result 'Cannot identify file type.';
      }
    
// make sure the image resource is OK
    
if (!$source) {
      
$result 'Problem copying original';
      }
    else {
      
// calculate the dimensions of the thumbnail
      
$thumb_width round($width $ratio);
      
$thumb_height round($height $ratio);
      
// create an image resource for the thumbnail
      
$thumb imagecreatetruecolor($thumb_width$thumb_height);
      
// create the resized copy
      
imagecopyresampled($thumb$source0000$thumb_width$thumb_height$width$height);
      
// save the resized copy
      
switch($type) {
        case 
1:
          
######################################################
          # use the new name as the basis for the thumb's name #
          ######################################################
          
$name basename($newName'.gif');
          
######################################################
          
if (function_exists('imagegif')) {
            
$success imagegif($thumbTHUMBS_DIR.$name.'_thb.gif');
            
$thumb_name $name.'_thb.gif';
            }
          else {
            
$success imagejpeg($thumbTHUMBS_DIR.$name.'_thb.jpg'50);
            
$thumb_name $name.'_thb.jpg';
            }
          break;
        case 
2:
          
######################################################
          # use the new name as the basis for the thumb's name #
          ######################################################
          
$name basename($newName'.jpg');
          
######################################################
          
$success imagejpeg($thumbTHUMBS_DIR.$name.'_thb.jpg'100);
          
$thumb_name $name.'_thb.jpg';
          break;
        case 
3:
          
######################################################
          # use the new name as the basis for the thumb's name #
          ######################################################
          
$name basename($newName'.png');
          
######################################################
          
$success imagepng($thumbTHUMBS_DIR.$name.'_thb.png');
          
$thumb_name $name.'_thb.png';
        }
        if (
$success) {
          
$result .= "$thumb_name created";
          }
        else {
          
$result .= 'Problem creating thumbnail';
          }
      
// remove the image resources from memory
      
imagedestroy($source);
      
imagedestroy($thumb);
      }
    }
?>

08-04-2008, 04:00 PM
#2
Dark_Prince11 is offline Dark_Prince11
Status: Member
Join date: Apr 2007
Location: Deer Park, NY
Expertise:
Software:
 
Posts: 123
iTrader: 0 / 0%
 

Dark_Prince11 is on a distinguished road

Send a message via AIM to Dark_Prince11 Send a message via MSN to Dark_Prince11

  Old

Originally Posted by Jako View Post
I have a script I kind of tweaked which would resize my images, jpg, gif, or pngs. The script uploads a copy of the source and then a resized thumbnail.

Only issue is that the thumbnail of a png has a black background and I cant figure out how to keep the original transparency.

PHP Code:
<?php

  
########################################
  # include the getNextFilename function #
  ########################################
  
include('getNextFilename5.php'); // use getNextFilename4.php on a PHP 4 server
  ########################################
  
  // define constants
  
define('THUMBS_DIR''/home/golden/public_html/images/clubs/thumbs/');
  
define('MAX_WIDTH'120);
  
define('MAX_HEIGHT'90);
  
  
// process the uploaded image
  
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
    
$original $_FILES['image']['tmp_name'];
    
// begin by getting the details of the original
    
list($width$height$type) = getimagesize($original);
    
// calculate the scaling ratio
    
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
      
$ratio 1;
      }
    elseif (
$width $height) {
      
$ratio MAX_WIDTH/$width;
      }
    else {
      
$ratio MAX_HEIGHT/$height;
      }
    
// strip the extension off the image filename
    
$imagetypes = array('/\.gif$/''/\.jpg$/''/\.jpeg$/''/\.png$/');
    
$name preg_replace($imagetypes''basename($_FILES['image']['name']));

    
########################################################
    # getNextFilename() requires the file type as a string #
    # so begin by converting it to a string assigned to $t #
    ########################################################
    
switch($type) {
      case 
1:
        
$t 'gif';
        break;
      case 
2:
        
$t 'jpg';
        break;
      case 
3:
        
$t 'png';
      }
    
################################
    # get new name for upload file #
    ################################
    
$newName getNextFilename5(UPLOAD_DIR$name$t);  // use getNextFilename4() on a PHP 4 server

    ################################################
    # use the new name instead of the existing one #
    ################################################
    // move the temporary file to the upload folder
    
$moved move_uploaded_file($originalUPLOAD_DIR.$newName);
    if (
$moved) {
      
$result "$newName successfully uploaded; ";
      
$original UPLOAD_DIR.$newName;
      }
    else {
      
$result 'Problem uploading '.$_FILES['image']['name'].'; ';
      }
    
################################################

    // create an image resource for the original
    
switch($type) {
      case 
1:
        
$source = @ imagecreatefromgif($original);
        if (!
$source) {
          
$result 'Cannot process GIF files. Please use JPEG or PNG.';
          }
        break;
      case 
2:
        
$source imagecreatefromjpeg($original);
        break;
      case 
3:
        
$source imagecreatefrompng($original);
        break;
      default:
        
$source NULL;
        
$result 'Cannot identify file type.';
      }
    
// make sure the image resource is OK
    
if (!$source) {
      
$result 'Problem copying original';
      }
    else {
      
// calculate the dimensions of the thumbnail
      
$thumb_width round($width $ratio);
      
$thumb_height round($height $ratio);
      
// create an image resource for the thumbnail
      
$thumb imagecreatetruecolor($thumb_width$thumb_height);
      
// create the resized copy
      
imagecopyresampled($thumb$source0000$thumb_width$thumb_height$width$height);
      
// save the resized copy
      
switch($type) {
        case 
1:
          
######################################################
          # use the new name as the basis for the thumb's name #
          ######################################################
          
$name basename($newName'.gif');
          
######################################################
          
if (function_exists('imagegif')) {
            
$success imagegif($thumbTHUMBS_DIR.$name.'_thb.gif');
            
$thumb_name $name.'_thb.gif';
            }
          else {
            
$success imagejpeg($thumbTHUMBS_DIR.$name.'_thb.jpg'50);
            
$thumb_name $name.'_thb.jpg';
            }
          break;
        case 
2:
          
######################################################
          # use the new name as the basis for the thumb's name #
          ######################################################
          
$name basename($newName'.jpg');
          
######################################################
          
$success imagejpeg($thumbTHUMBS_DIR.$name.'_thb.jpg'100);
          
$thumb_name $name.'_thb.jpg';
          break;
        case 
3:
          
######################################################
          # use the new name as the basis for the thumb's name #
          ######################################################
          
$name basename($newName'.png');
          
######################################################
          
$success imagepng($thumbTHUMBS_DIR.$name.'_thb.png');
          
$thumb_name $name.'_thb.png';
        }
        if (
$success) {
          
$result .= "$thumb_name created";
          }
        else {
          
$result .= 'Problem creating thumbnail';
          }
      
// remove the image resources from memory
      
imagedestroy($source);
      
imagedestroy($thumb);
      }
    }
?>
I handled a job like this a long time ago. I believe that you have to set imagesavealpha to true.

Sort of like this.
PHP Code:
// create an image resource for the thumbnail
$thumb imagecreatetruecolor($thumb_width$thumb_height);
// Set Alpha Blending To True
$isa_result imagesavealpha($thumb,true); 

08-04-2008, 06:34 PM
#3
Jako is offline Jako
Jako's Avatar
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
 
Posts: 2,199
iTrader: 3 / 100%
 

Jako is on a distinguished road

  Old

I ended up getting it to work using..

PHP Code:
  /* making the new image transparent */
        
$background imagecolorallocate($thumb000);
        
ImageColorTransparent($thumb$background); // make the new temp image all transparent
        
imagealphablending($thumbfalse); // turn off the alpha blending to keep the alpha channel


      // create the resized copy
      
imagecopyresampled($thumb$source0000$thumb_width$thumb_height$width$height); 
But the colors seem a tad lighter in the new thumbnail? Is that just due to the resizing and keeping transparency?

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed