4
我試圖從正常尺寸的圖像創建2個裁剪縮略圖(60x60和150x150)。一切工作都很好,除了種植,這是行不通的。這部分產生縮略圖,一條黑線下來的右側是這樣的:使用imagecopy的圖像的作物中心正方形使用imagecopyresampled
.............
. ...
. ...
. ...
.............
調整大小正常工作,作爲結束縮略圖有60和150把正確的高度(從風景圖像作爲原工作)但他們仍然出現風景。
我有什麼:
list($thumb_width, $thumb_height) = getimagesize($thumb_target);
if ($thumb_width > $thumb_height) { // landscape
$thumb1_new_height = 60;
$thumb1_new_width = floor($thumb_width * ($thumb1_new_height/$thumb_height));
$thumb1_crop_x = ceil(($thumb_width - $thumb_height)/2);
$thumb1_crop_y = 0;
$thumb2_new_height = 150;
$thumb2_new_width = floor($thumb_width * ($thumb2_new_height/$thumb_height));
$thumb2_crop_x = ceil(($thumb_width - $thumb_height)/2);
$thumb2_crop_y = 0;
}
else if ($thumb_width < $thumb_height){ // portrait
$thumb1_new_width = 60;
$thumb1_new_height = floor($thumb_height * ($thumb1_new_width/$thumb_width));
$thumb1_crop_x = 0;
$thumb1_crop_y = ceil(($thumb_height - $thumb_width)/2);
$thumb2_new_width = 150;
$thumb2_new_height = floor($thumb_height * ($thumb2_new_width/$thumb_width));
$thumb2_crop_x = 0;
$thumb2_crop_y = ceil(($thumb_height - $thumb_width)/2);
} else { // square
$thumb1_new_width = 60;
$thumb1_new_height = 60;
$thumb1_crop_x = 0;
$thumb1_crop_y = 0;
$thumb2_new_width = 150;
$thumb2_new_height = 150;
$thumb2_crop_x = 0;
$thumb2_crop_y = 0;
}
$thumb1_tmp_img = imagecreatetruecolor($thumb1_new_width,$thumb1_new_height);
$thumb2_tmp_img = imagecreatetruecolor($thumb2_new_width,$thumb2_new_height);
imagecopyresampled($thumb1_tmp_img, $thumb_img, 0, 0, $thumb1_crop_x, $thumb1_crop_y, $thumb1_new_width, $thumb1_new_height, $thumb_width, $thumb_height);
imagecopyresampled($thumb2_tmp_img, $thumb_img, 0, 0, $thumb2_crop_x, $thumb2_crop_y, $thumb2_new_width, $thumb2_new_height, $thumb_width, $thumb_height);
非常感謝。你是對的,這是imagecreatetruecolor我錯了,我把錯誤的寬度/高度。:) – FoxyFish 2014-12-08 19:22:02
沒問題!我將它作爲一個類來使用,只是爲了讓您看到您不必爲了製作150像素的版本(或其他任何大小的版本)而複製所有代碼。你可以在任何你想使用拇指指針的地方加入這個類,然後使用'$ ImageMaker-> MakeThumb($ thumb_target,60,60,'image60px.jpg');'來生成任意數量的類。無論如何,很高興它的工作! – Rasclatt 2014-12-08 19:26:15