您可以使用此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);
}
我不想使用任何庫,請建議我有沒有任何代碼來調整圖像 – pavithra