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

时间:2015-12-12 23:46:07

标签: 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";
   }
}
?>

1 个答案:

答案 0 :(得分:0)

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