Jump to content

Image filtering and text to URL.


Krasik

Recommended Posts

For starters. I'm relatively new to PHP programming. I've had minor VB and I have SOME understanding of programming in general. I was trying to find this solution myself without consulting anyone but with PHP5 and it's deprecated functions. I can't just paste code from the net and it work. Currently I have PHP 5.3.5 installed. I'm running on the newest Apache and MySQL builds.

 

Now to the part I want to get to work. I'm trying this just because I can. Basically. I would like php to scan a certain path and filter out the images. I have a script that does that fine. What I want to do is take the file names, append my host address and turn it into a clickable link. I also have a script that will turn text into URL's. I will paste the snippets I have. I was initially trying to use eregi_replace() but apparently that's been deprecated and I believe I should now use FileInfo() or something like that. Not quite sure what they do but am really wanting to learn. Thanks for any help you guys can offer.

<?php
$path = 'http://www.krasikart.no-ip.org';
$dir=$_SERVER['DOCUMENT_ROOT']."/htdocs/pics/";
$total = '';
// Retrieve all the images.
$files = glob ("$dir{*.jpg,*.jpeg,*.gif}", GLOB_BRACE);

// Confirm that matches were made.
if (is_array($files)) { 

// Loop through the array.
foreach ($files as $image) {
$total = $path . $image;		
print "$total \n";

// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls
$text = "$total";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);

} else {

       // if no urls in the text just return the text
       


}
}

} else {
echo 'No files matched the search pattern!';
}

?>

Link to comment
Share on other sites

You say you have some programming knowledge. Take a step back and look at your logic. You are over complicating things.

 

You are checking your paths for a url... why? You know its a url because you have pre-pended your domain name to the head of the string. You don't need any of that, get rid of it.

Link to comment
Share on other sites

Well, in order for the parser to work, it must find text that match URLs. I can only do that if I append my host address. I don't know any other way at this time. If i just parse for images, I get ./pics/testimgae.jpg. I don't get the full domain name path. I believe. I suppose I don't know how to append the images to my host name properly. I know the code is messy. I would like to know the most efficient means to accomplish this.

Link to comment
Share on other sites

I think I misread your question. I have to append the url in oder for the script to recognize that it's a URL. I'm wanting this script for auto link perposes. I can just place an image in the directory and it automatically turns it into a link without me having to html code it. Just retrieving the image path isn't enough to be recognized as a URL. I hope that sheds more light on it. Thanks.

Link to comment
Share on other sites

<?php
$dir = $_SERVER['DOCUMENT_ROOT']."/htdocs/pics/";
$files = glob("$dir{*.jpg,*.jpeg,*.gif}", GLOB_BRACE);
if (is_array($files)) { 
foreach ($files as $image) {
  echo "<a href='pics/$image' rel='nofollow'>$image</a>";
}

 

Your path within $dir doesn't look right to me, but this example should get you started.

Link to comment
Share on other sites

Ok, I've slightly modified the code but now it shows direct system paths, not urls. It out puts this:

C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/boxes2.jpg

 

C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/boxes3.jpg

 

C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/motherE.jpg

 

C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/motherE2.jpg

 

I would like for it to show http://www.krasikart.no-ip.org/boxes3.jpg etc. Any suggestions?

Link to comment
Share on other sites

<?php
$dir = $_SERVER['DOCUMENT_ROOT']."/htdocs/pics/";
$files = glob("$dir{*.jpg,*.jpeg,*.gif}", GLOB_BRACE);
if (is_array($files)) { 
  foreach ($files as $image) {
    $image = basename($image);
    echo "<a href='pics/$image' rel='nofollow'>$image</a>";
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.