当.jpeg类型为时,无法将图像上载到服务器


Uploading image to server does not work when .jpeg is the type

我正在使用此脚本上传.jpg格式的图像。png类型到服务器和数据库,但当涉及到.jpeg或.JPG时,它不起作用,而不是将文件放在正确的扩展名目录中,它只是这样做/galleri/uploads/a4b7c7fb0de9c561110c2279f24ec820jpeg.php它会自动在末尾添加.php。

我一直在尝试添加这些行

if ( $type == 'image/jpeg' ) { $filetype = '.jpeg'; } else {  $filetype = str_replace( 'image/', '', $type ); }
if ( $type == 'image/jpeg' ) { $filetype = '.JPG'; } else {  $filetype = str_replace( 'image/', '', $type ); }

但毫无用处。。

除此之外,在这种情况下,我还有什么更好的裁剪工具可以使用吗?

这是完整的脚本:

if(isset($_POST['addmedia'])) {
    $mediatype =    escape(striptags($_POST['mediatype']));
    $title =        escape(striptags($_POST['title']));
    $video =        escape(striptags($_POST['medialink']));
    $date =         date('Y-m-d');
    if ($mediatype === 'img') {
        if( !isset( $_POST['p'] ) ) { $_POST['p']= 0; }
        if( $_POST['p'] == 1 ) {
            $name =     $_FILES['image']['name'];
            $temp =     $_FILES['image']['tmp_name'];
            $type =     $_FILES['image']['type'];
            $size =     $_FILES['image']['size'];
            if ( $type == 'image/jpeg' ) { $filetype = '.jpg'; } else {  $filetype = str_replace( 'image/', '', $type ); }
            if ( $type == 'image/png' ) { $filetype = '.png'; } else {  $filetype = str_replace( 'image/', '', $type ); }
            $path =             md5( rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000) ) . $filetype;
            $thumb_path =       md5( rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000) ) . $filetype;
            $size2  =   getimagesize ($temp);
            $width  =   $size2[0];
            $height =   $size2[1];
            $maxwidth   =   1281;
            $maxheight  =   751;
            $allowed    =   array('image/jpeg', 'image/png');
            if( in_array( $type, $allowed ) ) {
                if( $width < $maxwidth && $height < $maxheight) {
                    if( $size < 10485760) {
                        if( $width == $height ) { $case = 1;}   // Square form
                        if( $width > $height ) { $case = 2;}    // Lying form
                        if( $width < $height ) { $case = 3;}    // Standing form
                        switch($case) {
                            case 1:
                            $newwidth   =    280;
                            $newheight  =    150;
                            break;
                            case 2:
                            $newheight  =   150;
                            $ratio      =   $newheight / $height;
                            $newwidth   =   round($width * $ratio);
                            break;
                            case 3:
                            $newwidth   =   280;
                            $ratio      =   $newwidth / $width;
                            $newheight  =   round($height * $ratio);
                            break;
                        }
                        switch($type) {
                            case 'image/jpeg':
                            $img    =   imagecreatefromjpeg( $temp );
                            $thumb  =   imagecreatetruecolor( $newwidth, $newheight );
                            imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
                            imagejpeg( $thumb, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path );
                            break;
                            case 'image/png':
                            $img    =    imagecreatefrompng( $temp );
                            $thumb  =    imagecreatetruecolor( $newwidth, $newheight );
                            imagecopyresized( $thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
                            imagepng( $thumb, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path );
                            break;
                        }
                        move_uploaded_file( $temp, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/" . $path );
                        $addimg = "INSERT INTO uploads (`type`, `title`, `src`, `thumb`, `date`) VALUES ('$mediatype', '$title', '$path', '$thumb_path', '$date')";
                        if ($add_img = $db_connect->query($addimg)) {}
                        echo 'Din bild har laddats upp!';
                        header("Location: " . $_SERVER['HTTP_REFERER']);
                    } else {
                        echo '10MB';
                    }
                } else {
                    echo 'To big in size';
                }
            } else {
                echo '.jpg, .jpeg, .png!';
            }
        }
    } else if ($mediatype === 'vid') {
        $name       =     $_FILES['image']['name'];
        $temp       =     $_FILES['image']['tmp_name'];
        $size       =     $_FILES['image']['size'];
        $thumb_path =     md5( rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000) ) . '.jpg';
        move_uploaded_file( $temp, $_SERVER['DOCUMENT_ROOT'] . "/galleri/uploads/thumbs/" . $thumb_path );
        $addvid = "INSERT INTO uploads (`type`, `title`, `thumb`, `videolink`, `date`) VALUES ('$mediatype', '$title', '$thumb_path', '$video', '$date')";
        if ($add_vid = $db_connect->query($addvid)) {}
        echo 'Video uploaded';
        header("Location: " . $_SERVER['HTTP_REFERER']);
    }
}

试试这个:

<?php
    ...
    $name = $_FILES['image']['name'];
    $temp = $_FILES['image']['tmp_name'];
    $size = $_FILES['image']['size'];
    $type = image_type_to_mime_type(exif_imagetype($temp)); // get the real image mime type
    if ( $type == 'image/jpeg' ) { // jpeg
        $filetype = '.jpg'; 
    } else if ( $type == 'image/png' ){ // png
        $filetype = '.png';
    } else { // other image type
        $filetype = '.' . str_replace( 'image/', '', $type ); // to get .gif for a gif image, for example
    }
    ...
?>