2016-01-04 209 views
0

夥計們,我知道這個問題之前被問過,但它不一樣,因爲我已經嘗試了別人的答案,沒有任何工作,因爲我想。Php創建固定寬度和高度的圖像拇指

我想用PHP和縮略圖寬度=「265px」高度=「125px」創建縮略圖,這裏是我的代碼:

$image_width = $imageSize[0]; 
$image_height = $imageSize[1]; 

$new_size = ($image_width + $image_height)/($image_width * ($image_height/45));   //this will set any image to 125*70 which is a good thumbnail 

$new_width = $image_width * $new_size; 
$new_height = $image_height * $new_size; 

if($ext == "gif"){ 
    $old_image = imagecreatefromgif($real_image_path); 
}else if($ext =="png"){ 
    $old_image = imagecreatefrompng($real_image_path); 
}else{ 
    $old_image = imagecreatefromjpeg($real_image_path); 
} 

$new_image = imagecreatetruecolor($new_width, $new_height);              //creating new image with a new color for quality 

imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); 

我不是很肯定我評論,但我只是寫他們,以防萬一

我覺得需要有人說是數學好

感謝和新年快樂

+0

,準確,這是怎麼工作不適合你......? –

+0

這裏是一個提示..你不需要數學來調整圖像大小.. –

+0

@MarcB它沒有得到我想要的尺寸 –

回答

1

無需數學..

if($ext == "gif"){ 
    $old_image = imagecreatefromgif($real_image_path); 
}else if($ext =="png"){ 
    $old_image = imagecreatefrompng($real_image_path); 
}else{ 
    $old_image = imagecreatefromjpeg($real_image_path); 
} 
$data = getimagesize($real_image_path); 

$height = 125; 
$width = 265; 
$thumb = imagecreatetruecolor($width, $height); 
imagealphablending($thumb, false); 
imagesavealpha($thumb, true); 
imagecopyresampled($thumb, $old_image, 0, 0, 0, 0, $width, $height, $data[0], $data[1]); 
+0

正是我想要的..我不知道我可以輸入這樣的寬度和高度 –