我正在网站上上传一些图像。但是我也想以较小尺寸的那些图像进行复制。我已经四处搜寻,并想出了以下代码。问题在于,它没有创建图像的较小版本,而是仅占据了图像的一部分。我认为它的左上角。有人看到我错过的问题吗?图片通过POST传递
<?php
include "connect.php";
$uploaddir = '../img/avatars/';
$uploaddir35 = '../img/avatars/35x35/';
$uploaddir50 = '../img/avatars/50x50/';
$uploaddir100 = '../img/avatars/100x100/';
$uploaddir140 = '../img/avatars/140x140/';
$dimension35 = 35;
$dimension50 = 50;
$dimension100 = 100;
$dimension140 = 140;
$filename=$_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$file_name=$_SESSION['user_ID']."_".time().".".$ext;
$uploadfile = $uploaddir.$file_name;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo $uploadfile;
$_SESSION['img']=$file_name;
$query="UPDATE user
SET img='$file_name'
WHERE ID='".$_SESSION['user_ID']."'";
mysqli_query($con,$query);
} else {
echo "Upload failed";
echo 'Here is some more debugging info:';
print_r($_FILES);
}
//make small imges
$file_name = $_FILES['file']['tmp_name'];
$target_filename=$_SESSION['user_ID']."_".time().".".$ext;
list($width, $height, $type, $attr) = getimagesize( $file_name );
$src = imagecreatefromjpeg(file_get_contents($file_name));
//35x35
$dst = imagecreatetruecolor($dimension35,$dimension35);
imagecopyresampled( $dst, $src, 0, 0, 0, 0, $dimension35, $dimension35, $width, $height );
imagejpeg($dst, $uploaddir35.$target_filename );
imagedestroy( $dst );
//50x50
$dst = imagecreatetruecolor($dimension50,$dimension50);
imagecopyresampled( $dst, $src, 0, 0, 0, 0, $dimension50, $dimension50, $width, $height );
imagejpeg($dst, $uploaddir50.$target_filename );
imagedestroy( $dst );
//100x100
$dst = imagecreatetruecolor($dimension100,$dimension100);
imagecopyresampled( $dst, $src, 0, 0, 0, 0, $dimension100, $dimension100, $width, $height );
imagejpeg($dst, $uploaddir100.$target_filename );
imagedestroy( $dst );
//140x140
$dst = imagecreatetruecolor($dimension140,$dimension140);
imagecopyresampled( $dst, $src, 0, 0, 0, 0, $dimension140, $dimension140, $width, $height );
imagejpeg($dst, $uploaddir140.$target_filename );
imagedestroy( $dst );
?>