How to get the URL from an Images ID

I'm trying to add a short URL of the page to every picture on my site. A simple way to do that would be to use the ID of a picture. So for instance domain.com/1234 would redirect to domain.com/category/subcategory/picturename.jpg.html

It's no problem to make that URL, the function getImageID does that. My knowledge of PHP is just not sufficient enough to retrieve the right URL when you only have the ID.

So basically, what I need is a function that fires on page load and checks if the given number is a unique picture and then redirects 301 to the right URL. Can anybody help me with that? Or maybe even provide the code?

Thanks in advance!

(More info about making your own short URL's can be found here. It's actually very useful for sending e-mails or twittering.

Comments

  • acrylian Administrator, Developer
    You will need to check if the ID matches an image ID in the images db table and then get the image's filename and the album folder name of its album and then build the actual image page url for redirection from that.
  • Wow, never thought I'd be able to write this myself but I did it. Please take a look at the code, this is the best I can do. I'm sure it's not optimal, I'm not a PHP coder so feedback is more than welcome.

    In the HEAD of your image.php template add this line
    `
    ">
    `
    In the HEAD of your album.php template add this line
    `
    ">
    `
    Create a folder named 'z' in the root of your domain folder. Add a .htaccess file in it with the following content:

    `
    RewriteEngine On
    RewriteRule ^(.*)/?$ index.php?p=$1 [L,QSA]
    `
    Add a file called index.php to the folder 'z' with the following code:

    `
    <?php
    $imgId = ($_GET['p']);
    $username="your_zenphoto_database_username";
    $password="your_zenphoto_database_password";
    $database="your_zenphoto_database";
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM zp_images WHERE id = ".$imgId;
    $result=mysql_query($query);
    while($row = mysql_fetch_array($result)) {
    $imgName = $row["filename"];
    $albId = $row["albumid"];
    }
    //Check if it's an image, else assume it's a category
    if($imgName==''){
    $albId = $imgId;
    }
    else{
    $imgName = $imgName.'/';
    }
    $query1="SELECT * FROM zp_albums WHERE id = ".$albId;
    $result1=mysql_query($query1);
    mysql_close();
    while($row1 = mysql_fetch_array($result1)) {
    $albName = $row1["folder"];
    //$albId = $row["albumid"];
    }
    header("HTTP/1.1 301 Moved Permanently");
    header('Location: http://yourdomain.com/zenphoto-directory/'.$albName.'/'.$imgName);
    ?>
    `
    In all code above you could replace yourdomain.com with your own shorter URL, if you have one.
  • Hello,

    Google has indexed my pages with and without "www". So I would like to add the link rel="canonical" tag to my pages. I found the following info on the web. Can you tell me if this is the proper code to use?

    "In the head just enter:

    <link rel="canonical" href="http://www.website.com<?php echo $_SERVER['PHP_SELF']?>">

    if you’re using a rewritten path then it needs to be changed to:

    <link rel="canonical" href="http://www.website.com<?php echo parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);?>">"

    Thank-you! - Renee
  • acrylian Administrator, Developer
    It would be easier to use the urls zenphoto provides. So on images.php you can get the url of that image page by using `$_zp_current_image->getImageLink()`.
    Similar functions do exist for albums and Zenpage items.
  • I'm new in php.

    1: I have installed the service yourls (domain slc.so)
    2: I need to shorten the url for the feed (rss)

    How do?
Sign In or Register to comment.