Jump to content

How can i desply more than one image?


Jragon

Recommended Posts

Hey,

 

I would like to be able to show more than one image that is made by php on the fly.

 

The two codes i want to join:

hitcounter

<?php

/**
* @author Jragon
* @copyright 2010
*/
getHits();

function getHits()
{

    $file = "hits.txt";
    $fh = fopen($file, "r");
    $hits = @fread($fh, filesize("$file"));
    fclose($fh);
    if ($hits <= 0){
    $hits =1;
    } else {
    ++$hits;
    }
    $fh = fopen($file, "w+");
    fwrite($fh, "$hits");
    fclose($fh);
    getImage("$hits");

}

function getImage($string)
{
    //Make $string to to a string
    $string = "$string";
    //Define fontsize
    $font = 5;
    //use imagefontwidth to find out the widht of each charicter,
    //then times it by how many charicters theere are,
    //and add some padding.
    $width = imagefontwidth($font) * strlen($string) + 10;
    //height of a charicter, and add some padding.
    $height = imagefontheight($font) + 10;
    //random color thingy.
    $r = rand(25, 250);
    $g = rand(25, 250);
    $b = rand(25, 250);
    //create a blank image using the dimsions set above.
    $image = imagecreate($width, $height);
    //set the background color
    $background_color = imagecolorallocate($image, 0, 0, 0);
    //set the text color using the random integers made above.
    $text_color = imagecolorallocate($image, $r, $g, $b);
    //Put the text into the image.
    imagestring($image, $font, 6, 6, $string, $text_color);
    //Change the type of webpage so you see the image
    header("Content-type: image/png");
    //put everything together and make the image
    imagepng($image);
    //free up some space on the temp drive
    imagedestroy($image);
}
?>

and my random quote thingy:

<?php

/**
* @author Jragon
* @copyright 2010
*/

    $textfile = "quotes.txt";
    $quotes = array();
    if(file_exists($textfile)){
        $quotes = explode("\n",file_get_contents($textfile));
        srand ((float) microtime() * 10000000);
        $string = $quotes[array_rand($quotes)];	
        text_to_image($string, 800);
    } else {
        $string = "Sig file non-existant...";
    }
    function text_to_image($text, $image_width)
    {
$r = rand(25, 250);
    	$g = rand(25, 250);
    	$b = rand(25, 250);        
$font = 5;
        $line_height = 15;
        $padding = 5;
        $text = wordwrap($text, ($image_width/10));
        $lines = explode("\n", $text);
        $image = imagecreate($image_width,((count($lines) * $line_height)) + ($padding * 2));
        $background = imagecolorallocate($image, 0, 0, 0);
        $colour = imagecolorallocate($image, $r, $g, $b);
        imagefill($image, 0, 0, $background);
        $i = $padding;
        foreach($lines as $line){
            imagestring($image, $font, $padding, $i, trim($line), $colour);
            $i += $line_height;
        }
        header("Content-type: image/jpeg");
        imagejpeg($image);
        imagedestroy($image);
        exit;
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/221544-how-can-i-desply-more-than-one-image/
Share on other sites

I rarely use the image functions but it looks like you are familiar with them.  Each of your functions needs to return the image instead of displaying it.  Then use the imagecreate...() and imagecopy() functions to build one from the two.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.