Jump to content

imagecolortransparent help please!


lordphate

Recommended Posts

I need help in creating transparent sides to my photos

 

if(!$path)
	$path = DOC_ROOT."/photos";

mt_srand((double) microtime() * 100000);
$photo_name = hs_substr(md5(time().mt_rand(0,100)),0,15);

$folder = substr($photo_name,0,2);
if(!file_exists($path."/".$folder))
	mkdir($path."/".$folder,PERMISSIONS);

$folder.= "/".substr($photo_name,2,2);
if(!file_exists($path."/".$folder))
	mkdir($path."/".$folder,PERMISSIONS);

$folder.= "/".substr($photo_name,4,2);
if(!file_exists($path."/".$folder))
	mkdir($path."/".$folder,PERMISSIONS);

$photo["ex"]    = $folder."/".$photo_name.".png";
$photo["med"]   = $folder."/".$photo_name."_med.png";
$photo["small"] = $folder."/".$photo_name."_small.png";

if($upload)
	move_uploaded_file($tmpfile,$path."/".$photo["ex"]);
else
	copy($tmpfile,$path."/".$photo["ex"]);

chmod($path."/".$photo["ex"], 0644);

list($ex_width,$ex_height) = getimagesize($path."/".$photo["ex"]);

//landscape image or square
if($ex_width >= $ex_height){

	if($ex_width > $GLOBALS["settings"]["photo_ex_width"]){

		$ds_width_ex  = $GLOBALS["settings"]["photo_ex_width"];
		$ratio_ex     = $ex_width / $ds_width_ex;
		$ds_height_ex = $ex_height / $ratio_ex;
		$ds_height_ex = round($ds_height_ex);

		if($ds_height_ex > $GLOBALS["settings"]["photo_ex_height"])
			$ds_height_ex = $GLOBALS["settings"]["photo_ex_height"];

	}//if
	else{

		$ds_width_ex  = $ex_width;
		$ds_height_ex = $ex_height;

	}//else

	if($ex_width > $GLOBALS["settings"]["photo_med_width"]){

		$ds_width_med  = $GLOBALS["settings"]["photo_med_width"];
		$ratio_med     = $ex_width / $ds_width_med;
		$ds_height_med = $ex_height / $ratio_med;
		$ds_height_med = round($ds_height_med);

		if($ds_height_med > $GLOBALS["settings"]["photo_med_height"])
			$ds_height_med = $GLOBALS["settings"]["photo_med_height"];

	}//if
	else{

		$ds_width_med  = $ex_width;
		$ds_height_med = $ex_height;

	}//else

	if($ex_width > $GLOBALS["settings"]["photo_small_width"]){

		$ds_width_small  = $GLOBALS["settings"]["photo_small_width"];
		$ratio_small     = $ex_width / $ds_width_small;
		$ds_height_small = $ex_height / $ratio_small;
		$ds_height_small = round($ds_height_small);

		if($ds_height_small > $GLOBALS["settings"]["photo_small_height"])
			$ds_height_small = $GLOBALS["settings"]["photo_small_height"];

	}//if
	else{

		$ds_width_small  = $ex_width;
		$ds_height_small = $ex_height;

	}//else

}//if

//portrait image
elseif($ex_width < $ex_height){

	if($ex_height > $GLOBALS["settings"]["photo_ex_height"]){

		$ds_height_ex = $GLOBALS["settings"]["photo_ex_height"];
		$ratio_ex     = $ex_height / $ds_height_ex;
		$ds_width_ex  = $ex_width / $ratio_ex;
		$ds_width_ex  = round($ds_width_ex);

		if($ds_width_ex > $GLOBALS["settings"]["photo_ex_width"])
			$ds_width_ex = $GLOBALS["settings"]["photo_ex_width"];

	}//if
	else{

		$ds_width_ex  = $ex_width;
		$ds_height_ex = $ex_height;

	}//else

	if($ex_height > $GLOBALS["settings"]["photo_med_height"]){

		$ds_height_med = $GLOBALS["settings"]["photo_med_height"];
		$ratio_med     = $ex_height / $ds_height_med;
		$ds_width_med  = $ex_width / $ratio_med;
		$ds_width_med  = round($ds_width_med);

		if($ds_width_med > $GLOBALS["settings"]["photo_med_width"])
			$ds_width_med = $GLOBALS["settings"]["photo_med_width"];

	}//if
	else{

		$ds_width_med  = $ex_width;
		$ds_height_med = $ex_height;

	}//else

	if($ex_height > $GLOBALS["settings"]["photo_small_height"]){

		$ds_height_small = $GLOBALS["settings"]["photo_small_height"];
		$ratio_small     = $ex_height / $ds_height_small;
		$ds_width_small  = $ex_width / $ratio_small;
		$ds_width_small  = round($ds_width_small);

		if($ds_width_small > $GLOBALS["settings"]["photo_small_width"])
			$ds_width_small = $GLOBALS["settings"]["photo_small_width"];

	}//if
	else{

		$ds_width_small  = $ex_width;
		$ds_height_small = $ex_height;

	}//else

}//elseif

$x_ex = round((($GLOBALS["settings"]["photo_ex_width"] - $ds_width_ex)/2));
if($x_ex == 0)
	$x_ex = 1;
$y_ex = round((($GLOBALS["settings"]["photo_ex_height"] - $ds_height_ex)/2));
if($y_ex == 0)
	$y_ex = 1;

$x_med = round((($GLOBALS["settings"]["photo_med_width"] - $ds_width_med)/2));
if($x_med == 0)
	$x_med = 1;
$y_med = round((($GLOBALS["settings"]["photo_med_height"] - $ds_height_med)/2));
if($y_med == 0)
	$y_med = 1;

$x_small = round((($GLOBALS["settings"]["photo_small_width"] - $ds_width_small)/2));
if($x_small == 0)
	$x_small = 1;
$y_small = round((($GLOBALS["settings"]["photo_small_height"] - $ds_height_small)/2));
if($y_small == 0)
	$y_small = 1;

if($extension == ".gif"){
	$src = ImageCreateFromGif($path."/".$photo["ex"]);
}//if
elseif($extension == ".jpeg"){
	$src = ImageCreateFromJpeg($path."/".$photo["ex"]);
}//elseif

$dst_ex    = ImageCreateTrueColor($GLOBALS["settings"]["photo_ex_width"]+2,$GLOBALS["settings"]["photo_ex_height"]+2);
$dst_med   = ImageCreateTrueColor($GLOBALS["settings"]["photo_med_width"]+2,$GLOBALS["settings"]["photo_med_height"]+2);
$dst_small = ImageCreateTrueColor($GLOBALS["settings"]["photo_small_width"]+2,$GLOBALS["settings"]["photo_small_height"]+2);

$white_ex    = imagecolorat($dst_ex,1,1);
$white_med   = imagecolorat($dst_med,1,1);
$white_small = imagecolorat($dst_small,1,1);

$white_ex	 = ImageColorTransparent($dst_ex,$white_ex);
$white_med	 = ImageColorTransparent($dst_med,$white_med);
$white_small = ImageColorTransparent($dst_small,$white_small);

ImageFilledRectangle($dst_ex,1,1,$GLOBALS["settings"]["photo_ex_width"],$GLOBALS["settings"]["photo_ex_height"],$white_ex);
ImageFilledRectangle($dst_med,1,1,$GLOBALS["settings"]["photo_med_width"],$GLOBALS["settings"]["photo_med_height"],$white_med);
ImageFilledRectangle($dst_small,1,1,$GLOBALS["settings"]["photo_small_width"],$GLOBALS["settings"]["photo_small_height"],$white_small);

ImageCopyResampled($dst_ex,$src,$x_ex,$y_ex,0,0,$ds_width_ex,$ds_height_ex,$ex_width,$ex_height);
ImageCopyResampled($dst_med,$src,$x_med,$y_med,0,0,$ds_width_med,$ds_height_med,$ex_width,$ex_height);
ImageCopyResampled($dst_small,$src,$x_small,$y_small,0,0,$ds_width_small,$ds_height_small,$ex_width,$ex_height);

if($extension == ".gif"){
	ImagePng($dst_ex,$path."/".$photo["ex"]);
}//if
elseif($extension == ".jpeg"){
	ImagePng($dst_ex,$path."/".$photo["ex"],80);
}//elseif

ImagePng($dst_med,$path."/".$photo["med"],80);
ImagePng($dst_small,$path."/".$photo["small"],80);

ImageDestroy($src);
ImageDestroy($dst_ex);
ImageDestroy($dst_med);
ImageDestroy($dst_small);

return $photo;

 

 

If you have any questions please don't hesitate to ask here is an example of what its doing right now

 

849bebcdbeb4a11.png

Link to comment
https://forums.phpfreaks.com/topic/84060-imagecolortransparent-help-please/
Share on other sites

$white_ex	 = ImageColorTransparent($dst_ex,$white_ex);

 

the above sets $white_ex to the previous transparent color (none), use

 

ImageColorTransparent($dst_ex,$white_ex);
ImageColorTransparent($dst_med,$white_med);
ImageColorTransparent($dst_small,$white_small);

Archived

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

×
×
  • 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.