使用jaxa和phpand干预库上传图像时出现错误

时间:2019-04-24 20:46:37

标签: php jquery mysql ajax intervention

我使用干预库将我的网站图像转换为webp图像格式,并且一切都在我的本地主机上运行良好,没有问题,但是当我在网站实时服务器上尝试运行时,出现了错误,并且无法下载该图像是用于上传图片的PHP代码的一部分。

// include the database file
require APP_ROOT."/functions/dbh.php";
require  APP_ROOT . '/vendor/autoload.php';
// import the Intervention Image Manager Class
use Intervention\Image\ImageManagerStatic as Image;
$userId = $_SESSION['id'];
$errors = array();
$success = array();
if (isset($_FILES['profileImg']) && !empty($_FILES['profileImg']['name'])) {
    $img = $_FILES['profileImg'];
    $getExt = array("jpg","png","jpeg","gif","jpeg 2000","webp");
  $imageName = strtolower($_FILES['profileImg']['name']);
    $imageNameTmp = strtolower($_FILES['profileImg']['tmp_name']);
    $dividImg = explode('.', $imageName);
    $getImgExt = end($dividImg);
    // check if the file uploaded is an image
    if (!in_array($getImgExt, $getExt)) {
        $errors[] = "Only Those Image Ext Allowed : " . implode(', ', $getExt);
    }else{
            $fileName = $_FILES['profileImg']['name'];
            $fileTmp  = $_FILES['profileImg']['tmp_name'];
            $fileSize = $_FILES['profileImg']['size'];
            $imgError = $_FILES['profileImg']['error'];
            $fileType = $_FILES['profileImg']['type'];
            if ($imgError !== 0) {
                $errors[] = "Oops! Sorry There was an error uploading your image please try again later";
            }else{
            // configure with favored image driver (gd by default)
            Image::configure(array('driver' => 'gd'));
           // set the image width and height for the image
          $newImgName     = "profile" . $userId . ".webp";
          $newImgName2     = "profile" . $userId . "-small.webp";
          $imgDestination = "../profile_image/" . $newImgName;
          $imgDestination2 = "../profile_image/" . $newImgName2;
          $saveProfileImg = Image::make($imageNameTmp)->resize(120, 90)->save($imgDestination);
          $saveProfileImg2 = Image::make($imageNameTmp)->resize(77, 40)->save($imgDestination2);

这是jquery ajax代码

// submit a new proile image to the database using ajax
            $("#profileImgForm").on('submit', function(event){
                event.preventDefault();
                $("#loader-icon").removeClass("d-none");
                $.ajax({
                    method: "POST",
                    target:   '#targetLayer', 
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData:false,
                    url:"<?php echo BASE_URL;?>/profile/add_avatar.php?id=<?php echo $_SESSION['id']; ?>",
                    success:function(data){
                        getAvatar();
                        $("#profileImgError").html(data);
                        $('#loader-icon').addClass("d-none");
                    }

                })
            });

这是我启用在服务器上显示错误时得到的错误

Uncaught Intervention\Image\Exception\NotReadableException: Image source not readable On Line 345

这部分干预是解决问题的原因

    public function init($data)

{
    $this->data = $data;

    switch (true) {

        case $this->isGdResource():
            return $this->initFromGdResource($this->data);

        case $this->isImagick():
            return $this->initFromImagick($this->data);

        case $this->isInterventionImage():
            return $this->initFromInterventionImage($this->data);

        case $this->isSplFileInfo():
            return $this->initFromPath($this->data->getRealPath());

        case $this->isBinary():
            return $this->initFromBinary($this->data);

        case $this->isUrl():
            return $this->initFromUrl($this->data);

        case $this->isStream():
            return $this->initFromStream($this->data);

        case $this->isDataUrl():
            return $this->initFromBinary($this->decodeDataUrl($this->data));

        case $this->isFilePath():
            return $this->initFromPath($this->data);

        // isBase64 has to be after isFilePath to prevent false positives
        case $this->isBase64():
            return $this->initFromBinary(base64_decode($this->data));

        default:
            throw new Exception\NotReadableException("Image source not readable");
    }
}

我安装了gd libery。

0 个答案:

没有答案