dlfjj
dlfjj

Reputation: 349

PHP - Combine two images vertically

I am combining two images in my Laravel application, it works on my local machine. But the bottom image disappear on my Ubuntu Server. All images are less than 100kb. The memory limit is set to -1.

Note: Before it combine, I converted the top image to have same width of the bottom image.

$top_image_path = '/textImage.jpg';
$bottom_image_path = '/blueImage.jpg';

list($top_image_width, $top_image_height) = getimagesize($top_image_path);
list($bottom_image_width, $bottom_image_height) = getimagesize($bottom_image_path);

$merged_width = $bottom_image_width;
$merged_height = $top_image_height + $bottom_image_height;

$merged_image = imagecreatetruecolor($merged_width, $merged_height);

imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

$img1 = imagecreatefromjpeg($top_image_path);
$img2 = imagecreatefromjpeg($bottom_image_path);

imagecopy($merged_image, $img1, 0, 0, 0, 0, $top_image_width, $top_image_height);
imagecopy($merged_image, $img2, 0, $bottom_image_width, 0, 0, $bottom_image_width, $bottom_image_height);

imagejpeg($merged_image, 'merged_image.jpg'); 

Result:

The output image on my local machine: successfully combined images

The output image on Ubuntu Server: unsuccessfully combined images

Upvotes: 0

Views: 362

Answers (1)

user11133653
user11133653

Reputation:

I think that the line has an issue as following.

imagecopy($merged_image, $img2, 0, $top_image_height, 0, 0, $bottom_image_width, $bottom_image_height);

Upvotes: 1

Related Questions