0
請在我上傳或在頁面上顯示圖像時使用下面的php圖像函數來調整圖像大小,但我發現當我試圖調整原始圖像大小時,它不會給我我想要的實際尺寸。Php圖像重採樣尺寸問題
例如:原始圖像寬度600
和高度是400
我打算把它調整爲180 X 180
,但它給了我寬度180
和高度120
。請問我不知道這個問題有哪些人可以幫忙?
<?php
function CroppedThumbnail($imagename, $meta, $newpath, $imgwidth, $imgheight, $rename, $imgQuality){
// Set a maximum height and width
$width = $imgwidth;
$height = $imgheight;
$url = '';
//Get image info
$info = @getimagesize($imagename);
$fileparts = pathinfo($imagename);
// Get new dimensions
$imageAvatar = substr($fileparts['filename'],0,5) . '-' . $imgwidth . 'x' . $imgheight . '.png';
if(is_dir($newpath . '/') && file_exists($newpath . '/' . $imageAvatar)){
return $url . $newpath . '/' . $imageAvatar;
}else{
list($width_orig, $height_orig) = $info;
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
if ($info['mime'] == 'image/jpeg'){
$image = imagecreatefromjpeg($url.$imagename);
}
else if ($info['mime'] == 'image/gif'){
$image = imagecreatefromgif($url.$imagename);
}
else if ($info['mime'] == 'image/png'){
$image = imagecreatefrompng($url.$imagename);
}
$image_p = imagecreatetruecolor($width, $height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//Rename the image
if($rename == true){
$newName = substr($fileparts['filename'],0,5) . '-' . $imgwidth . 'x' . $imgheight;
$newFileName = $newpath . '/' . $newName . '.png';
}else{
$newFileName = $newpath . '/' . $imagename;
}
// Output
imagejpeg($image_p, $newFileName, $imgQuality);
return $url . $newFileName;
}
}
600/400 = 1.5以及180/120 = 1.5。如果您將圖像轉換爲180x180,您會看到一張看起來不太好看的圖像。 – Danielius
@Danielius是的,但我不知道該怎麼做,因爲用戶上傳任何尺寸的圖像,我需要180x180。我也可以拒絕他們選擇的任何圖像,因爲他們有些不知道如何獲得實際尺寸 – Peter
菲利普,哦,那麼我明白了。 – Danielius