如何使用PHP重新调整图像大小,如何删除错误500内部服务器错误817ms

时间:2016-06-17 08:12:46

标签: php php-5.2

我正在用PHP上传一个文件。从这里用户上传大图像意味着,我想重新调整图像大小。我这样写,但我无法得到答案。我收到类似 500内部服务器错误x的错误         817ms 即可。在图像上调整大小后我想编码该值,我该怎么做?



$.ajax({
  formData.append('file', $('input[type=file]')[0].files[0]);
        url: 'file_check.php',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
        var res=jQuery.parseJSON(data);// convert the json
        console.log(res);
         
        },
       });

file_check.php


<?php
/*$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$path = $_FILES['file']['tmp_name'];
$filename = file_get_contents($path);*/
/*    
$imgdata = base64_encode($im); // here we got base64 encode value

$idproof = array("encode" => $imgdata,
                 "path" =>$path,
                 "extension" =>$extension,
                 );
echo json_encode($idproof);*/

   
   $image;
   $image_type;

   //-----------------------------------------------
   //   Load Image file
   //-----------------------------------------------
   function load($filename) 
   {
       global $image;
       global $image_type;

            $image_info = getimagesize($filename);
            $image_type = $image_info[2];
            if($image_type == IMAGETYPE_JPEG) 
            {
                $image = imagecreatefromjpeg($filename);
            } 
            elseif($image_type == IMAGETYPE_GIF) 
            {
                $image = imagecreatefromgif($filename);
            }
            elseif($image_type == IMAGETYPE_PNG) 
            {
                $image = imagecreatefrompng($filename);
            }
   }

   //-----------------------------------------------
   //   Save Image file
   //-----------------------------------------------
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null)
   {
       global $image;
       global $image_type;

            if( $image_type == IMAGETYPE_JPEG ) 
            {
                imagejpeg($image,$filename,$compression);
            }
            elseif( $image_type == IMAGETYPE_GIF )
            {
                imagegif($image,$filename);
            } 
            elseif( $image_type == IMAGETYPE_PNG )
            {
                imagepng($image,$filename);
            }
            if( $permissions != null ) 
            {
                chmod($filename,$permissions);
            }
   }

   //-----------------------------------------------
   //   View Image file
   //-----------------------------------------------
   function output($image_type=IMAGETYPE_JPEG) 
   {
      global $image;
      global $image_type;

      if( $image_type == IMAGETYPE_JPEG )
      {
         imagejpeg($image);
      } 
      elseif( $image_type == IMAGETYPE_GIF )
      {
         imagegif($image);
      } elseif( $image_type == IMAGETYPE_PNG ) 
      {
         imagepng($image);
      }
   }

   function getWidth() 
   {
      global $image;
      return imagesx($image);
   }
   function getHeight() 
   {
      global $image;
      return imagesy($image);
   }

   //=============================================================================
   //   Resize Images
   //=============================================================================

   //-----------------------------------------------
   //   Resize Images 01 - Resize to Height
   //-----------------------------------------------
   function resizeToHeight($height) 
   {
      $ratio = $height / getHeight();
      $width = getWidth() * $ratio;
      resize($width,$height);
   }

   //-----------------------------------------------
   //   Resize Images 02 - Resize to Width
   //-----------------------------------------------
   function resizeToWidth($width) 
   {
      $ratio = $width / getWidth();
      $height = getheight() * $ratio;
      resize($width,$height);
   }

   //-----------------------------------------------
   //   Resize Images 03 - Scale in %
   //-----------------------------------------------
   function scale($scale) 
   {
      $width = getWidth() * $scale/100;
      $height = getheight() * $scale/100;
      resize($width,$height);
   }

   //-----------------------------------------------
   //   Resize Images 04 - Scale in %
   //-----------------------------------------------
   function resize($width,$height) 
   {
      global $image;

      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, getWidth(), getHeight());
      $image = $new_image;
   }     


echo load($_FILES['IMAGE_FILE']['tmp_name']);

// To resize based on image width
echo resizeToWidth($width); 

// To resize based on image height
echo resizeToHeight($height); 

// To resize to specific size
echo resize($width, $height) 

echo output();

$filename = base64_encode(output());

$idproof = array("encode" => $filename,
                 );
echo json_encode($idproof);


?>
&#13;
<form method="post" enctype="multipart/form-data" class="form-horizontal" id="idproof" name="myForm">
<div class="input-group heading1">
          <span class="input-group-btn">
              <span class="btn btn-primary btn-file">
                  Browse&hellip; <input type="file" id="myFile" name="file" >
              </span>
          </span>
          <input type="text" class="form-control" readonly="">
      </div>
  </div>
</div>
<div class="row">
<div class="col-md-5 col-md-offset-2">
  <div class="input-group heading1">
  <span class="input-group-btn">
  <button type="submit" class="btn btn-primary btn-md horoscope">Upload ID Proof</button>
  </div>
</div> 
</div>
</form
&#13;
&#13;
&#13;

0 个答案:

没有答案