Its giving me some unexpected results-(namely not implementing/keeping the col2(color on layer 2) I am fairly new to GD so my code may be a bit basic...(or shocking )
Code: Select all
<?php
/**--------------------------------------------------------------------------------------|
| Produced with heaps of help from PHP Communities worldwide by JOSH REID for Diamond NZ |
| |
| CREATES A MULTI LAYER VARIABLE CONTROLLED IMAGE FROM BLACK & WHITE GIFS |
|--------------------------------------------------------------------------------------**/
// Select Source Main Pictures
if ($product1 == "DSG Overalls") {
$layer1_file = "dsg_yoke.gif";
$layer2_file = "dsg_main.gif";
$layer3_file = "dsg_out.gif";
} else if ($product1 == "Easy Action Overalls") {
$layer1_file = "ea_yoke.gif";
$layer2_file = "ea_main.gif";
$layer3_file = "ea_out.gif";
} else { // Fall back to DSG (no selection - testing)
$layer1_file = "dsg_yoke.gif";
$layer2_file = "dsg_main.gif";
$layer3_file = "dsg_out.gif";
}
// Select Source Fastening Picture
$fastening == "Zip" ? $layer4_file = "zip.gif": $layer4_file = "dome.gif";
/** COLORS **/
// $col1 vars to hex
$col1 == "White" ? $col1_hex = "#FFFFFF" : "";
$col1 == "Tele Grey" ? $col1_hex = "#AAAAAA" : "";
$col1 == "Grey" ? $col1_hex = "#666666" : "";
$col1 == "Red" ? $col1_hex = "#FF0000" : "";
$col1 == "Med Blue" ? $col1_hex = "#0042AD" : "";
// (...etc etc etc)
// $col2 vars to hex
$col2 == "White" ? $col2_hex = "#FFFFFF" : "";
$col2 == "Tele Grey" ? $col2_hex = "#AAAAAA" : "";
$col2 == "Grey" ? $col2_hex = "#666666" : "";
$col2 == "Red" ? $col2_hex = "#FF0000" : "";
$col2 == "Med Blue" ? $col2_hex = "#0042AD" : "";
// (...etc etc etc)
// Hex 2 rgb functions
function hex2rgb($hex) {
$color = str_replace('#', '', $hex);
$ret = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
return $ret;
}
// Set Col1 & Col2 RGB (Call Hex2rgb)
$col1_rgb = hex2rgb($col1_hex);
$col2_rgb = hex2rgb($col2_hex);
/**-------------------------------|
| COLOR LAYER IMAGE COMPOSITE |
|-------------------------------**/
// $findblack rgb array
$findblack = array('r' => 0, 'g' => 0, 'b' => 0);
// Function find color index array
function getColorFromColorIndex($color){
$array = array();
$array['r'] = ($color >> 16) & 0xFF;
$array['g'] = ($color >> & 0xFF;
$array['b'] = $color & 0xFF;
return $array;
}
/** FIRST STEP **/
// LAYER 1
$im_src1 = imagecreatefromgif($layer1_file);
for($x = 0;$x < 300;$x++){
for($y = 0;$y < 335;$y++){
$color1 = getColorFromColorIndex(imagecolorat($im_src1,$x,$y));
// BLACK - Set to $col1
if($color1 == $findblack){
$colors1 = imagecolorallocate($im_src1,$col1_rgb['r'],$col1_rgb['g'],$col1_rgb['b']);
imagesetpixel($im_src1,$x,$y,$colors1);
}
}
}
imagealphablending($im_src1, true);
/** NEXT STEP **/
// LAYER 2
$im_src2 = imagecreatefromgif($layer2_file);
for($x = 0;$x < 300;$x++){
for($y = 0;$y < 335;$y++){
$color2 = getColorFromColorIndex(imagecolorat($im_src2,$x,$y));
// BLACK - Set to $col2
if($color2 == $findblack){
$colors2 = imagecolorallocate($im_src2,$col2_rgb['r'],$col2_rgb['g'],$col2_rgb['b']);
imagesetpixel($im_src2,$x,$y,$colors2);
}
}
}
/** MERGE FIRST 2 LAYERS **/
imagecopymerge($im_src1,$im_src2,0,0,0,0, 300,335, 100);
imagedestroy($im_src2);
/** NEXT STEP **/
// LAYER OUTLINE
$im_outline = imagecreatefromgif($layer3_file);
// Set Transparent
$black = imagecolorallocate ($im_outline, 0, 0, 0);
$grey = imagecolorallocate ($im_outline, 0xAC, 0xAC, 0xAC);
$white = imagecolorallocatealpha ($im_outline, 255, 255, 255, 127);
/** NEXT STEP **/
// LAYER FASTENING
$im_fast = imagecreatefromgif($layer4_file);
// Set Transparent
$black = imagecolorallocate ($im_fast, 0, 0, 0);
$white = imagecolorallocatealpha ($im_fast, 255, 255, 255, 127);
/** LAST STEP **/
// MERGE Outline & Fastening to coloured layer
imagecopymerge($im_src1,$im_outline,0,0,0,0, 300,335, 100);
imagecopymerge($im_src1,$im_fast,0,0,0,0, 300,335, 100);
// Destroy last imgs not needed
imagedestroy($im_outline);
imagedestroy($im_fast);
/** OUTPUT **/
header("Content-type: image/jpeg");
imagejpeg($im_src1,"",100);
?>
I would appreciate any feedback
