我在我的ftp上使用img上傳器。 如果圖像大到1024x768我使用腳本調整大小。 但調整後失去質量。從uploader.php當圖像較大時調整圖像的質量而不會丟失質量
代碼:
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], "$upload_folder/" . $newname))
if($width>1024 || $height>768) {
require './image_resize.php';
echo (image_resize("$upload_folder/" . $newname, "$upload_folder/" . $newname, 1024, 768));
}
代碼舞會image_resize.php:
<?php ini_set('memory_limit','500M');
function image_resize($src, $dst, $width, $height, $crop=0){
if(!($pic = @getimagesize($src)))
return false;
$w = $pic[0];
$h = $pic[1];
$type = substr($pic['mime'], 6);
$func = 'imagecreatefrom' . $type;
if(!function_exists($func))
return false;
$img = $func($src);
if($crop){
if($w < $width && $h < $height)
return false;
$ratio = max($width/$w, $height/$h);
$h = $height/$ratio;
$x = ($w - $width/$ratio)/2;
$w = $width/$ratio;
}
else{
if($w < $width && $h < $height)
return false;
$ratio = min($width/$w, $height/$h);
$width = $w * $ratio;
$height = $h * $ratio;
$x = 0;
}
$new = imagecreatetruecolor($width, $height);
if($type == "gif" || $type == "png"){
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
$save = 'image' . $type;
$save($new, $dst);
return true;
}
這是不可能減少的大小而不失品位:(
第三個參數在'圖像{類型}()'是質量... http://php.net/manual/en/function.imagejpeg.php – Rasclatt