2017-10-11 89 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

相關問題