我使用此脚本让用户在我的网站上传图片。但是我想保存带有用户ID的图像,没有扩展名为png,gif或jpeg,而不是图像的原始名称。
$id_user = $_SESSION['user_id'];
if(isset($_FILES['file']))
{
$max_size = 500; //max image size in Pixels
$destination_folder = '../images/users/';
$image_name = $_FILES['file']['name']; //file name
$image_size = $_FILES['file']['size']; //file size
$image_temp = $_FILES['file']['tmp_name']; //file temp
$image_type = $_FILES['file']['type']; //file type
switch(strtolower($image_type)) //determine uploaded image type
{
//Create new image from file
case 'image/png':
$image_resource = imagecreatefrompng($image_temp);
break;
case 'image/gif':
$image_resource = imagecreatefromgif($image_temp);
break;
case 'image/jpeg': case 'image/pjpeg':
$image_resource = imagecreatefromjpeg($image_temp);
break;
default:
$image_resource = false;
}
if($image_resource){
//Copy and resize part of an image with resampling
list($img_width, $img_height) = getimagesize($image_temp);
//Construct a proportional size of new image
$image_scale = min($max_size / $img_width, $max_size / $img_height);
$new_image_width = ceil($image_scale * $img_width);
$new_image_height = ceil($image_scale * $img_height);
$new_canvas = imagecreatetruecolor($new_image_width , $new_image_height);
//resize image with new height and width
if(imagecopyresampled($new_canvas, $image_resource ,0, 0, 0, 0, $new_image_width, $new_image_height, $img_width, $img_height))
{
if(!is_dir($destination_folder)){ //create dir if it doesn't exist
echo (mkdir($destination_folder))?'New folder Created':'Could create new folder';
}
//Save as jpeg image file
switch(strtolower($image_type))
{
case 'image/png':
imagepng($new_canvas, $destination_folder.'/'.$image_name);
break;
case 'image/gif':
imagegif($new_canvas, $destination_folder.'/'.$image_name);
break;
case 'image/jpeg': case 'image/pjpeg':
imagejpeg($new_canvas, $destination_folder.'/'.$image_name, 80);
break;
}
}
}
}
?>
如何使用imagepng(),imagegif()或imagejpeg()函数执行此操作?