I have recently looked into how to make PHP images for a project of mine, but I am having a difficult time with it. I am trying to generate a column of 25 squares that are 10 x 10 with each a slightly different color from the other.

I have been able to make a 10 x 10 blue square, but every time I try to run a for loop to make more 10 x 10 squares of different colors, only the blue square is displayed.

This is the code that I am using right now for a blue square

Code:

<?
header( "Content-type: image/png" );
$my_img = imagecreate( 10, 10 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
imagepng( $my_img );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>


How can I output multiple of these generated images?

Thanks.
You could draw multiple 10x10 rectangles onto a single, larger image:


Code:
<?php # Use the full tag as some servers don't support the short tag.

header('Content-type: image/png');

# We have 20 tiles horizontally, 10 tiles vertically.
$tiles_x = 20; $tiles_y = 10;

# Tile width and height.
$tile_w = 10; $tile_h = 10;

# Create the image (make it a "truecolor" one, too, not limited to 256 colours).
$my_img = imagecreatetruecolor($tiles_x * $tile_w, $tiles_y * $tile_h);

# Loop over the all tile positions in X and Y.
for ($y = 0; $y < $tiles_y; ++$y) {
    for ($x = 0; $x < $tiles_x; ++$x) {
       
        # Allocate a random colour.
        $c = imagecolorallocate($my_img, rand(0, 255), rand(0, 255), rand(0, 255));
       
        # Draw the tile as a filled rectangle.
        imagefilledrectangle($my_img, $x * $tile_w , $y * $tile_h , ($x + 1) * $tile_w - 1, ($y + 1) * $tile_h - 1, $c);
       
        # Deallocate.
        imagecolordeallocate($my_img, $c);
    }
}

imagepng($my_img);
imagedestroy($my_img);

?>
Ben beat me to it, but have my simplified version too Razz

Well, the problem as I see it is that you're basically pasting each square over the other so only the last one shows.

What you're going to want to do is change

Code:
$my_img = imagecreate( 10, 10);

to

Code:
$my_img = imagecreate( 10, 250);

and use imagefilledrectangle, which will create a solid rectangular/square block of whatever size and color you want.

Then, loop that code 25 times, each time incrementing the y position arguments of imagefilledrectangle by 25. Should work then, I think.
I am just using this script on my server which has the short tag enabled but thanks for pointing possible problems out.

Both of your ideas would work but in my situation, I am trying to generate 25 different images rather than just one big image with different colored squares in it.

Is it possible for me to have my 10 x 10 colored squares but display them as separate images?
Oh hahah, wow, epic misinterpretation. Sorry about that, kpa! Sad

Anyway, I'm not quite sure on that one. I'll leave that to the better professionals.
Yes, though you'll need an HTML file that references your image file 25 times. What exactly are you trying to do?
Well this is the big picture. I am trying to make 26 (not 25, I mistyped two times in a row) different 10 x 10 colored squares that would represent letters of the alphabet.

My main goal in the end is to make some type of picture cipher that could interpret the colored squares as characters from the alphabet so that I can encipher and decipher coded images.

It is just a small project to keep me from dying of boredom during summer until the school year starts again.

In the end, this picture cipher would be able to translate plain text into an image filled with 1 x 1 colored squares (they are 10 x 10 for now so that I can actually see them) and also be able to decode an image back into plain text.

So, in the end I would need more than 26 colored squares, but right now I am starting off with the basic lower case letters.

Basically, I just need to make a php script that can save 26 different colored images into a folder on my website that are named with the letters of the alphabet.

It's kind of a silly/awesome idea of a project but it's good experience and it is keeping me busy. Very Happy
Ah, I see. One way to do it would be to make your image-generating script accept a GET parameter that would correspond to the letter it was representing:


Code:
<?php

$colors = array(
    'a'=>array(0, 0, 255),
    'b'=>array(0, 255, 0),
    'c'=>array(255, 0, 0),
    'd'=>array(255, 0, 255),
    # ... etc ...
);

if (isset($_GET['c']) && array_key_exists($_GET['c'], $colors)) {
    $color = $colors[$_GET['c']];
    header( "Content-type: image/png" );
    $my_img = imagecreate( 10, 10 );
    $background = imagecolorallocate( $my_img, $color[0], $color[1], $color[2] );
    imagepng( $my_img );
    imagecolordeallocate( $background );
    imagedestroy( $my_img );
}

?>


$colors maps letters to colours, and you would specify the letter in the c GET parameter - if the PHP file was letter.php, you could do this in your HTML file to spell "bad":


Code:
<img src="letter.php?c=b" alt="b" /><img src="letter.php?c=a" alt="a" /><img src="letter.php?c=d" alt="d" />
You could alternatively have your PHP script generate a single file with the requisite images inlined via data: URIs, which is particularly useful if you only want to keep the images for a single page load.

I don't have any experience with PHP so I shall make no attempt at code to do so, but you'd want to generate the raw image data somehow then dump it into an image tag something like this (in this case base64 encode it first), I think:
Code:
<img src="data:image/png;base64,[image data goes here]" />


Downside of this approach is not-quite universal support among browsers (particularly IE), though. Wikipedia has the details on that.

Oh, it seems PHP even has a function to do it: data_uri().
If you only have 26 images, why not just create them once, even by hand, and save them, saving yourself the PHP overhead of regenerating each one? And I bet that caching is the culprit for your problem; someone probably already said that.
To further that, you would completely reload a page using Control-R in Firefox. Not sure for any other browser.
_player1537 wrote:
To further that, you would completely reload a page using Control-R in Firefox. Not sure for any other browser.
Yeah, but if he's passing the arguments to the same PHP script as <img src="whatever.php?stuff" />, his browser might only bother to grab it once without some proper server-side cache-busting.
My problem was solved, thanks for the help everyone. This problem is closely related to this thread: http://cemetech.net/forum/viewtopic.php?t=6518. I'll be posting the project that I made after finding the solution to my problems soon in the Your Projects section for feedback.
kpa4941 wrote:
My problem was solved, thanks for the help everyone. This problem is closely related to this thread: http://cemetech.net/forum/viewtopic.php?t=6518. I'll be posting the project that I made after finding the solution to my problems soon in the Your Projects section for feedback.
Excellent, glad to hear it. I'd suggest looking at your third thread, the one about jump tables and multi-dimensional arrays that Kllrnohj was posting in.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement