2011-12-14 3 views
1

Donc j'utilise ce code pour redimensionner une image à la volée et en créer une copie.image PHP redimensionner fond noir - changer de couleur

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); 
} 

Mais ce que je n'aime pas est-il donne une valeur par défaut fond noir. Comment pourrais-je changer ce noir arrière-plan à quelque chose comme, blanc?

Ici, il est:

enter image description here

Répondre

2

Après l'initialisation $tci, vous pouvez utiliser imagefill() et imagecolorallocate() pour remplir l'image avec le blanc:

$white = imagecolorallocate($tci, 255, 255, 255); 
imagefill($tci, 0, 0, $white); 

Note: cela ne fonctionnera que si le L'image source utilise des pixels transparents.

+0

Vous mon ami est une légende! – Frank

Questions connexes