2016-06-17 10 views
0

Как изменить размер изображения в PHP

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 я хочу, как это , но этот код я не могу понять, следующий после re размер изображения я хочу кодировать это изображение, как это сделать?

<script type="text/javascript"> 
 
$("#idproof").submit(function(e) { 
 
      var formData = new FormData(); 
 
      var id = '<?php echo $id;?>'; 
 
      formData.append('ssmid', id); 
 
\t \t \t 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>

+1

Возможный дубликат [изменить размер изображения в PHP] (http://stackoverflow.com/questions/14649645/resize-image-in-php) –

ответ

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

EDIT: Вам не нужно echo сделать вызов функции.

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

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

$filename = base64_encode(output()); 
+0

Я использую простой PHP, не нужно использовать класс, как это сделать? –

+0

Просто скопируйте коды в классе 'SimpleImage' и вставьте его в свой PHP-файл, вы можете использовать его как функции. – FrozenFire

+0

Возможные способы объединения моего кода и вашего кода –

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

?>