如何在将图像上传到文件夹时调整图像大小

时间:2017-10-11 07:02:28

标签: php

<?php 

 include 'config.php';
 $name = $_POST['name']; 
 $image = $_POST['image']; 

 //image in string format //decode the image

  $imageconverting = base64_decode($image);

 //upload the image 
 file_put_contents("pets_imgs/".$name.".jpg", $imageconverting);

?>

如何在将图像上传到文件夹时调整图像大小

1 个答案:

答案 0 :(得分:0)

您可以使用此library

实施图片大小调整

使用class.upload.php

library for image resize

或使用以下代码

maxDim = 800;
list($width, $height, $type, $attr) = getimagesize( $_FILES['myFile']['tmp_name'] );
if ( $width > $maxDim || $height > $maxDim ) {
     $target_filename = $_FILES['myFile']['tmp_name'];
     $fn = $_FILES['myFile']['tmp_name'];
     $size = getimagesize( $fn );
     $ratio = $size[0]/$size[1]; // width/height
     if( $ratio > 1) {
           $width = $maxDim;
           $height = $maxDim/$ratio;
     } else {
           $width = $maxDim*$ratio;
           $height = $maxDim;
     }
     $src = imagecreatefromstring( file_get_contents( $fn ) );
     $dst = imagecreatetruecolor( $width, $height );
     imagecopyresampled( $dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] );
     imagedestroy( $src );
     imagepng( $dst, $target_filename ); // adjust format as needed
     imagedestroy( $dst );
}