我目前在上传php格式时重新调整图像大小有问题 这是我的代码:
<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
<?php
if (isset($_FILES['profilepic'])) {
if (((@$_FILES["profilepic"]["type"]=="image/jpeg") || (@$_FILES["profilepic"]["type"]=="image/png") || (@$_FILES["profilepic"]["type"]=="image/gif"))&&(@$_FILES["profilepic"]["size"] < 1048576)) //1 Megabyte
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
$dir = "/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name";
mkdir($dir);
move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
$profile_pic_name = @$_FILES["profilepic"]["name"];
$target_file = "http://www.hootpile.com/userdata/profile_pics/$rand_dir_name/$profile_pic_name";
$resized_file = "http://www.hootpile.com/userdata/profile_pics/$rand_dir_name/resized_$profile_pic_name";
$wmax = 200;
$hmax = 150;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $imageFileType);
$profile_pic_query = mysqli_query($conn,"UPDATE users2 SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$user'");
}
else
{
echo "Invailid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
}
}
?>
我可以将图像上传到数据库,但调整大小功能似乎无法正常工作,无法缩小图像大小。
答案 0 :(得分:1)
您正在将网址传递给imagejpeg函数。输出文件必须是本地目录的路径,如下所示
$resized_file = "/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name/resized_$profile_pic_name"
必须允许apache写入$ resized_file