简体   繁体   English

无法调整大小的图像在PHP?

[英]unable to resize image in php?

I am currently having problem re-sizing images in upload form of php. 我目前在以php上传格式重新调整图像大小时遇到​​问题。
Here is my code : 这是我的代码:

<?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";
  }
  }

?>

I am able to upload my image to the database but the resize function doesn't seem to work and is unable to reduce the image size. 我可以将图像上传到数据库,但调整大小功能似乎无法正常工作,并且无法减小图像尺寸。

You are passing an url to imagejpeg function. 您正在将URL传递给imagejpeg函数。 The output file have to be the path to local directory as follows 输出文件必须是本地目录的路径,如下所示

$resized_file =  "/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name/resized_$profile_pic_name"

And apache must be allowed to write to the $resized_file 并且必须允许apap写入$ resized_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM