如何解决这个php中的图像大小调整错误?初学者



我正在尝试在服务器端调整图像大小。我正在获取图像,但是当我尝试调整图像大小时;它抛出了一些错误。这是我所做的:

imgresize.php:此文件创建调整大小的图像。

<?php
function compressImage($ext, $uploadedfile, $actual_image_name, $newwidth)
{
  if($ext=="jpg" || $ext=="jpeg" ){
    $src = imagecreatefromjpeg($uploadedfile);
      list($width,$height)=getimagesize($uploadedfile);
      $newheight=($height/$width)*$newwidth;
      $tmp=imagecreatetruecolor($newwidth,$newheight);
      imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    //  $filename = $newwidth.'_'.$actual_image_name; //PixelSize_TimeStamp.jpg
      $filename = $actual_image_name; //PixelSize_TimeStamp.jpg
      imagejpeg($tmp,$filename,100);
      imagedestroy($tmp);
  }
  else if($ext=="png"){
    $src = imagecreatefrompng($uploadedfile);
    list($width,$height)=getimagesize($uploadedfile);
    $newheight=($height/$width)*$newwidth;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
  //  $filename = $newwidth.'_'.$actual_image_name; //PixelSize_TimeStamp.jpg
    $filename = $actual_image_name; //PixelSize_TimeStamp.jpg
    imagepng($tmp,$filename,100);
    imagedestroy($tmp);
  }
  else{
  }
  return $filename;
}
?>

这是我在服务器端获取它的代码:

include 'imgresize.php';
if($_FILES['img']['tmp_name'] != ''){
            $tmp_name_array = $_FILES['img']['tmp_name'];
            $uploadedImageName = $_FILES['img']['name'][0];
            $imageName = htmlspecialchars($_FILES['img']['tmp_name']);
            $imgSize = $_FILES['img']['size'];
            //$name= htmlspecialchars($_FILES['img']['name']);
            print_r($_FILES['img']);
            $tempImgName = $_FILES['img']['tmp_name'];
            if(strlen($tempImgName)){
                $ext = pathinfo($tempImgName, PATHINFO_EXTENSION);
                if(in_array($ext, $valid_formats)){
                    if($imgSize < 2*1024*1024){
                        $imageName = compressImage($ext, $tempImgName, $tempImgName, 200);
                        $imageContents = file_get_contents($imageName);
                        $encodedImage = base64_encode($imageContents);
                        array_push($uploadImage, $encodedImage);
                        // database query

} else{
                        echo "<script type="text/javascript">".
                            "window.alert('Image size should be less than 2MB.');".
                            "window.location.href='edit-profile.php?id=".$id."';".
                            "</script>";
                    }
                } else{
                    echo "<script type="text/javascript">".
                            "window.alert('Invalid image extension found. (Image should be of jpg, jpeg, png extension only) ');".
                            "window.location.href='edit-profile.php?id=".$id."';".
                            "</script>";
                }
            }

它给了我以下错误:

[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  imagecreatefromjpeg(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/utils/imgresize.php on line 6
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  getimagesize(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/utils/imgresize.php on line 15
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  Division by zero in /home/user/public_html/utils/imgresize.php on line 17
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  imagecreatetruecolor(): Invalid image dimensions in /home/prernnys/public_html/utils/imgresize.php on line 19
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/prernnys/user/utils/imgresize.php on line 20
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  imagejpeg() expects parameter 1 to be resource, boolean given in /home/user/public_html/utils/imgresize.php on line 25
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  imagedestroy() expects parameter 1 to be resource, boolean given in /home/user/public_html/utils/imgresize.php on line 27
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning:  file_get_contents(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/update.php on line 916

图像未按预期调整大小。请纠正我出错的地方。

我认为产生问题是因为您在 压缩Image 函数中传递的$tempImgName不是服务器文件夹中的真实文件,而只是一个缓存文件 (11150825_10153266879053764_3215821412576026934_n.jpg)。要修复此错误,只需使用$imageName = compressImage($ext, $tempImgName, $tempImgName, 200);传递图像的真实路径。可能$uploadedImageName是正确的使用方式

最新更新