Hi everyone,
I'd like someone to help me merging 2 codes to make one. I'd display random image with greyscales.
Here is the code for the random image:
<?php
// include_once 'rand_img.php';
random_image("phpimages/","175","120");
function random_image($dir,$w,$h) {
if(file_exists($dir) AND !ereg("\.\.",$dir) AND !ereg("\.ht",$dir)) {
$d = dir("$dir");
$i = 0;
while (false !== ($entry = $d->read())) {
if(eregi(".jpg|.jpeg|.gif|.png",$entry)) {
$array[$i] = $entry;
$i++;
}
}
$d->close();
$i = $i -1;
$i = rand(0,$i);
if($array[$i] != "") {
echo "<img src=\"$dir$array[$i]\"";
if(isset($w) AND is_numeric($w)) {
echo " width=\"$w\"";
}
if(isset($h) AND is_numeric($h)) {
echo " height=\"$h\"";
}
echo ">";
} else {
echo "Error: Failed to select a random image (does the dir contain any .jpg/.png/gif's?).";
}
} else {
echo "Error: Directory $dir doesn't seem to exist.";
}
}
?>
Now the code to make grey images files:
<?php
// The file you are grayscaling
$file = 'yourfile.jpg';
// This sets it to a .jpg, but you can change this to png or gif if that is what you are working with
header('Content-type: image/jpeg');
// Get the dimensions
list($width, $height) = getimagesize($file);
// Define our source image
$source = imagecreatefromjpeg($file);
// Creating the Canvas
$bwimage= imagecreate($width, $height);
//Creates the 256 color palette
for ($c=0;$c<256;$c++)
{
$palette[$c] = imagecolorallocate($bwimage,$c,$c,$c);
}
//Creates yiq function
function yiq($r,$g,$b)
{
return (($r*0.299)+($g*0.587)+($b*0.114));
}
//Reads the origonal colors pixel by pixel
for ($y=0;$y<$height;$y++)
{
for ($x=0;$x<$width;$x++)
{
$rgb = imagecolorat($source,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
//This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
$gs = yiq($r,$g,$b);
imagesetpixel($bwimage,$x,$y,$palette[$gs]);
}
}
// Outputs a jpg image, but you can change this to png or gif if that is what you are working with
imagejpeg($bwimage);
?>
Maybe someone can help with this? thanks again, Mazh