0

在共享网站上,我具有以下目录结构:

public_html
    skipper
        includes
            functions.php
        public
            img
                DSC_0195.JPG
                DSC_0197.JPG
            login.php
        views

所以public目录里面是img目录和login.php,img目录里面是两个jpg。

当我运行位于文件 functions.php 的包含目录中的合并函数时,我得到:

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in /home1/sailwbob/public_html/skipper/includes/functions.php on line 707

Warning: imagecreatefromjpeg(): 'img/DSC_0195.JPG' is not a valid JPEG file in /home1/sailwbob/public_html/skipper/includes/functions.php on line 707

我从位于公共目录中的 login.php 调用该函数:

merge('img/DSC_0195.JPG', 'img/DSC_0197.JPG', 'img/merged_test.jpg',0); 

这是合并功能:

function merge($filename_x, $filename_y, $filename_result, $mergeType = 0) {
  // merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged_har.jpg',0); //merge horizontally
  // merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged.jpg',1); //merge vertically

  //$mergeType 0 for horizandal merge 1 for vertical merge

  // Get dimensions for specified images
  list($width_x, $height_x) = getimagesize($filename_x);
  list($width_y, $height_y) = getimagesize($filename_y);


  $lowerFileName = strtolower($filename_x);
  if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
      $image_x = imagecreatefromjpeg($filename_x);
  }else if(substr_count($lowerFileName, '.png')>0){
      $image_x = imagecreatefrompng($filename_x);
  }else if(substr_count($lowerFileName, '.gif')>0){
      $image_x = imagecreatefromgif($filename_x);
  }


  $lowerFileName = strtolower($filename_y);
  if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
      $image_y = imagecreatefromjpeg($filename_y);
  }else if(substr_count($lowerFileName, '.png')>0){
      $image_y = imagecreatefrompng($filename_y);
  }else if(substr_count($lowerFileName, '.gif')>0){
      $image_y = imagecreatefromgif($filename_y);
  }


  if($mergeType==0){
  //for horizandal merge
     if($height_y<$height_x){
        $new_height = $height_y;

        $new_x_height = $new_height;
        $precentageReduced = ($height_x - $new_height)/($height_x/100);
        $new_x_width = ceil($width_x - (($width_x/100) * $precentageReduced));

        $tmp = imagecreatetruecolor($new_x_width, $new_x_height);
        imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);
        $image_x = $tmp;

        $height_x = $new_x_height;
        $width_x = $new_x_width;

     }else{
        $new_height = $height_x;

        $new_y_height = $new_height;
        $precentageReduced = ($height_y - $new_height)/($height_y/100);
        $new_y_width = ceil($width_y - (($width_y/100) * $precentageReduced));

        $tmp = imagecreatetruecolor($new_y_width, $new_y_height);
        imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);
        $image_y = $tmp;

        $height_y = $new_y_height;
        $width_y = $new_y_width;

     }

     $new_width = $width_x + $width_y;

     $image = imagecreatetruecolor($new_width, $new_height);

     imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
     imagecopy($image, $image_y, $width_x, 0, 0, 0, $width_y, $height_y);

  }else{
  //for verical merge
     if($width_y<$width_x){
        $new_width = $width_y;

        $new_x_width = $new_width;
        $precentageReduced = ($width_x - $new_width)/($width_x/100);
        $new_x_height = ceil($height_x - (($height_x/100) * $precentageReduced));

        $tmp = imagecreatetruecolor($new_x_width, $new_x_height);
        imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);
        $image_x = $tmp;

        $width_x = $new_x_width;
        $height_x = $new_x_height;

     }else{
        $new_width = $width_x;

        $new_y_width = $new_width;
        $precentageReduced = ($width_y - $new_width)/($width_y/100);
        $new_y_height = ceil($height_y - (($height_y/100) * $precentageReduced));

        $tmp = imagecreatetruecolor($new_y_width, $new_y_height);
        imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);
        $image_y = $tmp;

        $width_y = $new_y_width;
        $height_y = $new_y_height;

     }

     $new_height = $height_x + $height_y;

     $image = imagecreatetruecolor($new_width, $new_height);

     imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
     imagecopy($image, $image_y, 0, $height_x, 0, 0, $width_y, $height_y);

  }





  $lowerFileName = strtolower($filename_result);
  if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
     imagejpeg($image, $filename_result);
  }else if(substr_count($lowerFileName, '.png')>0){
     imagepng($image, $filename_result);
  }else if(substr_count($lowerFileName, '.gif')>0){
    imagegif($image, $filename_result);
  }


  // Clean up
  imagedestroy($image);
  imagedestroy($image_x);
  imagedestroy($image_y);

} 
4

1 回答 1

1

根据您的警告,您尝试合并的文件不是有效的 JPG 文件。无需检查给定文件的扩展名以确定它是什么类型的文件,您将能够根据文件的mime 类型更好地处理文件。

于 2019-03-12T17:56:09.920 回答