5

I am using this code for resizing:

  <?php
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
           $w = $h * $scale_ratio;
    } else {
           $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif"){ 
      $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
      $img = imagecreatefrompng($target);
    } else { 
      $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    imagejpeg($tci, $newcopy, 80);
}
?>

But everytime I upload a transparant png image it turns out with a black background. How do I prevent this from happening and maintain the transparant background?

DaViDa7
  • 137
  • 1
  • 4
  • are you try this with jpg image or else wise png ? – chhameed Mar 26 '12 at 11:00
  • This SO question may help http://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using-phps-gdlib-imagecopyresample – Ben Mar 26 '12 at 11:02
  • Jpeg can not have transparent background. http://stackoverflow.com/questions/313070/png-transparency-with-php –  Apr 05 '12 at 09:17
  • Possible duplicate of [imagecreatefrompng() Makes a black background instead of transparent?](https://stackoverflow.com/questions/2611852/imagecreatefrompng-makes-a-black-background-instead-of-transparent) – davidkonrad Feb 11 '18 at 13:14

3 Answers3

6

try this before imagecopyresampled, added alpha blending, and then save alpa

imagealphablending($tci, false);
imagesavealpha($tci,true);
$transparent = imagecolorallocatealpha($tci, 255, 255, 255, 127);
imagefilledrectangle($tci, 0, 0, $w, $h, $transparent);
Muhammad Cahya
  • 288
  • 4
  • 13
0

Try this, for making your re-size function

<?php
    $file_name =testimg.jpg // your file name
    extension= explode(".", strtolower($file_name));//get ext of your image

    if($extension[1]=="jpg" || $extension[1]=="jpeg" )
    {
        $src = imagecreatefromjpeg($filename); // Check ext and set src 
    }
    else if($extension[2]=="png")
    {
        $src = imagecreatefrompng($filename); // Check ext and set src 
    }
    else 
    {
        $src = imagecreatefromgif($filename); // Check ext and set src 
    } 
    list($width,$height)=getimagesize($filename);
    $newwidth=round($width/10);   //resized image width
    $newheight=round($height/10);  //resized image height
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $resizedfile = "resizeduploads/". $name;
    if($extension[1]=="jpg" || $extension[1]=="jpeg" )
    {
        imagejpeg($tmp,$resizedfile);
    }
    else if($extension[1]=="png")
    {
        imagepng($tmp,$resizedfile);
    }
    else 
    {
        imagegif($tmp,$resizedfile);
    }
    imagedestroy($src);
    imagedestroy($tmp);
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
Saurabh
  • 148
  • 1
  • 10
0

Try setting the transparent color using imagecolortransparent:

http://il.php.net/manual/en/function.imagecolortransparent.php

Ynhockey
  • 3,845
  • 5
  • 33
  • 51