Page 1 of 1

php help needed please using foreach $key as a file name

Posted: Thu Jun 17, 2010 2:05 pm
by alforddm
I'm writting code to output some images.

The code

Code: Select all

foreach($stringvar as $key=>$value){
echo "<p style=\"font-size:20px;\"
   onmouseover=\"ShowPart('uniquename$j');\" onmouseout=\"HidePart('uniquename$j');\";
   >$key has a $value% Chance of Occuring<br></p>";
do some stuff

Code: Select all

foreach ($colorpic as $img){
imagealphablending($img, true); // setting alpha blending on
imagesavealpha($img, true); // save alphablending setting (important)
imagecopy($dest,$img,0,0,0,0,816,542);}
echo "<div id=\"uniquename$j\" style=\"display:none;float:right;position: relative; top: -250px; clear:both;width:407px;\"><p>";
imagepng($dest,"images/img$j.png");
imagedestroy($dest);
print "<img src=\"images/img$j.png\" width=\"407\" height=\"271\">";
echo "</p></div>";
$j++;
}
This works perfectly but I would like to save the files by the $key value in the arrays so that the processor doesn't have to regenerate a file that has already been created.

This however does not work. Only one picture will be output to the source directory. It will be the last picture generated named with the first $key. If you view the source however it is calling all the images by $key as it should.

Code: Select all

imagepng($dest,"images/$key.png");
imagedestroy($dest);

print "<img src=\"images/$key.png\" width=\"407\" height=\"271\">";
I'm self taught so I'm probably doing something stupid but a solution is evading me.

Thanks for your help,

Daylene

forgot to add I'm using the lastest xampp for development on windows
* Apache 2.2.14 (IPv6 enabled) + OpenSSL 0.9.8l
* MySQL 5.1.41 + PBXT engine
* PHP 5.3.1
* phpMyAdmin 3.2.4
* Perl 5.10.1
* FileZilla FTP Server 0.9.33
* Mercury Mail Transport System 4.72

Re: php help needed please

Posted: Thu Jun 17, 2010 4:34 pm
by cpetercarter
You have split the code between two blocks, so I have to guess a bit about how they relate to each other. I am right that the code is structured like this?

Code: Select all

foreach($stringvar as $key=>$value){
      // do some stuff
      foreach ($colorpic as $img){
             // do more stuff including saving the image files
            }
      }      
If you think about it, within each iteration of the outer ($stringvar) loop, the value of $key will remain constant. So, if you save each $img in the inner loop with a name $key, each image will have the same name as the image generated in the previous iteration, and will therefore overwrite it. So you are left with only the image generated by the last iteration. Does this give you a clue about how to fix it?

Re: php help needed please

Posted: Thu Jun 17, 2010 4:43 pm
by alforddm
No, the second foreach is used to layer images that are stored in an array and is closed before the image is saved or sent to the browser.

foreach($stringvar as $key=>$value){
// do some stuff
foreach ($colorpic as $img){
// layer the image
}
//save layered image to file
//output saved file to the browser
}

Re: php help needed please using foreach $key as a file name

Posted: Thu Jun 17, 2010 5:23 pm
by cpetercarter
OK, I understand now. You might start by looking at the $strigvar array If you do a print_r on it, does it contain the values you expect?

Re: php help needed please using foreach $key as a file name

Posted: Thu Jun 17, 2010 6:55 pm
by alforddm
Yes, it does. In fact, I do several operations on the array and everything works perfectly. The output to the browser even reads correctly (correct files names and call). The images just aren't created. I'm not understanding why it works with img$j.png and not $key.png. That is the only difference.

Re: php help needed please using foreach $key as a file name

Posted: Fri Jun 18, 2010 2:41 am
by cpetercarter
A possibility is that some of the values for $key contains characters which cannot be used in filenames in Windows.

It would help me if you could post the complete code (ie the code which does not work), from foreach($stringvar ... to the end of the foreach loop.

Re: php help needed please using foreach $key as a file name

Posted: Fri Jun 18, 2010 11:05 am
by alforddm
Are you sure you want to see all of it? It's just a bunch of if else statements looking for stuff in $key and pushing the results to a different array. Its probably quite messy compared to what a professional could do.

Code: Select all

$j=0;
echo "<div style=\"margin-left: 15%;
    margin-right: auto; padding-bottom:100px;
    
\">";
//start foreach statement to print $key and $value and create hidden div for each result
foreach($foalStringsTotal as $key=>$value){
echo "<p style=\"font-size:20px;\"
   onmouseover=\"ShowPart('uniquename$j');\" onmouseout=\"HidePart('uniquename$j');\";
   >$key has a $value% Chance of Occuring<br></p>
";

$color=array();
$colorpic=array();

//search $key and if a result is present add that result to one array and the img that goes with that result to another
if (strchr($key,EE)){
	$dest = imagecreatefrompng("images/black.png");
	imagealphablending($dest, true); // setting alpha blending on
	imagesavealpha($dest, true); // save alphablending setting (important)
 if (strchr($key,AAt)){
		
		$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		array_push($color,"Base Color is Bay Homozygous for Extension and Carries both Seal and Wild-type Alleles at Agouti");
 		}
    elseif (strchr($key,Aa)){
		$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
    	array_push($color,"Base Color is Bay Homozygous Extension and Heterozygous for the Wild-Type at Agouti");
    }
    elseif (strchr($key,AtAt)){
    	$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		$tmp = imagecreatefrompng("images/seal.png");
		array_push($colorpic,$tmp);
    	array_push($color,"Base Color is Seal/Brown Homozygous for Extension 
    	Homozygous for Seal at Agouti");}
    elseif (strchr($key,Ata)){
    	$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		$tmp = imagecreatefrompng("images/seal.png");
		array_push($colorpic,$tmp);    
    	array_push($color,"Base Color is Seal/Brown Homozygous for 
    	Extension Heterozygous for Seal at Agouti");}
    elseif (strchr($key,AA)){
		$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		array_push($color,"Base Color is Bay Homozygous for
		 Extension and Wild-Type at Agouti");}
 	elseif (strchr($key,AtA)){
    	$tmp=imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
 		array_push($color,"Base Color is Bay Homozygous for Extension 
 		Carries both Seal and Wild-Type Alleles at Agouti");}   
    else {
    	array_push($color,"Base Color is Black Homozygous for Extension and Homozygous for the recessive Alle at Agouti");
        }
}
elseif (strchr($key,Ee)){
	$dest = imagecreatefrompng("images/black.png");
	imagealphablending($dest, true); // setting alpha blending on
	imagesavealpha($dest, true); // save alphablending setting (important)
if (strchr($key,AAt)){
		
		$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		array_push($color,"Base Color is Bay Heterozygous for Extension and Carries both Seal and Wild-type Alleles at Agouti");}
    elseif (strchr($key,Aa)){
		
		$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
    	array_push($color,"Base Color is Bay Heterozygous 
    	Extension and Heterozygous for the Wild-Type at Agouti");
    }
    elseif (strchr($key,AtAt)){
        $tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		$tmp = imagecreatefrompng("images/seal.png");
		array_push($colorpic,$tmp);
    	array_push($color,"Base Color is Seal/Brown Heterozygous for 
    	Extension Homozygous for Seal at Agouti");}
    elseif (strchr($key,Ata)){
        $tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		$tmp = imagecreatefrompng("images/seal.png");
		array_push($colorpic,$tmp);    
    	array_push($color,"Base Color is Seal/Brown Heterozygous for 
    	Extension Heterozygous for Seal at Agouti");}
    elseif (strchr($key,AA)){
		$tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
		array_push($color,"Base Color is Bay Heterozygous for 
		Extension and Wild-Type at Agouti");}
 elseif (strchr($key,AtA)){
        $tmp = imagecreatefrompng("images/bay.png");
		array_push($colorpic,$tmp);
 		array_push($color,"Base Color is Bay Heterozygous for Extension 
 		Carries both Seal and Wild-Type Alleles at Agouti");}   
    else {
    	array_push($color,"Base Color is Black Heterozygous for Extension and Homozygous for the recessive Alle at Agouti");
        }
}


else {
    array_push($color,"Base Color is Chestnut Homozygous for the recessive Alle at Extension");
    $dest = imagecreatefrompng("images/red.png");
    imagealphablending($dest, true); // setting alpha blending on
	imagesavealpha($dest, true); // save alphablending setting (important)
	if (strchr($key,AAt))
        array_push($color," Carries both Seal and Wild-type Alleles at Agouti");
    elseif (strchr($key,Aa)){
            array_push($color," Heterozygous for the Wild-Type at Agouti");
    }
    elseif (strchr($key,AtAt))
        array_push($color," Homozygous for Seal at Agouti");
    elseif (strchr($key,Ata))
            array_push($color," Heterozygous for Seal at Agouti");
    elseif (strchr($key,AA))
        array_push($color," Homozygous for Wild-Type at Agouti");
elseif (strchr($key,AtA))
            array_push($color," Carries both Seal and Wild-Type Alleles at Agouti");   
        else {
       array_push($color," Homozygous for the recessive Alle at Agouti");
        }
}
	

if (strchr($key,"CC")){
    array_push($color,"<br>Homozygous Cream");
    $tmp = imagecreatefrompng("images/DD.png");
		array_push($colorpic,$tmp);}
if (strchr($key,Ccr)){
	array_push($color,"<br>Heterozygous Cream");
	if (strchr($key,E)){
		if (!strchr($key,aa))	{
			$tmp = imagecreatefrompng("images/creamblack.png");
			array_push($colorpic,$tmp);}}
    
    else {	$tmp = imagecreatefrompng("images/redcream.png");
		array_push($colorpic,$tmp);}}
if (strpos($key,prlcr) || (strpos($key,crprl))){
	array_push($color,"<br>Heterozygous Pearl");
}
	
if (strpos($key,Cprl)){
	array_push($color,"<br>Heterozygous Cream and Pearl");
	$tmp = imagecreatefrompng("images/DD.png");
	array_push($colorpic,$tmp);}
if (strpos($key,prlprl)){
	array_push($color, "<br>Homozygous Pearl");
	$tmp = imagecreatefrompng("images/champagne.png");
	array_push($colorpic,$tmp);}
if (strchr($key,DD)){
	$tmp = imagecreatefrompng("images/dun.png");
	array_push($colorpic,$tmp);
    array_push($color,"<br>Homozygous Dun");}
if (strchr($key,D)){
    array_push($color,"<br>Heterozygous Dun");
	$tmp = imagecreatefrompng("images/dun.png");
	array_push($colorpic,$tmp);}
if (strchr($key,CHCH)){
    array_push($color,"<br>Homozygous Champagne");
	$tmp = imagecreatefrompng("images/champagne.png");
	array_push($colorpic,$tmp);}
if (strchr($key,Ch)){
    array_push($color,"<br>Heterozygous Champagne");
	$tmp = imagecreatefrompng("images/champagne.png");
		array_push($colorpic,$tmp);}
if (strchr($key,ZZ)){
    array_push($color,"<br>Homozygous Silver");
	$tmp = imagecreatefrompng("images/silver.png");
		array_push($colorpic,$tmp);}
if (strchr($key,Z)){
    array_push($color,"<br>Heterozygous Silver");
	$tmp = imagecreatefrompng("images/silver.png");
		array_push($colorpic,$tmp);} 
if (strchr($key,RR)){
	array_push($color,"<br>Homozygous Roan");
	$tmp = imagecreatefrompng("images/roan.png");
		array_push($colorpic,$tmp);}
if (strchr($key,R)){
    array_push($color,"<br>Heterozygous Roan");
$tmp = imagecreatefrompng("images/roan.png");
		array_push($colorpic,$tmp);}
if (strchr($key,TT)){
    array_push($color,"<br>Homozygous Tobiano");
$tmp = imagecreatefrompng("images/tobiano.png");
		array_push($colorpic,$tmp);}
if (strchr($key,T)){
    array_push($color,"<br>Heterozygous Tobiano");
$tmp = imagecreatefrompng("images/tobiano.png");
		array_push($colorpic,$tmp);}
if (strchr($key,O)){
    array_push($color,"<br>Heterozygous Frame");
$tmp = imagecreatefrompng("images/frame.png");
		array_push($colorpic,$tmp);}
if (strchr($key,SBSB)){
    array_push($color,"<br>Homozygous Sabino");
$tmp = imagecreatefrompng("images/DW.png");
		array_push($colorpic,$tmp);}
if (strchr($key,SB)){
    array_push($color,"<br>Heterozygous Sabino");
$tmp = imagecreatefrompng("images/sabino.png");
		array_push($colorpic,$tmp);}     
if (strchr($key,SPLSPL)){
	array_push($color,"<br>Homozygous Splash");
$tmp = imagecreatefrompng("images/spsp.png");
		array_push($colorpic,$tmp);}
if (strchr($key,SPL)){
    array_push($color,"<br>Heterozygous Splash");
$tmp = imagecreatefrompng("images/sp.png");
		array_push($colorpic,$tmp);}
if (strchr($key,LPLP)){
    array_push($color,"<br>Homozygous Leopard Complex");
$tmp = imagecreatefrompng("images/LPLP.png");
		array_push($colorpic,$tmp);}
if (strchr($key,LP)){
    array_push($color,"<br>Heterozygous Leopard Complex");
$tmp = imagecreatefrompng("images/LP.png");
		array_push($colorpic,$tmp);} 
if (strchr($key,W)){
	array_push($color,"<br>Heterozygous Dominant White");
$tmp = imagecreatefrompng("images/DW.png");
		array_push($colorpic,$tmp);}
if (strchr($key,RBRB))	{
	array_push($key,"<br>Homozygous Rabicano");
$tmp = imagecreatefrompng("images/rabicano.png");
		array_push($colorpic,$tmp);}
if (strchr($key,RB)){
	array_push($color,"<br>Heterozygous Rabicano");
$tmp = imagecreatefrompng("images/rabicano.png");
		array_push($colorpic,$tmp);}
if (strchr($key,GG)){
    array_push($color,"<br>Homozygous Gray");
    $tmp = imagecreatefrompng("images/gray.png");
		array_push($colorpic,$tmp);}
if (strchr($key,G)){
    array_push($color,"<br>Heterozygous Gray");
    $tmp = imagecreatefrompng("images/gray.png");
		array_push($colorpic,$tmp);}
  $colors=implode(" ",$color); 

//layer the imges in the $colorpic array	
foreach ($colorpic as $img){
imagealphablending($img, true); // setting alpha blending on
imagesavealpha($img, true); // save alphablending setting (important)
imagecopy($dest,$img,0,0,0,0,816,542);}
//added for testing take the spaces out of $key and save to a new variable 
$name = str_replace (" ", "", $key);		
//create hidden div
echo "<div id=\"uniquename$j\" style=\"display:none;float:right;position: relative; top: -250px; clear:both;width:407px;\"><p>";
//send layered img to file
imagepng($dest,"images/$name.png");
imagedestroy($dest);
//print layered img 
print "<img src=\"images/$name.png\" width=\"407\" height=\"271\">";
echo "<br>$colors";


echo "</p></div>";

$j++;

}
echo "</div>";
I've tried substituting several different things for $key...$name$j actually works as does $j$name. img$name does not work nor does $name.img I'm completely dump struck as to why $name$j would work but not $name

Re: php help needed please using foreach $key as a file name

Posted: Fri Jun 18, 2010 4:11 pm
by cpetercarter
A possibility is that $name has the same value in several different iterations.

It would be useful to test whether the images are being created and then overwritten, or whether they are not being created at all.

If you replace line 256 with

Code: Select all

if(imagepng($dest,"images/$name.png")) echo "This is iteration " . $j . " Image ".$name.".png created.<br />";
else echo "This is iteration ".$j. ".No image created</br>";
what output do you get (from this echo statement)?

Re: php help needed please using foreach $key as a file name

Posted: Fri Jun 18, 2010 5:50 pm
by alforddm
I can't get to the computer the files are on atm but all the keys in the array are unique. I made sure of that for other reasons before any output. Therefore $name should be unique for each iterations as well.

I will still try your code though and let you know as soon as my kid finishes his dungon. :lol:

Re: php help needed please using foreach $key as a file name

Posted: Fri Jun 18, 2010 10:26 pm
by alforddm
Well this is a bit embarrassing... This afternoon I restarted my computer. I was still getting wrong results but they weren't the same wrong results (some of the img were actually correct but some where still wrong). Which made me curious so I uploaded the files to host and walla everything works like it should. I still have no clue what is causing the problem it still doesn't work right on my home computer (which makes testing a pain) but it works correctly on my live server and I guess that is what counts. ATM I'm just going to blame Windows.

Thank you so much for your help.

Daylene

Re: php help needed please using foreach $key as a file name

Posted: Sat Jun 19, 2010 12:58 am
by cpetercarter
Good. I am glad that it works on your server. I have no idea what the problem in XAMPP could have been. I always blame Windows too when unexplained things happen!