0

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);

I am doing one php file upload,in that field user uploads the large image means, i want to re size the image,see here i want like this ,but this code i can't understand,next after re size the image i want to encode that image how can do this one?

<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);

?>
  <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>
Community
  • 1
  • 1
kasthuri S
  • 39
  • 7

2 Answers2

1

Here's the php class to resize images.

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

Call this class using:

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();

EDIT: You don't need echo to make a function call.

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

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

$filename = base64_encode(output());
FrozenFire
  • 579
  • 14
  • 23
  • I am using simple PHP no need to use class how can do? – kasthuri S Jun 17 '16 at 06:27
  • Just copy the codes within the `SimpleImage` class and paste it to your PHP file, you can use it as functions. – FrozenFire Jun 17 '16 at 06:30
  • Yes, just get those functions out from class. – FrozenFire Jun 17 '16 at 06:32
  • Sorry Mr @FrozenFire,edit your code,then i will copy your code and test,bcz i am only 2 months experience of PHP developer – kasthuri S Jun 17 '16 at 06:39
  • You can use `output();`, or for your code `$filename = base64_encode(output());` – FrozenFire Jun 17 '16 at 07:05
  • i did removed my php code and fully copy your code and paste in my php file,and also call all functions finally $filename = base64_encode(output()); $idproof = array("encode" => $filename, ); echo json_encode($idproof); but i am getting like this error --->500 Internal Server Error – kasthuri S Jun 17 '16 at 07:13
  • @kasthuri S Sorry I forgot to add `global` in functions. see my updated answer. – FrozenFire Jun 17 '16 at 07:29
  • Still i am getting same error, i am calling functions like this echo resizeToWidth($width); // To resize based on image height echo resizeToHeight($height); // To resize to specific size echo resize($width, $height) FROM HERE $width and $hight what? we can give static or something else ? – kasthuri S Jun 17 '16 at 07:42
  • You must call it like this `resizeToWidth($width);` then `echo output();`. Use `output();` when you want to retrieve the image. – FrozenFire Jun 17 '16 at 07:49
  • Now i want to base64 code value after that i want insert value in DB,after inserting value only i want retrieve the image – kasthuri S Jun 17 '16 at 08:04
  • @kasthuri S please read my prev. comments. `$filename = base64_encode(output());` will do. – FrozenFire Jun 17 '16 at 08:06
  • still am getting same error,Please see my updated code,if i am anything did wrong means inform me – kasthuri S Jun 17 '16 at 08:16
  • You don't need to `echo` when making a function call, see my updated answer. – FrozenFire Jun 17 '16 at 09:06
  • K but load function you wrote two method load($_FILES['file']['tmp_name']); – kasthuri S Jun 17 '16 at 09:23
  • its depend on the how you've declared `name` in your form. Since you've used `` it should be Filename (`load($_FILES["Filename"]["tmp_name"]);`) e.g. also need to replace `$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);` to `$extension = pathinfo($_FILES['Filename']['name'], PATHINFO_EXTENSION);` – FrozenFire Jun 17 '16 at 09:33
  • Please see my code now changed instead of name= Filename(file),give hight and width but like this ------>SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data – kasthuri S Jun 17 '16 at 09:45
  • You're still using `echo` on function call, please read the EDITED part of my answer. – FrozenFire Jun 17 '16 at 09:48
  • Sorry i forgot to post this code but remove the echo i am getting same SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data – kasthuri S Jun 17 '16 at 09:54
  • save($PATH);//means what.from here i don't want to move this image in one folder,after encode the value directly insert into DB – kasthuri S Jun 17 '16 at 09:55
  • Then don't call `save($PATH);` – FrozenFire Jun 18 '16 at 01:42
0

Call this function where you insert your image

<?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);

?>
FrozenFire
  • 579
  • 14
  • 23