我正在尝试将压缩的图像保存到数据库以及文件夹中。现在两个图像都被保存了。这是我的代码
要压缩图片我使用此代码参考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']."'";
}
答案 0 :(得分:1)
正如我在评论中所说。您的代码正在保存两个文件。 1.)move_uploaded_file()正在保存图片。 2.)imagejpeg()正在保存添加了“_thumb”的图片。
所以我对你的代码做了一些修改。 1.)不要move_uploaded_file。 2.)我更改了缩略图功能以传递文件并直接从中创建了图像对象。
请参阅代码中的注释。
$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']."'";
}
那应该有用。希望有所帮助。