How can I put a description under image? - php

I have made a database with blob images and descriptions. The description is connected to the blob data with the same ID. In this code, the description is displayed right next to its image.
Show Images Page
$query = "SELECT * FROM `photo`.`photo`";
$query_run = mysql_query($query);
while ($data = mysql_fetch_array($query_run)) {
echo '<'.'img src="id.php?id='.$data['id'].'">';
$short_description = substr($data['description'], 0, 10);
$long_description = $data['description'];
echo $long_description;
}
echo "<br><a href='Photosite.php'>Upload a Photo</a>";
Converting blobs to jpegs for display page
$id = abs($_GET['id']);
$query = mysql_query("SELECT * FROM `photo`.`photo` WHERE id='$id'");
$data = mysql_fetch_array($query) or die (mysql_error());
$image = $data['image'];
$description = $data['description'];
$jpgimage = imagecreatefromstring($image);
$image_width = imagesx($jpgimage);
$image_height = imagesy($jpgimage);
$new_size = ($image_width + $image_height)/($image_width*($image_height/45));
$new_width = $image_width * $new_size;
$new_height = $image_height * $new_size;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($new_image, $jpgimage, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
$imagearray = imagejpeg($new_image, null);
header('Content-type: image/jpeg');
echo $imagearray;
My question is how can I get the description to go UNDER the image displayed? and not have image next to description next to image next to description and so forth...?
Big help thanks! And yes I know some of my functions are out-of-date, no need to remind me thanks!

If you want images next to each other with descriptions below each image, then:
<?php
$max_width = '200px'; // Set this to whatever the image's width is.
$query = "SELECT * FROM `photo`.`photo`";
$query_run = mysql_query($query);
while ($data = mysql_fetch_array($query_run)) {
// short_description doesn't look like it's being used... ??
$short_description = substr($data['description'], 0, 10);
$long_description = $data['description'];
echo '<div style="float:left;width:'.$max_width.';">';
echo ' <img src="id.php?id='.$data['id'].'" />';
echo ' <br style="clear:both;" />';
echo $long_description;
echo '</div>';
}
echo "<br style="clear:both;"><a href='Photosite.php'>Upload a Photo</a>";
Otherwise if you want images underneath each other with descriptions beneath image, then:
<?php
$query = "SELECT * FROM `photo`.`photo`";
$query_run = mysql_query($query);
while ($data = mysql_fetch_array($query_run)) {
// short_description doesn't look like it's being used... ??
$short_description = substr($data['description'], 0, 10);
$long_description = $data['description'];
echo '<div>';
echo ' <img src="id.php?id='.$data['id'].'">';
echo ' <br style="clear:both;" />';
echo $long_description;
echo '</div>';
}
echo "<br style="clear:both;"><a href='Photosite.php'>Upload a Photo</a>";

Try a line break before the description. You could also consider a simple two row one column table.

Related

How to store base64 image create from string in database in php?

I want to store my image create from string into database in php.But there is a error.It shows an error message. The message is Warning: Undefined array key 1 in C:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php on line 29
Warning: Undefined array key 1 in C:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php on line 30
Any ideas how i can solve/fix this.Thanks in advance...
Here is my demo code...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button>click</button>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
let text = "What is Lorem Ipsum?Lorem Ipsum is ";
$.ajax({
url:'create_image_php.php',
type:'post',
data:{"text":text},
success:function(data){
alert(data);
console.log(data);
}
})
})
})
</script>
</body>
</html>
create image_php.php
<?php
$text = $_POST['text'];
$image_width = 320; // pixels
text_to_image($text, $image_width);
function text_to_image($text, $image_width, $colour = array(0,244,34), $background = array(0,0,0))
{
include 'conn.php';
$font = 5;
$line_height = 30;
$padding = 10;
$text = wordwrap($text, ($image_width/10));
$lines = explode("\n", $text);
$image = imagecreate($image_width,480);
$background = imagecolorallocate($image, $background[0], $background[1], $background[2]);
$colour = imagecolorallocate($image,$colour[0],$colour[1],$colour[2]);
imagefill($image, 0, 0, $background);
$i = $padding;
foreach($lines as $line){
imagestring($image, $font, $padding, $i, trim($line), $colour);
$i += $line_height;
}
ob_start();
imagepng($image);
$image= printf('data:image/png;base64,%s', base64_encode(ob_get_clean()));
$image_array_1 = explode(";", $image);
$image_array_2 = explode(",", $image_array_1[1]);
$image = base64_decode($image_array_2[1]);
$imageName = time() . '.png';
$image = file_put_contents($imageName, $image);
$image_file = addslashes(file_get_contents($imageName));
$sql = "INSERT INTO `images`(`image`) VALUES(:image_file)";
$stmt = $conn->prepare($sql);
$stmt->bindparam(':image_file',$image_file);
$query = $stmt->execute();
if($query){
echo "success";
}
else{
echo "error";
}
exit;
} ?>
conn.php
<?php
try{
$conn = new PDO("mysql:host=localhost;dbname=social_site","root","");
}
catch(PDOException $e){
echo "Error:".$e->getMessage();
}
?>
Table Name:images
index:image
$image= printf('data:image/png;base64,%s', base64_encode(ob_get_clean()));
the printf function is used for "Produces output according to format.", it returns the length of the output, not the result of format, try change this code to:
$image = 'data:image/png;base64,'.base64_encode(ob_get_clean();

Can't get GD image to display

my code:
save this file as captcha.php
<?php
ob_start();
session_start();
if(!isset($_POST['submit'])){
echo "<form method=\"post\" action=\"captcha.php\">\n";
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td>Type The Letters You See Below Into the Box</td></tr>\n";
echo "<tr><td align=\"center\"><img src=\"image.php\"></td></tr>\n";
echo "<tr><td align=\"center\"><input type=\"text\" name=\"image\"></td></tr>\n";
echo "<tr><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Check CAPTCHA\"></td></tr>\n";
echo "</table></form>\n";
}else {
$image = $_POST['image'];
if ($image == $_SESSION['string'])
{
echo "<b>Great success!</b>\n";
}
else
{
echo "<em>Failure!</em>\n";
}
}
ob_end_flush();
?>
save this file as image.php
<?php
session_start();
$img = imagecreatetruecolor(118,80);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$red = imagecolorallocate($img, 255, 0, 0);
$grey = imagecolorallocate($img,150, 150, 150);
$pink = imagecolorallocate($img, 200, 0, 150);
$yellow = imagecolorallocate($img, 255, 246, 0);
function randomString($length){
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$str = "";
$i = 0;
while($i <= $length){
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$str = $str . $tmp;
$i++;
}
return $str;
}
for($i=1;$i<=rand(1,5);$i++){
$color = (rand(1,2) == 1) ? $pink : $red;
imageline($img,rand(6,90),rand(6,40), rand(6,90)+6,rand(6,40)+6, $color);
}
imagefill($img, 0, 0, $yellow);
$string = randomString(rand(4,6));
$_SESSION['string'] = $string;
imagettftext($img, 20, 10, 10, 52, $black, "calibri.ttf", $string);
imagettftext($img, 20, 10, 12, 53, $grey, "calibri.ttf", $string);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
go to this address http://www.cufonfonts.com/en/font/12048/calibri and download the font and save it in the same root folder as the to .php files we just created.
NOTE!! THIS WILL WORK ON LOCALHOST BUT IM HAVING A PROBLEM WITH THE image.php file SHOWING UP LIKE THIS -> ����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222��x"�����}!1AQa"q2���#B��R��$3br� .
THIS ONLY HAPPENS WHEN I TRY AND ADD IT TO THE CMS I CREATED. ALL I NEED TO KNOW IS WHY THIS IS HAPPENING I HAVE A FEELING ITS GOT SOMETHING TO DO WITH "header("Content-type: image/png");" although i could be way wrong. please help me. by the way im am trying to add a email form and have the captcha evaluate it
this is the form i am trying to insert the code into:
you can save it as form.php
<?php
$query = mysql_query("SELECT * FROM forms where id = '$formid'");
echo '<h2>'.mysql_result($query, 0, "title").'</h2>';
if(mysql_result($query, 0, "description") != "") {
echo '<p>'.mysql_result($query, 0, "description").'</p>';
}
error_reporting(E_ERROR);
?>
<form action="plugins/form_send.php" method="post" enctype="multipart/form-data" class="ajax" name="formplugin" id="form_<?php echo $formid; ?>">
<input name="formid" id="formid" type="hidden" value="<?php echo $formid; ?>" />
<p><span class="required">*</span> = required fields.</p>
<table border="0" cellspacing="5" cellpadding="5">
<?php
$field_labels_col = mysql_result($query, 0, "field_label");
$field_labels = array();
$field_labels =(explode('|', $field_labels_col));
$field_types_col = mysql_result($query, 0, "field_type");
$field_types = array();
$field_types =(explode('|', $field_types_col));
$field_option1_col = mysql_result($query, 0, "field_option1");
$field_option1 = array();
$field_option1 =(explode('|', $field_option1_col));
$field_option2_col = mysql_result($query, 0, "field_option2");
$field_option2 = array();
$field_option2 =(explode('|', $field_option2_col));
$field_validtype_col = mysql_result($query, 0, "field_validtype");
$field_validtype = array();
$field_validtype =(explode('|', $field_validtype_col));
$i = 0;
while (array_key_exists($i, $field_labels))
{
if(!isset($title)) { $title = ''; }
if($title != $field_labels[$i]) {
if(isset($field_labels[$i])) { $title = $field_labels[$i]; }
if(isset($title)) { $stripped = urlify($title); }
if(isset($field_types[$i])) { $type = $field_types[$i]; }
if(isset($field_option1[$i])) { $option1 = $field_option1[$i]; }
if(isset($field_option2[$i])) { $option2 = $field_option2[$i]; }
if(isset($field_validtype[$i])) { $valid = $field_validtype[$i]; }
echo '<tr>';
echo '<td align="left" valign="top">'.$title.'</td>';
echo '<td align="left" valign="top">';
// -- INPUT FIELDS --
if($type == "input") {
echo '<input name="'.$stripped.'" type="text" id="'.$stripped.'" size="'.$option1.'" maxlength="'.$option2.'" />';
if($valid != '') { echo '<span class="required">*</span>'; }
}
// -- TEXT AREAS --
if($type == "textarea") {
echo '<textarea name="'.$stripped.'" cols="'.$option1.'" rows="size="'.$option2.'"" id="'.$stripped.'"></textarea>';
}
// -- DROP DOWNS --
if($type == "dropdown") {
echo '<select name="'.$stripped.'" id="'.$stripped.'">';
$l = 0;
while (array_key_exists($l, $field_labels))
{
$thisoption = $field_option1[$l];
$thistitle = $field_labels[$l];
if($thistitle == $title) {
echo '<option value="'.$thisoption.'">'.$thisoption.'</option>';
}
$l++;
}
echo'</select>';
}
// -- RADIO BUTTONS --
if($type == "radiobutton") {
$l = 0;
while (array_key_exists($l, $field_labels))
{
$thisoption = $field_option1[$l];
$thistitle = $field_labels[$l];
$thisstripped = urlify($field_labels[$l]);
if($thistitle == $title) {
echo '<label><input type="radio" name="'.$thistitle.'" id="'.$thisstripped.'" value="'.$option1.'">'.$thisoption.'</label><br />';
}
$l++;
}
}
// -- SIMPLE YES / NO --
if($type == "yesno") {
echo '<label><input type="radio" name="'.$stripped.'" id="'.$stripped.'" value="Yes">Yes</label>';
echo '<label><input type="radio" name="'.$stripped.'" id="'.$stripped.'" value="No">No</label>';
}
// -- FILE UPLOADS --
if($type == "fileupload") {
echo '<input type="file" name="'.$stripped.'" id="'.$stripped.'" size="10"> (2mb limit)';
}
echo '</td>';
echo '</tr>';
}
$i++;
}
?>
<tr id="comment-div"><td colspan="2"><input name="user-comment" size="3" value="" /></td></tr>
<tr>
<td colspan="2">
<?php if(mysql_result($query, 0, "submit_text") != "") { ?>
<p><br /><?php echo mysql_result($query, 0, "submit_text"); ?></p>
<?php } ?>
<div id="result"><strong>Loading... please wait.</strong></div>
<!-- <input type="submit" name="button" id="button" value="<?php echo mysql_result($query, 0, "submit_button"); ?>" /> -->
</td>
</tr>
</table>
</form>
I guess the code you posted is not the whole file: try to remove all the content before and after <?php and ?> tags. You could even remove the final tag ?> (it is a style convention adopted by many, because of this very issue).
This affects every other file you are executing: every space, newline or other content outside the PHP tags or echoed by PHP is added to the image content, and ends up with the image corruption. Also PHP notices and warnings are an issue.
For clarity, the executed files format must be:
<?php // nothing before this line
// no other PHP tags till the end
... // PHP code with no echo calls
// no closing tag
In your code, you do:
header( "Content-type: image/png" );
# ...
echo "<img src='image/png;base64," . base64_encode( $x )."'>";
You are outputting a PNG sorrounding it with HTML, which inevitably corrupts your image content. You have to remove the HTML (echo $x) and use the image from another file, f.e. with
<img src="png_generator_script.php"/>
Or use the script as HTML page, removing
header( "Content-type: image/png" );
i was faced same problem . Remove all html tag it will 100% work..
save as php file .. index.php
<?php
/**
* PHP GD
* create a simple image with GD library
*
*/
//setting the image header in order to proper display the image
header("Content-Type: image/png");
//try to create an image
$im = #imagecreate(800, 600)
or die("Cannot Initialize new GD image stream");
//set the background color of the image
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD);
//set the color for the text
$text_color = imagecolorallocate($im, 133, 14, 91);
//adf the string to the image
imagestring($im, 5, 300, 300, "I'm a pretty picture:))", $text_color);
//outputs the image as png
imagepng($im);
//frees any memory associated with the image
imagedestroy($im);
?>
instead of...
<html>
<head>
</head>
<body>
<?php
/**
* PHP GD
* create a simple image with GD library
*
*/
//setting the image header in order to proper display the image
header("Content-Type: image/png");
//try to create an image
$im = #imagecreate(800, 600)
or die("Cannot Initialize new GD image stream");
//set the background color of the image
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD);
//set the color for the text
$text_color = imagecolorallocate($im, 133, 14, 91);
//adf the string to the image
imagestring($im, 5, 300, 300, "I'm a pretty picture:))", $text_color);
//outputs the image as png
imagepng($im);
//frees any memory associated with the image
imagedestroy($im);
?>
</body>
</html>
This code is working fine. I have tested it locally on chrome & firefox.

Keep getting: "stripslashes() expects parameter 1 to be string, array given in /target.php on line 30" error

The Error I keep getting is: stripslashes() expects parameter 1 to be string, array given in /target.php on line 30
This is my code for this site, I am new to this and i've been trying to do it for over 30 minutes.
I am sorry for asking such a stupid question.
<?php
// rnprofile.php
include_once 'rnheader.php';
if (!isset($_SESSION['user']))
die("<br /><br />You need to login to view this page");
$user = $_SESSION['user'];
echo "<h3>Edit your Profile</h3>";
if (isset($_POST['text'])) {
$text = sanitizeString($_POST['text']);
$text = preg_replace('/\s\s+/', ' ', $text);
$query = "SELECT * FROM rnprofiles WHERE user='$user'";
if (queryMysql($query) != false) {
queryMysql("UPDATE rnprofiles SET text='$text'
where user='$user'");
} else {
$query = "INSERT INTO rnprofiles VALUES('$user', '$text')";
queryMysql($query);
}
} else {
$query = "SELECT * FROM rnprofiles WHERE user='$user'";
$result = queryMysql($query);
$sth = $dbh->prepare($query);
$sth->execute();
if (queryMysql($query) != false/* mysql_num_rows($result) */) {
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
//$row = mysql_fetch_row($result);
$text = stripslashes($row[1]);
}
else
$text = "";
}
$text = stripslashes(preg_replace('/\s\s+/', ' ', $text));
if (isset($_FILES['image']['name'])) {
$saveto = "$user.jpg";
move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
$typeok = TRUE;
switch ($_FILES['image']['type']) {
case "image/gif": $src = imagecreatefromgif($saveto);
break;
case "image/jpeg": // Both regular and progressive jpegs
case "image/pjpeg": $src = imagecreatefromjpeg($saveto);
break;
case "image/png": $src = imagecreatefrompng($saveto);
break;
default: $typeok = FALSE;
break;
}
if ($typeok) {
list($w, $h) = getimagesize($saveto);
$max = 100;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w) {
$th = $max / $w * $h;
$tw = $max;
} elseif ($h > $w && $max < $h) {
$tw = $max / $h * $w;
$th = $max;
} elseif ($max < $w) {
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array(// Sharpen image
array(−1, −1, −1),
array(−1, 16, −1),
array(−1, −1, −1)
), 8, 0);
imagejpeg($tmp, $saveto);
imagedestroy($tmp);
imagedestroy($src);
}
}
showProfile($user);
echo <<<_END
<form method='post' action='rnprofile.php'
enctype='multipart/form-data'>
Enter or edit your details and/or upload an image:<br />
<textarea name='text' cols='40' rows='3'>$text</textarea><br />
Image: <input type='file' name='image' size='14' maxlength='32' />
<input type='submit' value='Save Profile' />
</pre></form>
_END;
?>
from PDO::FETCH manual
PDO::FETCH_ASSOC: returns an array indexed by column name as returned
in your result set
PDO::FETCH_NUM: returns an array indexed by column number as returned
in your result set, starting at column 0
you have used FETCH_ASSOC then you must use column name as key of $row
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
$text = stripslashes($row['column_name']);
or change it to FETCH::NUM
$row = $sth->fetchAll(PDO::FETCH_NUM);
$text = stripslashes($row[1);
The fetchAll method returns an array containing all rows, so this line:
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
doesn't set $row in the way that the name row implies. As a result, $row[1] is not a single string, but rather, it's an associative array containing an entire row.
You should probably change this:
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
//$row = mysql_fetch_row($result);
$text = stripslashes($row[1]);
to this:
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
$text = stripslashes($rows[0]['column_name_of_interest']);
(Note that I also changed 1 to 0: arrays are indexed from 0 in PHP, not from 1.)
It's because you're using fetchAll, which returns you an array of associative arrays:
if (queryMysql($query) != false/* mysql_num_rows($result) */) {
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
$text = stripslashes($row[1]);
So when you call stripslashes, and pass it $row[1], you're passing it an associative array that's made up of the second result from the database. I think this might do what you mean:
if (queryMysql($query) != false/* mysql_num_rows($result) */) {
if ($row = $sth->fetch(PDO::FETCH_ASSOC) {
$text = stripslashes($row[1]);
That's assuming that your search for a user by name will only return one result

php script to show 4 random photos from a folder is showing the same photo 4 times

i'm trying to build a little module for a site that pulls 4 random photos from a folder on the server and presents them in a div. what's happening is the same 4 photos are being called. how do i get it to call 4 different photos?
heres the random.php file:
<?php
$folder = '.';
$extList = array();
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}
if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
file_exists( $folder.$imageInfo['basename'] )
) {
$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}
if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = #imagecreate (100, 100)
or die ("Can't initialize image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}
?>
here's how random.php is being called:
<?php
/* DATA */
$data = array(
array('COUNT', '88', 'Here are a few of them', '1, 2, 3, 4'),
);
?>
<div>
<table class="reop" border='0' width='100%' cellpadding='0' cellspacing='10'>
<?php
$count = 0;
foreach($data as $row) {
$class = ($count % 2 == 1 ? " class='alt'" : '');
echo "<tr$class>";
for($j = 0; $j < count($row); $j++) {
if ($j!=3) {
echo "<td class='cell_$j'>$row[$j]</td>";
} else {
// $avatar = '';
$array = preg_split('/,/', $row[$j], -1, PREG_SPLIT_NO_EMPTY);
foreach ($array as $val) {
$avatar .= '<img src="/staffpics/random.php"> ';
}
echo "<td class='cell_$j'>$avatar</td>";
}
}
echo '</tr>';
// $count++;
}
?>
</table>
when your page is displayed, most browsers will immediately request the four pictures.. nearly all at the same time. your code for "random" picking a picture is based on time() which returns a count of seconds since the begin of unixtime. executed 4 times within the same second it will also yield the same result 4 times.
your code:
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
should be rewritten as:
// filelist starts with "0", count with "1" .. so we are off-by-one and first picture would never show without -1
$imageNumber = rand() % (count($fileList) - 1);
$img = $folder.$fileList[$imageNumber];
that way you will get "random" pictures.
Also, what Mister Lister commented is totally valid - your browser may also cache the picture and thus only request a picutre once and just display that one again and again using its cache.
to avoid this, replace any <img src="random.php"> with <img src="random.php?rand=<?= rand(); ?>">
But note:
your server may eventually return the same random number multiple times so eventually (rarely) you will end up with the same picture twice on the same page.
to fix that, you must completely rewrite your code.
the script rendering the page should decide which pictures to choose and ensure that no picture is displayed twice instead of just including a "give random picture" script, which is independent of the page rendered.
much easier, you could try this:
// find all images - case SenSItivE
$all_images = glob("/path/to/images/*.{jpeg|jpg|png|gif}", GLOB_BRACE);
// bring array in random order
shuffle($all_images);
// pick four random images - you may also use a for or foreach loop to iterate the array
list ($img1, $img2, $img3, $img4) = $all_images;
// write code to display four images here
Here is another way of fetching 4 random photos:
//randomize order
shuffle($fileList);
//get first 4 values of array
$randomPhotos = array_slice($fileList, 0, 4);
Please note, shuffle would change value order in your original array.
Call srand to seed your random number generator.

wordpress image path, how to edit?

i have a small problem in figuring out a path to a image var.
here is what happens. this is the image tag:
<img src="http://www.xxx.info/wp-content/uploads/http://ecx.images-amazon.com/images/I/51AV8B7CT2L._SL160_.jpg" class="attachment-135x135 wp-post-image" alt="Clean & Sober" title="Clean & Sober">
and this is how i would like it to be, without the http://www.xxx.info/wp-content/uploads/:
<img src="http://ecx.images-amazon.com/images/I/51AV8B7CT2L._SL160_.jpg" class="attachment-135x135 wp-post-image" alt="Clean & Sober" title="Clean & Sober">
here is the wordpress code:
<?php $thumb = '';
$width = 135;
$height = 135;
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"]; ?>
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
more related functions:
/* this function prints thumbnail from Post Thumbnail or Custom field or First post image */
function print_thumbnail($thumbnail = '', $use_timthumb = true, $alttext = '', $width = 100, $height = 100, $class = '', $echoout = true, $forstyle = false, $resize = true, $post='') {
if ( $post == '' ) global $post;
$output = '';
$thumbnail_orig = $thumbnail;
$thumbnail = et_multisite_thumbnail($thumbnail);
$cropPosition = get_post_meta($post->ID, 'etcrop', true) ? get_post_meta($post->ID, 'etcrop', true) : '';
if ($cropPosition <> '') $cropPosition = '&a=' . $cropPosition;
if ($forstyle === false) {
if ($use_timthumb === false) {
$output = $thumbnail_orig;
} else {
$output = '<img src="'.get_bloginfo('template_directory').'/timthumb.php?src='.$thumbnail.'&h='. $height .'&w='. $width .'&zc=1&q=90'.$cropPosition.'"';
if ($class <> '') $output .= " class='$class' ";
$output .= " alt='$alttext' width='$width' height='$height' />";
if (!$resize) $output = $thumbnail;
}
} else {
$output = $thumbnail;
if ($use_timthumb === false) {
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $output, $matches);
$output = $matches[1][0];
} else {
$output = get_bloginfo('template_directory').'/timthumb.php?src='.$output.'&h='.$height.'&w='.$width.'&q=90&zc=1'.$cropPosition;
}
}
if ($echoout) echo $output;
else return $output;
}
this is what i get returned $output = $thumbnail_orig; in the image tag.
i know that there should be a var for http://www.xxx.info/wp-content/uploads/
and another one for http://ecx.images-amazon.com/images/I/51AV8B7CT2L._SL160_.jpg
i want to remove the the website path to uploads.
i cant seem to figure it out,
All help appreciated,
Thanks
I might be wrong or misunderstanding the question, but I thought setting the path in WP is as simple as editing it in the admin panel, under settings, no?
DC

Categories