我在webby上发现了一个免费的图片上传脚本,但它只允许JPG / JPEG格式。
如何允许它上传PNG和BMP格式?
for($i=0;$i<count($_FILES["fileUpload"]["name"]);$i++)
{
if(trim($_FILES["fileUpload"]["tmp_name"][$i]) != "")
{
$images = $_FILES["fileUpload"]["tmp_name"][$i];
$new_images = "thumbnails_".$_FILES["fileUpload"]["name"][$i];
copy($_FILES["fileUpload"]["tmp_name"][$i],"PIC_FOLDER".$_FILES["fileUpload"]["name"][$i]);
$width=100; //*** Fix Width & Heigh (Autu caculate) ***//
$size = GetimageSize($images);
$height = round($width*$size[1]/$size[0]);
$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,"PIC_FOLDER".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Resize Successful.<br>";
}
}
提前致谢
答案 0 :(得分:0)
正如评论所说,您已经能够上传任何类型,但您需要它才能成为jpeg才能制作缩略图。只需检查类型然后使用它打开图像
$src= $images;
$image_info = @getimagesize($src);
list($imgwidth,$imgheight,$image_type) = $image_info;
if( $image_type == IMAGETYPE_JPEG ) {
$images_orig = @imagecreatefromjpeg($src);
} else if( $image_type == IMAGETYPE_GIF ) {
$images_orig = @imagecreatefromgif($src);
} else if( $image_type == IMAGETYPE_PNG ) {
$images_orig = @imagecreatefrompng($src);
} else if( $image_type == IMAGETYPE_BMP ) {
$images_orig = @imagecreatefrombmp($src);
}
只需使用此代替$ images_orig = ImageCreateFromJPEG($ images);
如果您还希望以任何格式保存图像,请在底部执行类似检查而不是ImageJPEG($ images_fin,“PIC_FOLDER”。$ new_images);