Jump to content

Merging images doesn't work


Ishimoto

Recommended Posts

Hi,

I'm trying to combine two images - an uploaded image and a permanent. First I try to resize them to the same dimensions. Why I resize the uploaded image to the dimensions of permanent, everything works fine. But when I do the opposite, the images just don't merge, and the output is just the permanent image with resized dimensions. Anyone knows why?

Here is my code:

<?php
if( isset( $_FILES['image'] ) ){
	$desired_width = 360;
	$desired_height = 360;

	$layers = array();
	switch($_FILES['image']['type']){
		case "image/jpeg":
			$source_image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
		    break;
		case "image/gif":
			$source_image = imagecreatefromgif($_FILES['image']['tmp_name']);
			break;
		case "image/png":
			$source_image = imagecreatefrompng($_FILES['image']['tmp_name']);
			break;
	}
	
	$frame = imagecreatefrompng("1.png");
        $desired_width = imagesx($source_image);
        $desired_height = imagesy($source_image);
        $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
        imagecopyresampled($virtual_image, $frame, 0, 0, 0, 0, $desired_width, $desired_height, 360, 360);

	$layers = array($source_image,$virtual_image);

	$image = imagecreatetruecolor($desired_width, $desired_height);

	// to make background transparent
	imagealphablending($image, false);
	$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
	imagefill($image, 0, 0, $transparency);
	imagesavealpha($image, true);

	imagealphablending($image, true);
	for ($i = 0; $i < count($layers); $i++) {
	    imagecopy($image, $layers[$i], 0, 0, 0, 0, $desired_width, $desired_height);
	}
	imagealphablending($image, false);
	imagesavealpha($image, true);

	header('Content-type: image/png');
	header('Content-Disposition: attachment; filename="new_profile.png"');
	imagepng($image);
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.