Jump to content

php image transparant


traxionn

Recommended Posts

hey all,i've been looking around this week but i cant find it out..I'm trying to generate a picture from inside a car, but with user given front-window and center-mirror.the center-mirror is going great, but when i want to get it together with the front-window it looks like the transparity is gone :S the result is..final.jpgi've put below the code, with some images that are generated during the creating of the fileto run the code lokal, you need the php code below and 3 imagehttp://erwinsmeets.nl/car/100_4.jpghttp://erwinsmeets.nl/car/mirror.jpghttp://erwinsmeets.nl/car/spiegel.pngthese images are generated during running the script to test the script step by stephttp://erwinsmeets.nl/car/init.jpghttp://erwinsmeets.nl/car/mirror_resize.jpghttp://erwinsmeets.nl/car/raw_mirror.pnghttp://erwinsmeets.nl/car/nice_mirror.pnghttp://erwinsmeets.nl/car/final.jpg

// needed// 100_4.jpg// mirror.jpg// spiegel.pngerror_reporting(E_ALL);ini_set('display_errors','On');// set config options$percent = 1;// create the background ( test dummy red)$src_img = imagecreate(3008,861);$red = imagecolorallocate($src_img, 255, 0, 0);imagecolortransparent($src_img, $red);imagejpeg($src_img, "init.jpg");imagedestroy($src_img);//----------------------------------------------------------------// create the center-mirror image on right size$mirror_path=  "mirror.jpg";$mirror=imagecreatefromjpeg($mirror_path);$factor = round(imageSX($mirror) / 800, 1);$mirror_x= round( imageSX($mirror) / $percent / $factor);$factor = round(imageSY($mirror) / 200, 1);$mirror_y= round( imageSY($mirror) / $percent / $factor);/* mirror coordinaten berekenen */$coordinate_X = round( 1180 / $percent);$coordinate_Y = round( 230 / $percent);// Resample$mirror_resize = imagecreate($coordinate_X, $coordinate_Y);imagecopyresampled($mirror_resize, $mirror, 0, 0, 0, 0, $mirror_x, $mirror_y, imageSX($mirror), imageSY($mirror));imagejpeg($mirror_resize, "mirror_resize.jpg");imagedestroy($mirror_resize);//----------------------------------------------------------------------// get all image together$src_img = imagecreatefromjpeg("init.jpg");//	paste the mirror onto the init.jpg$mirror_img = imagecreatefromjpeg("mirror_resize.jpg");imagecopy($src_img, $mirror_img, 1180, 230, 0, 0, imageSX($mirror_img), imageSY($mirror_img));//	paste the mirror-frame onto the init.jpg ( and so over a part of the mirror we just put on ( to make sure that the cornors are nice$spiegel_img = imagecreatefrompng("spiegel.png");imagecopy($src_img, $spiegel_img, 0, 0, 0, 0, imageSX($spiegel_img), imageSY($spiegel_img));imagepng($src_img, "raw_mirror.png");imagedestroy($mirror_img);imagedestroy($spiegel_img);imagedestroy($src_img);//-------------------------------------------------------------------------// So fare so good, i've got an image. Now i want to clear all the blue ( 0,255,255) of the image to make it transparant, // and then replace the default red background with a differnt one//clear over content ( blue rpg:0,255,255)$src_img = imagecreatefrompng("raw_mirror.png");$lightblue = imagecolorallocate($src_img, 0, 255, 255);imagecolortransparent($src_img, $lightblue);imagepng($src_img, "nice_mirror.png");imagedestroy($src_img);//---------------------------------------------------// oke it looks all oke.. in windows the image is transpirant ( also in mine browser)//get a background( front-window with pre generated dashboard);$src_img = imagecreatefromjpeg("100_4.jpg");//get the nice_mirror.png ( with transparity remember)$mirror_img = imagecreatefrompng("nice_mirror.png");// copy the mirror_img over the scr_imgimagecopy($src_img, $mirror_img, 0, 0, 0, 0, imageSX($mirror_img), imageSY($mirror_img));// go to final.jpg and view the final image.. NOT :(// where went it wrong :S//clear buffer and exit scriptimagejpeg($src_img, "final.jpg");imagedestroy($src_img);imagedestroy($mirror_img);

can somebody find out whats wrong?ty for the help

Link to comment
Share on other sites

You're not doing anything with transparency. Transparency does not automatically get copied over, you need to set what to do with the transparency. Like you can see, the transparent areas are not transparent, they're blue. You need to set the alpha settings to make that color transparent.This is a function I use to resize an image, you can see there's a part that checks for transparency inside gif and png images:

function resize_image($opts){  $src = isset($opts['source']) ? $opts['source'] : '';  $dest = isset($opts['dest']) ? $opts['dest'] : '';  $w = isset($opts['w']) ? intval($opts['w']) : 0;  $h = isset($opts['h']) ? intval($opts['h']) : 0;    if ($src == '')  {	return;  }  if ($w == 0 && $h == 0)  {	return;  }  if ($dest == '')	$dest = $src; // resize in place  // open the image  $ext = strtolower(array_pop(explode('.', $src)));  switch ($ext)  {	case 'jpg':	case 'jpeg':	  $i = imagecreatefromjpeg($src);	break;	case 'gif':	  $i = imagecreatefromgif($src);	break;	case 'png':	  $i = imagecreatefrompng($src);	break;	default:	  return;  }  $new_w = imagesx($i);  $new_h = imagesy($i);  if (($w != 0 && $new_w <= $w && $h == 0) ||	  ($w == 0 && $h != 0 && $new_h <= $h) ||	  ($w != 0 && $new_w <= $w && $h != 0 && $new_h <= $h))  {	// image is small enough	if ($dest != $src)	  copy($src, $dest);	return;  }  // determine new size  if ($w != 0 && $new_w > $w)  {	$new_h = ($w / $new_w) * $new_h;	$new_w = $w;  }  if ($h != 0 && $new_h > $h)  {	$new_w = ($h / $new_h) * $new_w;	$new_h = $h;  }    // resize  $new = imagecreatetruecolor($new_w, $new_h);    if ($ext == 'png' || $ext == 'gif')  {	$trnprt_indx = imagecolortransparent($i);	// If we have a specific transparent color	if ($trnprt_indx >= 0) 	{	  // Get the original image's transparent color's RGB values	  $trnprt_color = imagecolorsforindex($i, $trnprt_indx);	  // Allocate the same color in the new image resource	  $trnprt_indx = imagecolorallocate($new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);	  // Completely fill the background of the new image with allocated color.	  imagefill($new, 0, 0, $trnprt_indx);	  // Set the background color for new image to transparent	  imagecolortransparent($new, $trnprt_indx);	}	// Always make a transparent background color for PNGs that don't have one allocated already	elseif ($ext == 'png')	{	  // Turn off transparency blending (temporarily)	  imagealphablending($new, false);	  // Create a new transparent color for image	  $color = imagecolorallocatealpha($new, 0, 0, 0, 127);	  // Completely fill the background of the new image with allocated color.	  imagefill($new, 0, 0, $color);	  // Restore transparency blending	  imagesavealpha($new, true);	}  }  imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i));  imagedestroy($i);  // save the image  switch ($ext)  {	case 'jpg':	case 'jpeg':	  imagejpeg($new, $dest);	break;	case 'gif':	  imagegif($new, $dest);	break;	case 'png':	  imagepng($new, $dest);	break;  }  imagedestroy($new);}

Link to comment
Share on other sites

ty for the replaywhen i see ure code i've see that the script comes though

// If we have a specific transparent color	if ($trnprt_indx >= 0)	{	  // Get the original image's transparent color's RGB values	  $trnprt_color = imagecolorsforindex($i, $trnprt_indx);	  // Allocate the same color in the new image resource	  $trnprt_indx = imagecolorallocate($new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);	  // Completely fill the background of the new image with allocated color.	  imagefill($new, 0, 0, $trnprt_indx);	  // Set the background color for new image to transparent	  imagecolortransparent($new, $trnprt_indx);	}

Now i've copyed that section in mine code but i keep getting the same blue result :).More and more i'm thinking that the nice_mirror.png is broken, as i see the blue when i open the file in photoshop :)when i delete the blue i get the second colour of photoshop as new background for transparent :SCan somebody confirm if mine nice_mirror.png is correct or not?i've can read with multiple options the a color, which the function says its the transparent color.

$lightblue = imagecolorallocate($mirror_img, 0, 255, 255);imagecolortransparent($mirror_img, $lightblue);imagealphablending($mirror_img, true);imagesavealpha($mirror_img, false);

Now i've got with the thnx tip to the alphablending some extra functions, who should do the trick... but they arent.. i've been resettings some true/false settings but every possibility gives the same return..

Link to comment
Share on other sites

If that's all the code you copied, that's not enough. That's half of an if statement which checks for 2 types of transparency. It looks like you copied the part that checks for index transparency, which is what a GIF or 8-bit PNG would use.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...