imagepng() - sending image to browser

Discussion in 'PHP' started by snicher, Nov 27, 2007.

  1. #1
    Hello

    I am beginning to explore the image stuff in PHP. I was trying something real simple: Creating an image, and sending it directly to the browser. But somehow I mess it up. Please help ..

    Regards, Snicher


    
    <html><head><title>PHP fun</title></head><body>
    
    <SCRIPT LANGUAGE="PHP">
    
    $picName = "funstuff.png";
    
    // Size
    $picWidth = 320;
    $picHeight = 240;
    
    $myPic = imagecreatetruecolor($picWidth, $picHeight);
    
    $bgCol = ImageColorAllocate($myPic, 225, 225, 225);
    
    for  ( $myX = 0; $myX < $picWidth; $myX++) {
    	for  ( $myY = 0; $myY < $picHeight; $myY++) {
    		//$newColor = ImageColorDeAllocate($myPic, rand(0,255), rand(0,255), rand(0,255));
    		$newColor = ImageColorAllocate($myPic, rand(0,255), rand(0,255), rand(0,255));
    		//$newColor = ImageColorAllocate($myPic, 60, 200, 111);
    		imagesetpixel($myPic, $myX, $myY, $newColor);
    	}
    }
    
    header("Content-type: image/png");
    
    imagePNG($myPic);
    ImageDestroy($myPic);
    
    </SCRIPT>
    </body></html>
    
    PHP:
     
    snicher, Nov 27, 2007 IP
  2. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <SCRIPT LANGUAGE="PHP">
    header("Content-type: image/png");
    
    $picName = "funstuff.png";
    
    // Size
    $picWidth = 320;
    $picHeight = 240;
    
    $myPic = imagecreatetruecolor($picWidth, $picHeight);
    
    $bgCol = ImageColorAllocate($myPic, 225, 225, 225);
    
    for  ( $myX = 0; $myX < $picWidth; $myX++) {
        for  ( $myY = 0; $myY < $picHeight; $myY++) {
            //$newColor = ImageColorDeAllocate($myPic, rand(0,255), rand(0,255), rand(0,255));
            $newColor = ImageColorAllocate($myPic, rand(0,255), rand(0,255), rand(0,255));
            //$newColor = ImageColorAllocate($myPic, 60, 200, 111);
            imagesetpixel($myPic, $myX, $myY, $newColor);
        }
    }
    
    
    imagePNG($myPic);
    ImageDestroy($myPic);
    
    </SCRIPT>
    PHP:
     
    selling vcc, Nov 27, 2007 IP
  3. Jackel.ca

    Jackel.ca Well-Known Member

    Messages:
    108
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    128
    #3
    EDIT - selling vcc got it :p
     
    Jackel.ca, Nov 27, 2007 IP
  4. snicher

    snicher Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    wow, that was fast. Thanks guys! :D

    Hey, is it possible to include some html, or is that method image only?
     
    snicher, Nov 27, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The function is for images only. To include HTML, you have to remember that HTML itself doesn't have images in it.... it only has links to images. Therefore, PHP can't do it as you are thinking, either.

    What you have to do is have your PHP script create the HTML that then points the browser to another PHP script for the actual image itself. It's late here, so hopefully that makes sense.
     
    TwistMyArm, Nov 27, 2007 IP
  6. snicher

    snicher Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, you mean like:

    <img src="funstuff.php">

    That makes total sense. Cool! :D
     
    snicher, Nov 28, 2007 IP