【问题标题】:saving only compressed image to database仅将压缩图像保存到数据库
【发布时间】:2018-03-29 17:19:57
【问题描述】:

我正在尝试将压缩图像保存到数据库以及文件夹中。现在两个图像都被保存了。这是我的代码

为了压缩图像,我使用此代码引用 reduce image size while uploading using the following PHP code used to upload image

这是我的完整代码

$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']."'";

}

更新

$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.) 移动具有您原始文件名的上传文件。 2.)imagejpeg 与拇指添加到文件名。你说的是这两个吗?
  • 是的。例如:我的图像是 DSC_0169.JPG。并且 2 张图片被上传为 DSC_0169.JPG 和 DSC_0169.JPG_thumb.jpg。这里我只想上传 DSC_0169.JPG_thumb.jpg

标签: php mysql upload


【解决方案1】:

正如我在评论中所说。您的代码正在保存两个文件。 1.) move_uploaded_file() 正在保存图片。 2.) imagejpeg() 正在保存一张添加了“_thumb”的图片。

所以我对您的代码进行了一些更改。 1.) 让我们不要 move_uploaded_file。 2.) 我更改了您的缩略图功能以传递文件并直接从中创建您的图像对象。

见代码中的 cmets。

$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']."'";

}

应该可以。希望对您有所帮助。

【讨论】:

  • getimagesize() 正在发出警告。文件路径在数据库中得到更新,但在文件夹中它没有被上传。 Warning: getimagesize(pics/PCD8Q5/IMG_20161031_123058.jpg): failed to open stream:.. my file path, Warning: imagecreatetruecolor(): Invalid image dimensions in .. file path ..
  • @meenal 你不再需要它了。把它注释掉。您可以使用 $fileInfo = image_type_to_mime_type(exif_imagetype($file));如果您仍然需要它,可以从图像中获取 exif 数据。
  • 如果我注释掉 getimagesize(),list( $width, $height ) 呢,否则宽度和高度将是不确定的,对吧?
  • @meenal 我在createimagefromstring下添加了宽度和高度
  • @meenal 你缺少高度和宽度。检查我的代码它们在 imagecreatefromsting 函数下
猜你喜欢
  • 2017-11-30
  • 2012-06-14
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多