PHP gevorderde |
|
ik gebruik deze code om een random plaatje te selecteren uit mijn DB, mischien heb je hier wat aan:
<?php
function get_rand_pic($cat, $size, $tmp_dir, $type=0) {
global $conf;
$max_img_id = mysql_result(
mysql_query(
"SELECT MAX(img_id) FROM images
WHERE img_cat = '".$cat."'"
), 0
);
$img_result = false;
$img_dir = $conf['doc_root'] . 'images/';
$th_dir = $conf['doc_root'] . 'images/th/';
while ($img_result === false) {
if ($max_img_id == 0) {
$src_img = imagecreatefromjpeg($img_dir . 'fusionmedia.jpg');
$tgt_img = imagecreate($size[0], $size[1]);
$tmp_file = $tmp_dir . 'tmp_'.rand_str(10).'.jpg';
$img_rnd_x = rand(0, $size[0]);
$img_rnd_y = rand(0, $size[1]);
$copy = imagecopyresized($tgt_img, $src_img, 0, 0, $img_rnd_x, $img_rnd_y, $size[0], $size[1], $size[0], $size[1]);
$new_img = imagejpeg($tgt_img, $tmp_file, 80);
if (!$copy || !$new_img) {
die('falied to create temp image');
}
imagedestroy($src_img);
imagedestroy($tgt_img);
$img_result = true;
return $tmp_file;
}
else {
$rand_img_id = rand(1, $max_img_id);
$img_query = mysql_query(
"SELECT img_file FROM images
WHERE img_cat = '".$cat."'
AND img_id = '".$rand_img_id."'"
) or die(mysql_error());
$img_result = (mysql_num_rows($img_query) == 1) ? true : false;
}
}
if ($type == 0) {
$img_data = mysql_fetch_array($img_query);
$src_img = imagecreatefromjpeg($img_dir . $img_data['img_file']);
$tgt_img = imagecreate($size[0], $size[1]);
$tmp_file = './tmp/tmp_'.rand_str(10).'.jpg';
$img_rnd_x = rand(0, $size[0]);
$img_rnd_y = rand(0, $size[1]);
$copy = imagecopyresized($tgt_img, $src_img, 0, 0, $img_rnd_x, $img_rnd_y, $size[0], $size[1], $size[0], $size[1]);
$new_img = imagejpeg($tgt_img, $tmp_file, 80);
if (!$copy || !$new_img) {
die('falied to create temp image');
}
imagedestroy($src_img);
imagedestroy($tgt_img);
return $tmp_file;
}
else {
$img_data = mysql_fetch_array($img_query);
return $th_dir . $img_data['img_file'];
}
}
?>
<?php function get_rand_pic($cat, $size, $tmp_dir, $type=0) { "SELECT MAX(img_id) FROM images WHERE img_cat = '".$cat."'" ), 0 ); $img_result = false; $img_dir = $conf['doc_root'] . 'images/'; $th_dir = $conf['doc_root'] . 'images/th/'; while ($img_result === false) { if ($max_img_id == 0) { $src_img = imagecreatefromjpeg($img_dir . 'fusionmedia.jpg'); $tgt_img = imagecreate($size[0], $size[1]); $tmp_file = $tmp_dir . 'tmp_'.rand_str(10).'.jpg'; $img_rnd_x = rand(0, $size[0]); $img_rnd_y = rand(0, $size[1]); $copy = imagecopyresized($tgt_img, $src_img, 0, 0, $img_rnd_x, $img_rnd_y, $size[0], $size[1], $size[0], $size[1]); $new_img = imagejpeg($tgt_img, $tmp_file, 80); if (!$copy || !$new_img) { die('falied to create temp image'); } imagedestroy($src_img); imagedestroy($tgt_img); $img_result = true; return $tmp_file; } else { $rand_img_id = rand(1, $max_img_id); "SELECT img_file FROM images WHERE img_cat = '".$cat."' AND img_id = '".$rand_img_id."'" } } if ($type == 0) { $src_img = imagecreatefromjpeg($img_dir . $img_data['img_file']); $tgt_img = imagecreate($size[0], $size[1]); $tmp_file = './tmp/tmp_'.rand_str(10).'.jpg'; $img_rnd_x = rand(0, $size[0]); $img_rnd_y = rand(0, $size[1]); $copy = imagecopyresized($tgt_img, $src_img, 0, 0, $img_rnd_x, $img_rnd_y, $size[0], $size[1], $size[0], $size[1]); $new_img = imagejpeg($tgt_img, $tmp_file, 80); if (!$copy || !$new_img) { die('falied to create temp image'); } imagedestroy($src_img); imagedestroy($tgt_img); return $tmp_file; } else { return $th_dir . $img_data['img_file']; } } ?>
|