Reputation: 1864
So I am using this code to resize an image on the fly and create a copy of it.
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 what I don't like is it gives a default black background. How could I change this black background to something like, white?
Here it is:
Upvotes: 1
Views: 1544
Reputation:
After you initialize $tci
, you can use imagefill()
and imagecolorallocate()
to fill the image with white:
$white = imagecolorallocate($tci, 255, 255, 255);
imagefill($tci, 0, 0, $white);
Note: this will only work if the source image uses transparent pixels.
Upvotes: 2