2017-10-11 64 views
0
<?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); 

?> 

如何调整图像大小,而图像上传到该文件夹​​如何调整图像大小,而图像上传到该文件夹​​

回答

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); 
} 
+0

我不想使用任何库,请建议我有没有任何代码来调整图像 – pavithra