如何在php中创建图像

I am creating image with php

code

$src = array ("22.jpg","33.jpg","44.jpg","55.jpg","66.jpg","77.jpg");    
                $imgBuf = array (); 
                foreach ($src as $link) 
                { 
                   switch(substr ($link,strrpos ($link,".")+1)) 
                   { 
                       case 'png': 
                           $iTmp = imagecreatefrompng($link); 
                           break; 
                       case 'gif': 
                           $iTmp = imagecreatefromgif($link); 
                           break;                
                       case 'jpeg':            
                       case 'jpg': 
                           $iTmp = imagecreatefromjpeg($link); 
                           break;                
                   } 
                   array_push ($imgBuf,$iTmp); 
                } 

                $iOut = imagecreatetruecolor ("35","210") ; 

                imagecopy ($iOut,$imgBuf[0],0,0,0,0,imagesx($imgBuf[0]),imagesy($imgBuf[0])); 
                imagedestroy ($imgBuf[0]); 
                imagecopy ($iOut,$imgBuf[1],0,35,0,0,imagesx($imgBuf[1]),imagesy($imgBuf[1])); 
                imagedestroy ($imgBuf[1]); 
                imagecopy ($iOut,$imgBuf[2],0,70,0,0,imagesx($imgBuf[2]),imagesy($imgBuf[2])); 
                imagedestroy ($imgBuf[2]); 
                imagecopy ($iOut,$imgBuf[3],0,105,0,0,imagesx($imgBuf[3]),imagesy($imgBuf[3])); 
                imagedestroy ($imgBuf[3]); 
                imagecopy ($iOut,$imgBuf[4],0,140,0,0,imagesx($imgBuf[4]),imagesy($imgBuf[4])); 
                imagedestroy ($imgBuf[4]); 
                imagecopy ($iOut,$imgBuf[5],0,175,0,0,imagesx($imgBuf[5]),imagesy($imgBuf[5])); 
                imagedestroy ($imgBuf[5]);
                imagepng($iOut); 

            //header ( 'Content-type:image/png' );
                // save the img to directory 

                $char='0123456789';
                $length=10;
                $max_i=strlen($char)-1;
                $value='';
                for($j=0;$j<$length;$j++)
                {
                $value.=$char{mt_rand(0,$max_i)};
                }
                $imageid=$value;

it giving error on page like

‰PNG IHDR#ÒOuî² CIDATxœíÖ]ŒdÇUðÿ9§êÞþ˜™]ïìÇlbc‚ÀІ(@dQž @"$ƒ”<°E‚XXY~ E D¼€"!ÂI’;Q$£°M¼ïzw½_3ÓÓÝ÷Ö×9<ô̬óÄRê§V«Õ·»ÿU]uÏ)JÙ ‚Ì€˜7€wâÕ½«¯^»Óçþå™åfȹ-š© *6›çŸù¹÷ðêðÌ[í‰%üÈ]؆0‡8@@ÙL3¼Än‘×ãÞ«·žxôñ»×69àôú©e?ÊóÙÊx™’W+Ü”˜C1Vö4Üîowq÷zwë?ýI·u§,É~@½™@PF¼Ž'>ô»ÇýÚ‘áÊòêÒh8HëᬠŒ,eïÄ9îK ¡Lº½4ëºÙüÖ7Óä¿ø—Ø–€‡(™€b’ »øØ¹ß\K÷o¾ãÌúI&*‚lÙÁR4P£ÄEÕ‰P¢ó!Î}ë:ëEdÄ-gX_.߸òòök¯ûŸúöߣMp~1'ç($àßo½-­ÿÀÛï][Z/1Ëlo‡PaÕa#¥›{4Óɘ4—\Š9í%©FË'7ÓÕñÝîƒÅ_h¹@øNþÄû}ðØ­?6NsLªêü0ö¡q%ö#fÖ2²Óbð’´¨ªjS¸,¾GE.ì\x~÷ùO>ûØ x°"*¶ñ±|äÄÚ™ñʱÉv§Á4X‰¶·Óõ†S‹½€½Ž£QFècÅcŒ¡OH ˆ²ÝˤϷws›ë›'è$þûòþ:931þkû¸-Z;1Ÿv%ôÐ’c ߸&Lûaëgq:Ó>Í•àÃñ`¼:;EŠÉHBL­¸ºv<š¤É€†§†Ç¿õ¥¯ŸýÉì');¾ó·7ëÜ' 9ö¥ˆH\C–Çäß¼ùæ%Ýž #µs­¦áéptU–}±lST°F:£#úãÏ}ù«g?HÊ Q<÷¥=5>–»b¢M¹uV †½8»<»úûŸ{ooa€xmïÓ|üÈÚÀ›Ï}/

how i can solve this

What you're seeing is the actual content of the image that's generated. You do need to specify content/type, otherwise it will be assumed to be text/plain or text/html. Your image seem to be PNG, so

header("Content-type: image/png")

should be sufficient. I can see this line commented out - but it needs to be included. One note though: it needs to go before the actual image data is output, so you need to move it to the top of your script (or at least above imagephp call).

EDIT: If you want to save the resulting image into a file rather than output it to the browser, then you need to pass the second parameter to imagepng function:

imagepng($iOut, $myfilename)

See Imagephp documentation for more details

EDIT 2: If you need to get the content of the created image to use elsewhere, you can use this trick:

ob_start();
imagephp($iOut);
$image_data = ob_get_clean();

Now you got the resulting image data in a variable and you can continue with your script.