douqiaotong8682 2018-03-29 07:29
浏览 61
已采纳

仅将压缩图像保存到数据库

I am trying to save the compressed image to database as well as to the folder. Now both the images are getting saved. Here is my code

To compress image i used this code referring reduce image size while uploading using the following PHP code used to upload image

Here is my complete code

$k1 = mysqli_query($con, "select img_path from members where     mem_id='".$_SESSION['user_id']."'");
$k2 = mysqli_fetch_array($k1);
$dirname = "pics/".$_SESSION['user_id']."/"; 
$target = $dirname . basename( $_FILES['docs']['name']); 

function thumbnail( $img, $source, $dest, $maxw, $maxh ) {      
    $jpg = $source.$img;

if( $jpg ) {
    list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image
    $source = imagecreatefromjpeg( $jpg );

    if( $maxw >= $width && $maxh >= $height ) {
        $ratio = 1;
    }elseif( $width > $height ) {
        $ratio = $maxw / $width;
    }else {
        $ratio = $maxh / $height;
    }

    $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
    $thumb_height = round( $height * $ratio );

    $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

    imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

    $path = $dest.$img."_thumb.jpg"; // I want to save _thumb.php image
    imagejpeg( $thumb, $path, 75 );
}
imagedestroy( $thumb );
imagedestroy( $source );

}

if( isset( $_FILES['docs'] ) ) {
$img = str_replace( " ","_",$_FILES['docs']['name'] );
move_uploaded_file( $_FILES['docs']['tmp_name'], $target );
$source = "pics/".$_SESSION['user_id']."/";
$dest = "pics/".$_SESSION['user_id']."/";
thumbnail( $img, $source, $dest, 480, 400 );
unlink($dirname."/".$k2['img_path']);   
}

$m1 = "UPDATE members SET fname = '".$fname."', lname = '".$lname."',  password='".$password_hash."',  img_path = '".$docs."'  WHERE mem_id='".$_SESSION['user_id']."'";

}

UPDATED

$k1 = mysqli_query($con, "select img_path from members where     mem_id='".$_SESSION['user_id']."'");
$k2 = mysqli_fetch_array($k1);
$dirname = "pics/".$_SESSION['user_id']."/"; 
$target = $dirname . basename( $_FILES['docs']['name']); 

function thumbnail( $img, $source, $dest, $maxw, $maxh, $file) {      
    $jpg = $source.$img;

if( $jpg ) {
   ///list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image
    //$source = imagecreatefromjpeg( $file ); //<---got rid of this.
    $source = imagecreatefromstring(file_get_contents($file)); //<----Added this.
    if( $maxw >= $width && $maxh >= $height ) {
        $ratio = 1;
    }elseif( $width > $height ) {
        $ratio = $maxw / $width;
    }else {
        $ratio = $maxh / $height;
    }

    $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
    $thumb_height = round( $height * $ratio );

    //$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

    imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

    $path = $dest.$img."_thumb.jpg"; // I want to save _thumb.php image
    imagejpeg( $thumb, $path, 75 );
}
imagedestroy( $thumb );
imagedestroy( $source );

}

if( isset( $_FILES['docs'] ) ) {
$img = str_replace( " ","_",$_FILES['docs']['name'] );
//move_uploaded_file( $_FILES['docs']['tmp_name'], $target );  <----This is saving the picture you don't want.  
$source = "pics/".$_SESSION['user_id']."/";
$dest = "pics/".$_SESSION['user_id']."/";
$file = $_FILES['docs']['tmp_name']; //<---- This is the file you are making the image with.
thumbnail( $img, $source, $dest, 480, 400, $file);  //<--Added $file to your function.
//unlink($dirname."/".$k2['img_path']);   
}

$m1 = "UPDATE members SET fname = '".$fname."', lname = '".$lname."',  password='".$password_hash."',  img_path = '".$docs."'  WHERE mem_id='".$_SESSION['user_id']."'";

}
  • 写回答

1条回答 默认 最新

  • dongpai6567 2018-03-29 08:29
    关注

    As I said in my comment. Your code is saving two files. 1.) move_uploaded_file() is saving a picture. 2.) imagejpeg() is saving a picture with the "_thumb" added to it.

    So I made some changes to your code. 1.) Lets not move_uploaded_file. 2.) I changed your thumbnail function to pass the file and created your image object from it directly.

    See comments in the code.

    $k1 = mysqli_query($con, "select img_path from members where     mem_id='".$_SESSION['user_id']."'");
    $k2 = mysqli_fetch_array($k1);
    $dirname = "pics/".$_SESSION['user_id']."/"; 
    $target = $dirname . basename( $_FILES['docs']['name']); 
    
    function thumbnail( $img, $source, $dest, $maxw, $maxh, $file) {      
        $jpg = $source.$img;
    
    if( $jpg ) {
        //list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image  //<--This won't work anymore. 
    
        //$source = imagecreatefromjpeg( $file ); //<---got rid of this.
        $source = imagecreatefromstring(file_get_contents($file)); //<----Added this.
    
        $width   = imagesx($source); //<---Added this
        $height  = imagesy($source); //<---And this  
    
        if( $maxw >= $width && $maxh >= $height ) {
            $ratio = 1;
        }elseif( $width > $height ) {
            $ratio = $maxw / $width;
        }else {
            $ratio = $maxh / $height;
        }
    
        $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
        $thumb_height = round( $height * $ratio );
    
        $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
    
        imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );
    
        $path = $dest.$img."_thumb.jpg"; // I want to save _thumb.php image
        imagejpeg( $thumb, $path, 75 );
    }
    imagedestroy( $thumb );
    imagedestroy( $source );
    
    }
    
    if( isset( $_FILES['docs'] ) ) {
    $img = str_replace( " ","_",$_FILES['docs']['name'] );
    //move_uploaded_file( $_FILES['docs']['tmp_name'], $target );  <----This is saving the picture you don't want.  
    $source = "pics/".$_SESSION['user_id']."/";
    $dest = "pics/".$_SESSION['user_id']."/";
    $file = $_FILES['docs']['tmp_name']; //<---- This is the file you are making the image with.
    thumbnail( $img, $source, $dest, 480, 400, $file);  //<--Added $file to your function.
    unlink($dirname."/".$k2['img_path']);   
    }
    
    $docs = $dest.$img."_thumb.jpg"; <---Updated path to save to database.
    
    $m1 = "UPDATE members SET fname = '".$fname."', lname = '".$lname."',  password='".$password_hash."',  img_path = '".$docs."'  WHERE mem_id='".$_SESSION['user_id']."'";
    
    }
    

    That should work. Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示