Thumbs have black fill color

Discussion in 'PHP' started by detective, May 21, 2008.

  1. #1
    Hy, i have a little piece of code here wich creates thubnail from a jpg image and it keeps its aspect ratio, and the remaining space is filled with black, but the thing is i want to fill that space with transparent or white, cause black looks very ugly on my site.

    heres the piece of code, you can see i have tried with imagefill function but still doesnt work
    
    if(isset($_POST['Submit'])){ 
         $thumbsize = 90; // dimensiunea thumbnail-ului
    
         $filedir = 'pics/'; // directorul imaginii pe care o vei uploada
         $thumbdir = 'pics/'; // directorul imaginii thumbnail
         $prefix = 'small_'; // prefixul adaugat imaginii
    	 $width = 90;
    		$height = 90;
         $maxfile = '2000000'; 
         $mode = '0666'; 
          
         $userfile_name = $_FILES['image']['name']; 
         $userfile_tmp = $_FILES['image']['tmp_name']; 
         $userfile_size = $_FILES['image']['size']; 
         $userfile_type = $_FILES['image']['type']; 
          
         if (isset($_FILES['image']['name'])){ 
             $prod_img = $filedir.$userfile_name; 
             $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 
    
             move_uploaded_file($userfile_tmp, $prod_img); 
             chmod ($prod_img, octdec($mode)); 
    		 
    		 
    	
    		 
    		 // Get new dimensions
    	list($width_orig, $height_orig) = getimagesize($prod_img);
    
    	$ratio_orig = $width_orig/$height_orig;
    
    	if ($width/$height > $ratio_orig) {
       $width = $height*$ratio_orig;
    	} else {
       $height = $width/$ratio_orig;
    	}
    		 
    		 
             
    
             $destimg=ImageCreateTrueColor($thumbsize, $thumbsize) or die('Problem In Creeare thumbnail'); 
             $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem la deschiderea imaginii sursa'); 
    		 imagefill($destimg, 0, 0, $alb);
             ImageJPEG($destimg, null, 93) or die('Problema la salvare');
             ImageCopyResampled($destimg,$srcimg,-($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig) or die('Problem In resizing'); 
    		 $alb = imagecolorallocate($destimg, 255, 255, 255);
             imagedestroy($destimg); 
    
    Code (markup):
    I have attached a thumb so you can see the how it looks like now.

    By the way sorry for my bad english.
     

    Attached Files:

    detective, May 21, 2008 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have you tried converting them to PNGs? JPGs don't support transparency...
     
    Altari, May 21, 2008 IP
  3. dan.h

    dan.h Guest

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try putting $alb before the imagefill:

             $destimg=ImageCreateTrueColor($thumbsize, $thumbsize) or die('Problem In Creeare thumbnail'); 
             $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem la deschiderea imaginii sursa'); 
    		[b] $alb = imagecolorallocate($destimg, 255, 255, 255);
    		 imagefill($destimg, 0, 0, $alb);[/b]
             ImageJPEG($destimg, null, 93) or die('Problema la salvare');
             ImageCopyResampled($destimg,$srcimg,-($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig) or die('Problem In resizing'); 
             imagedestroy($destimg);
    
    Code (markup):
     
    dan.h, May 21, 2008 IP
  4. detective

    detective Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Nope it doesnt work, how can i convert them to png ? or other ways of filling with white or transparent ?
     
    detective, May 22, 2008 IP
  5. detective

    detective Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can anyone help me please ?
     
    detective, May 23, 2008 IP
  6. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #6
    before imagefill function add this line

    $alb= ImageColorAllocate($image, 255, 255, 255);

    Regards

    Alex
     
    kmap, May 23, 2008 IP