Обрезать центральный квадрат изображения с помощью imagecopyresampled

Я пытаюсь создать 2 обрезанных эскиза (60×60 и 150×150) из изображения нормального размера. Все отлично работает, кроме обрезки, которая не работает. Эта часть создает миниатюру с черной линией вниз по правой стороне, вот так:

.............
.         ...
.         ...
.         ...
.............

Изменение размера работает отлично, так как конечные миниатюры имеют правильную высоту 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);

4

Решение

Я думаю, что вы ошибаетесь в своем thumb1_tmp_img близко к концу. Я сделал это классом, поэтому мне не нужно было копировать так много, как у вас, но вы можете увидеть изменения. Посмотрим, сделает ли это это для вас:

class ImageFactory
{
public  function MakeThumb($thumb_target = '', $width = 60,$height = 60,$SetFileName = false, $quality = 80)
{
$thumb_img  =   imagecreatefromjpeg($thumb_target);

// size from
list($w, $h) = getimagesize($thumb_target);

if($w > $h) {
$new_height =   $height;
$new_width  =   floor($w * ($new_height / $h));
$crop_x     =   ceil(($w - $h) / 2);
$crop_y     =   0;
}
else {
$new_width  =   $width;
$new_height =   floor( $h * ( $new_width / $w ));
$crop_x     =   0;
$crop_y     =   ceil(($h - $w) / 2);
}

// I think this is where you are mainly going wrong
$tmp_img = imagecreatetruecolor($width,$height);

imagecopyresampled($tmp_img, $thumb_img, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h);

if($SetFileName == false) {
header('Content-Type: image/jpeg');
imagejpeg($tmp_img);
}
else
imagejpeg($tmp_img,$SetFileName,$quality);

imagedestroy($tmp_img);
}
}

// Initiate class
$ImageMaker =   new ImageFactory();

// Here is just a test landscape sized image
$thumb_target   =   'http://media1.santabanta.com/full6/Outdoors/Landscapes/landscapes-246a.jpg';

// 60px x 60px
$ImageMaker->MakeThumb($thumb_target,60,60,'image60px.jpg');

// Here is a portrait for testing
$thumb_target   =   'http://imgc.allpostersimages.com/images/P-488-488-90/55/5543/L6HLG00Z/posters/warning-you-are-entering-a-fart-zone.jpg';

// 150px x 150px
$ImageMaker->MakeThumb($thumb_target,150,150,'image150px.jpg');
6

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector