处理上传器并为其添加一个switch语句,将任何图像.png,.gif,.bmp转换为jpg。
但它似乎不起作用,是否有人能够解释为什么它通过交换机的问题并且stil将文件格式保持为.gif或png?
<?php
if (isset($_POST['addpart']))
{
$image = $_FILES['images']['tmp_name'];
$name = $_POST['username'];
$i = 0;
foreach($image as $key)
{
$fileData = pathinfo(basename($_FILES["images"]["name"][$i]));
$fileName[] = $name . '_' . uniqid() . '.' . $fileData['extension'];
move_uploaded_file($key, "image/" . end($fileName));
copy("image/" . end($fileName) , "image_thumbnail/" . end($fileName));
$images = "image_thumbnail/" . end($fileName);
$new_images = "image_thumbnail/" . end($fileName);
$width = 100; //*** Fix Width & Heigh (Autu caculate) ***//
$size = GetimageSize($images);
$height = round($width * $size[1] / $size[0]);
switch ($size[2])
{
case IMAGETYPE_GIF:
$images_orig = imagecreatefromgif($images);
break;
case IMAGETYPE_JPEG:
$images_orig = imagecreatefromjpeg($images);
break;
case IMAGETYPE_PNG:
$images_orig = imagecreatefrompng($images);
break;
default:
die("Unknown filetype");
}
//$images_orig = imagecreatefromjpeg($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY);
ImageJPEG($images_fin, $new_images);
$i++;
}
echo 'Uploaded<br />';
echo 'Main Image - ' . $fileName[0] . '<br />';
echo 'Extra Image 1 - ' . $fileName[1] . '<br />';
echo 'Extra Image 2 - ' . $fileName[2] . '<br />';
echo '<hr>';
}
?>
答案 0 :(得分:1)
感谢大家对此上传者提供的所有帮助。
功能
这是最终的代码,对于任何想要将其分开的人来说,它都是一个有用的片段。
<?php
if (isset($_POST['addpart'])) {
$image = $_FILES['images']['tmp_name'];
$name = $_POST['username'];
$i = 0;
$i2 = 1;
$id = uniqid();
foreach ($image as $key) {
$fileData = pathinfo(basename($_FILES["images"]["name"][$i]));
$fileName[] = $name . '_' . $id . '_' . $i2 . '.' . $fileData['name'] . 'jpg';
move_uploaded_file($key, "image/" . end($fileName));
$images = "image/" . end($fileName);
$new_images = "image_thumbnail/" . end($fileName);
$width = 100; //*** Fix Width & Heigh (Autu caculate) ***//
$size = GetimageSize($images);
$height = round($width * $size[1] / $size[0]);
switch ($size[2]) {
case IMAGETYPE_GIF:
$images_orig = imagecreatefromgif($images);
break;
case IMAGETYPE_JPEG:
$images_orig = imagecreatefromjpeg($images);
break;
case IMAGETYPE_PNG:
$images_orig = imagecreatefrompng($images);
break;
default:
die("Unknown filetype");
}
//$images_orig = imagecreatefromjpeg($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY);
ImageJPEG($images_fin, $new_images);
$i++;
$i2++;
}
echo 'Uploaded<br />';
echo 'Main Image - ' . $fileName[0] . '<br />';
echo 'Extra Image 1 - ' . $fileName[1] . '<br />';
echo 'Extra Image 2 - ' . $fileName[2] . '<br />';
echo '<hr>';
}
?>
<form action="" method="post" enctype="multipart/form-data" id="form">
username - <input name="username" type="text" id="username" size="50" /><br>
Main Image - <input name="images[]" type="file" id="images[]" size="50" accept="image/jpeg" /><br>
Extra Image 1 - <input name="images[]" type="file" id="images[]" size="50" accept="image/jpeg" /><br>
Extra Image 2 - <input name="images[]" type="file" id="images[]" size="50" accept="image/jpeg" /><br>
<input type="submit" name="addpart" value="Upload" /><br>
</form>