如何在php中为上传的拇指图像创建白色背景色


How can i create white background color to an uploaded thumb image in php?

我正在尝试从上传的图像创建一个拇指图像,我想将缩略图背景颜色设置为白色。。。当上传的图片bg是黑色的。。。我怎样才能把它变成白色?

这是HTML

<!DOCTYPE HTML>
 <html>
 <head>
<title>PHP upload test</title>
 </head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
 <input type="hidden" name="MAX_FILE_SIZE" value="">
Select file:<input type="file" name="image" id="image" >
<p>
<input type="submit" value="Upload" name="submit">
</p>
</form>
</body>
 </html>

以下是php代码:

<?php
$destination="C:/wamp/www/upload-images/images/";
$thumb_recipient ="C:/wamp/www/upload-images/images/thumbs/";
$thumb_size = 100; 
$acceptedMIME= array('image/jpeg', 'image/pjpeg');
if (isset($_POST['submit'])) {
if (is_dir($destination) || is_writable($destination)) { // Check the recipient folder exist
$image = $_FILES['image']['tmp_name'];
$img_name = $_FILES['image']['name'];
$img_type = $_FILES['image']['type'];
if (is_file($image) && is_readable($image)) {
$details = getimagesize($image);
if (is_array($details)) {
$imgOriginalWidth = $details[0];
$imgOriginalHeigth = $details[1];
if ($imgOriginalWidth <= $thumb_size && $imgOriginalHeigth <= $thumb_size) {
$ratio =1;
}
 elseif ($imgOriginalWidth > $imgOriginalHeigth) {
 $ratio = $thumb_size / $imgOriginalWidth;
 }
   else
   {
    $ratio = $thumb_size / $imgOriginalHeigth;
   }
$thumbHeigth = round($imgOriginalWidth * $ratio);
$thumbWidth  = round($imgOriginalHeigth * $ratio);
$imageName = preg_replace($extensions, '', $img_name);
$ext = pathinfo($img_name, PATHINFO_EXTENSION);
$thumb = imagecreatetruecolor($thumbWidth, $thumbWidth);
if ($ext == 'jpg') {
$resource = imagecreatefromjpeg($image);
imagecopyresampled($thumb, $resource, 0, 0, 0, 0, $thumbWidth, $thumbHeigth, $imgOriginalWidth, $imgOriginalHeigth);
$imgthumbname = $imageName.'.jpeg';
$success = imagejpeg($thumb, $thumb_recipient . $imgthumbname, 100);
 if ($success) {
  echo "Successful";
 }
imagedestroy($thumb); 
}
}
  else
  {
    echo "File is invalid..it is not an array";
  }
}
  else{
    echo "Uploaded file cannot be open";
  }
}
   else
   {
    echo "The directory folder is not valid";
   }
}
?>

您可以使用http://php.net/manual/en/function.imagefill.php从背景的任何一点开始-可能0,0应该是好的。(如果找不到功能,请检查是否打开了GD扩展。)