Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Графика > Как разделить картинку на 2 части


Автор: apostolnikolai 23.6.2018, 20:26
Здравствуйте! 

Подскажите как на PHP можно разделить один графический файл на два, ровно по середине?

Вот вариант кода, который это делает, но он чуть обрезает картинки на выходе и вырезанное закрашивает черным. Подскажите что не так 

Код

$padding=10;
$info = getimagesize("files/upload/1_1.jpg");
$width=$info[0];
$height=$info[1];

$canvasWidth=$width+4*$padding;
$canvasHeight=$height+2*$padding;

$output = imagecreatetruecolor($canvasWidth, $canvasHeight);
//$background = imagecolorallocate($output, 255, 255, 255);
imagefill($output, 0, 0, $background);

$output1 = imagecreatetruecolor($canvasWidth, $canvasHeight);
//$background = imagecolorallocate($output, 255, 255, 255);
imagefill($output, 0, 0, $background);

$orig = imagecreatefromjpeg("files/upload/1_1.jpg");

imagecopy($output, $orig,$padding, $padding, 0, 0, $width/2, $height);

imagecopy($output1, $orig,2*$padding+$width/2, $padding, $width/2, 0, $width/2, $height);

imagejpeg($output,"files/upload/ram/str.1.jpg");

imagejpeg($output1,"files/upload/ram/str.2.jpg");

Автор: Vardoulacha 24.6.2018, 05:39
Если я с утра правильно вижу ))

1. обрезает из-за паддингов, зачем они там вообще?

2. черным красит потому что неправильный размер канвы $canvasWidth а надо $width/2

P.S. зачем вот это вот все $canvasWidth=$width+4*$padding; ? из-за этого все лаги с обрезанием

Автор: apostolnikolai 24.6.2018, 11:25
Если Вас не затруднит можете поправить код! я с php очень плоха дружу.

Автор: apostolnikolai 24.6.2018, 12:01
С левой стороной разобрался 
Код


$padding=10;
$info = getimagesize("files/upload/1_1.jpg");
$width=$info[0];
$height=$info[1];

$canvasWidth=$width;
$canvasHeight=$height;

$output = imagecreatetruecolor($canvasWidth/2, $canvasHeight);
//$background = imagecolorallocate($output, 255, 255, 255);
imagefill($output, 0, 0, $background);

$orig = imagecreatefromjpeg("files/upload/1_1.jpg");

imagecopy($output,    $orig,    0,    0,    0,            0, $width/2, $height);


Добавлено через 1 минуту и 20 секунд
А вот правую сторону как пока не выходит

Код

$padding=10;
$info = getimagesize("files/upload/1_1.jpg");
$width=$info[0];
$height=$info[1];

$canvasWidth=$width;
$canvasHeight=$height;

$output1 = imagecreatetruecolor($canvasWidth, $canvasHeight);
//$background = imagecolorallocate($output, 255, 255, 255);
imagefill($output, 0, 0, $background);

$orig = imagecreatefromjpeg("files/upload/1_1.jpg");



imagecopy($output1, $orig,    0,     0,    $width/2,    0, $width/2, $height);


Добавлено через 8 минут и 35 секунд
Все разобрался! Вот рабочий код если кому надо :

Код

$padding=10;
$info = getimagesize("files/upload/1_1.jpg");
$width=$info[0];
$height=$info[1];

$canvasWidth=$width/2;
$canvasHeight=$height;

$output = imagecreatetruecolor($canvasWidth, $canvasHeight);
//$background = imagecolorallocate($output, 255, 255, 255);
imagefill($output, 0, 0, $background);

$output1 = imagecreatetruecolor($canvasWidth, $canvasHeight);
//$background = imagecolorallocate($output, 255, 255, 255);
imagefill($output, 0, 0, $background);

$orig = imagecreatefromjpeg("files/upload/1_1.jpg");

imagecopy($output,    $orig,    0,    0,    0,            0, $width/2, $height);

imagecopy($output1, $orig,    0,     0,    $width/2,    0, $width/2, $height);

//imagecopy($output, $orig,3*$padding+2*$width/3,$padding, 2*$width/3, 0, $width/3, $height);

imagejpeg($output,"files/upload/ram/str.1.jpg");

imagejpeg($output1,"files/upload/ram/str.2.jpg");



ВСЕМ СПАСИБО !!!!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)