如何在php中调整图像大小

时间:2016-06-17 06:12:40

标签: php image-resizing php-5.2



My Updated code


/*$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;
   $width = 150;
   $height = 150;
   //-----------------------------------------------
   //   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;
   }     


 load($_FILES['file']['tmp_name']);

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

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

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

$filename = base64_encode(output());

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




我正在做一个php文件上传,在该字段中用户上传大图像意味着,我想重新调整图像大小see here我想要这样,但是这个代码我无法理解,接下来重新调整图像大小后,我想对该图像进行编码怎么做呢?



<script type="text/javascript">
 $("#idproof").submit(function(e) {
            var formData = new FormData();
            var id = '<?php echo $id;?>';
            formData.append('ssmid', id);
			formData.append('file', $('input[type=file]')[0].files[0]);
              $.ajax({
              url: 'idproof_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);
              },
             });
             });
</script>


idproof_check.php

<?php
$userid = $_POST['userid'];
 
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$path = $_FILES['file']['tmp_name'];

$im = file_get_contents($path);
/*Here i want to resize the image after that i want to encode the image*/

$filename = base64_encode($im); // here we got base64 encoded value

$idproof = array("encode" => $filename,
                 "path" =>$path,
                 "extension" =>$extension,
                 );
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 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;

2 个答案:

答案 0 :(得分:1)

这是用于调整图像大小的php类。

    <?php

    /*
    * File: SimpleImage.php
    * Author: Simon Jarvis
    * Copyright: 2006 Simon Jarvis
    * Date: 08/11/06
    * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details:
    * http://www.gnu.org/licenses/gpl.html
    *
    */

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

使用以下方法调用此类:

load($_FILES["IMAGE_FILE"]["tmp_name"]);

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

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

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

// To save file in web server directory
save($PATH);

// To retrieve resized images directly from variable
output();

修改 您不需要echo进行函数调用。

$width = 150;
$height = 150;
load($_FILES['file']['tmp_name']);

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

$filename = base64_encode(output());

答案 1 :(得分:0)

在插入图片的地方调用此功能

<?php
function make_thumb($src, $dest, $desired_width, $desired_height) {

    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);
    /*if($width <600){
    $desired_width = $width;    
    }*/

    /* find the "desired height" of this thumbnail, relative to the desired width  */


    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest);
}


    $src = "../../gallery_image/$file_name";
    $dest = "../../gallery_image/thumb/$file_name";
    $desired_width = "360";
    $desired_height = "260";
    make_thumb($src, $dest, $desired_width, $desired_height);

?>