Is this possible to do using php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1330

    #1

    Tech Is this possible to do using php?

    I have a bunch of web pages that all contain an image on them however, not all of the pages *actually* have an image associated with them so I'm using a generic placeholder.jpg image.

    Is there a method I can use to change the name of that image dynamically so depending on what the pages title is, it will rename itself automagically?

    For example:

    The page catfood.php would display the generic placeholder.jpg but it would be renamed to catfood.jpg?

    Anyone know of a simple/easy method of achieving this please?

    TIA.
    Extreme Link List - v1.0
  • Webster01
    Confirmed User
    • Apr 2013
    • 378

    #2
    I had that done as a custom mod by Nick from RoboScripts on his PHP script, for custom posts on the site.
    But also had that done by another dev on a WP project.
    It renames the image according the post title/permalink.
    So when the link is /sexy-hot-girl it renames the image to sexy-hot-girl.jpg

    Sure that is possible and a great mod!
    But I´m not too technical so I cannot tell you exactly how

    Also you maybe want to get an alt tag added automatically?
    And the title + alt tag on the thumbs in the thumb overview??
    Another great idea

    Comment

    • fuzebox
      making it rain
      • Oct 2003
      • 22351

      #3
      placeholder.jpg as the 404 page in your images directory?

      Comment

      • zijlstravideo
        Confirmed User
        • Sep 2013
        • 806

        #4
        yourdomain.com/go-fuck-yourself.html will become go-fuck-yourself.jpg.

        <?php
        $myPage = explode('.',strtolower($_SERVER[REQUEST_URI]));
        $myImage = trim(preg_replace("/[^a-zA-Z0-9\-]+/", "", $myPage[0]));
        $myImage = $myImage . '.jpg';

        // can also be: $myImage = 'files/images/' . $myImage . '.jpg';
        // or whatever folder or image file format you want...

        if (@getimagesize($myImage)) {
        echo "<img src='$myImage'>";
        } else {
        // file doesn't exist, show placeholder
        echo "<img src='files/images/placeholder.jpg'>";
        }
        die();
        ?>


        I got a feeling there's even a faster way, but just like you, I'm too lazy to dive into the PHP docs as well.
        Contact: email

        Comment

        • zijlstravideo
          Confirmed User
          • Sep 2013
          • 806

          #5
          Oops, didn't read it properly. I see what you are trying to achieve.

          That would probably be more a case for url rewriting rules in your htaccess file in combination with a few lines of PHP on that page.

          Thing is, you don't want to screw this up and might want to think of what logic to apply on how to handle security and validation, considering it is user input (anyone can make up and type in some random page url).
          Contact: email

          Comment

          • lock
            Confirmed User
            • Jul 2003
            • 5065

            #6
            https://stackoverflow.com/questions/...d-image-in-php
            Traffic.Tools - 40+ Free Tools
            Free.Marketing - 150+ Free Tools
            Submission.Tools
            - 20+ Free Tools

            Comment

            • zijlstravideo
              Confirmed User
              • Sep 2013
              • 806

              #7
              I think OP is looking to rewrite urls, rather than creating a ton of copies of the same image file.
              Contact: email

              Comment

              • NoWhErE
                Too lazy to set a custom title
                • Sep 2005
                • 10583

                #8
                Technically you can do it. The main problem is re-writing the image filename every time.

                This means you need to copy the placeholder.jpg to a new file, rename it, then display it.

                And then you need to have a clean-up script that deletes the new .jpg after the session is finished (or have it clean up your images directory every X hours or so).


                HOWEVER, is it worth doing? Probably not.
                The images won't be indexed by google images because they will only exist for a finite amount of time (unless you want to keep all of the clones in a directory which could result in tens of thousands of images AND this also opens up your site to spam attacks that would overload your server storage).

                As for SEO benefits, the return on investment is negligible to none.



                So if you would actually want to do it the process would roughly look like this:

                1 - Get the URL slug (mydomain.com/this-is-the-slug) : How you do this varies on your setup. In Wordpress $post->slug, in pure PHP you'd need to use some REGEX
                2 - Find the placeholder.jpg image: $imagePath = /home/domain/public_html/images/your-image.jpg
                3 - Copy the file with its new name : $copied = copy($imagePath , $newName);
                4 - Serve the image to the user: $image = $copied ? $copied : $default_placeholder;
                skype: lordofthecameltoe

                Comment

                • Publisher Bucks
                  Confirmed User
                  • Oct 2018
                  • 1330

                  #9
                  Originally posted by NoWhErE
                  Technically you can do it. The main problem is re-writing the image filename every time.

                  This means you need to copy the placeholder.jpg to a new file, rename it, then display it.

                  And then you need to have a clean-up script that deletes the new .jpg after the session is finished (or have it clean up your images directory every X hours or so).


                  HOWEVER, is it worth doing? Probably not.
                  The images won't be indexed by google images because they will only exist for a finite amount of time (unless you want to keep all of the clones in a directory which could result in tens of thousands of images AND this also opens up your site to spam attacks that would overload your server storage).

                  As for SEO benefits, the return on investment is negligible to none.



                  So if you would actually want to do it the process would roughly look like this:

                  1 - Get the URL slug (mydomain.com/this-is-the-slug) : How you do this varies on your setup. In Wordpress $post->slug, in pure PHP you'd need to use some REGEX
                  2 - Find the placeholder.jpg image: $imagePath = /home/domain/public_html/images/your-image.jpg
                  3 - Copy the file with its new name : $copied = copy($imagePath , $newName);
                  4 - Serve the image to the user: $image = $copied ? $copied : $default_placeholder;
                  Thanks for that detailed response

                  Also, thanks for everyone else who replied, it seems like it might just be worth placing a standard call to the default image after reading the responses and the explanation on how SEO benefits will be negligable, again appreciate the replies
                  Extreme Link List - v1.0

                  Comment

                  • zijlstravideo
                    Confirmed User
                    • Sep 2013
                    • 806

                    #10
                    Originally posted by NoWhErE
                    Technically you can do it. The main problem is re-writing the image filename every time.

                    This means you need to copy the placeholder.jpg to a new file, rename it, then display it.
                    Making a copy of the same image file doesn't make much sense...

                    Instead, I would create a php file, let's say, call it placeholder.php.

                    - Add rewrite rule to .htaccess file:
                    RewriteRule image-(.*)\.jpg$ placeholder.php?slug=$1 [L]

                    - Create placeholder.php:
                    Takes input from a query like this: placeholder.php?slug=this-is-the-url-slug.
                    Then, check if that url slug is a match with an entry from the SQL database.
                    If it's a match, print the new re-written image url (<img src='image-this-is-the-url-slug.jpg'>) else, just print (<img src='image-fallback.jpg'>).

                    Replace the - with spaces and you can include it as an alt tag as well.
                    Contact: email

                    Comment

                    • blackmonsters
                      Making PHP work
                      • Nov 2002
                      • 20960

                      #11
                      I don't know if there is much benefit for it but :

                      PHP Code:
                      <?php
                      
                          $placeholder_path = __DIR__ . "/placeholder.jpg";
                      
                          $myPage = explode('.',strtolower($_SERVER[REQUEST_URI]));
                          $myImage = trim(preg_replace("/[^a-zA-Z0-9\-]+/", "", $myPage[0]));
                          $alt = $myImage;
                          $myImage = $myImage . '.jpg';
                      
                          $new_path = __DIR__ . "/$myImage";
                          
                          if (@getimagesize($new_path )) {
                                  echo "<img src='$myImage'>";
                          } else {
                      
                              $placeholder_image = @file_get_contents($placeholder_path);
                              
                              $file = fopen("$new_path","w+");
                              if (flock($file,LOCK_EX)) {
                                  fwrite($file,$placeholder_image);
                                    fflush($file);
                      
                                    flock($file,LOCK_UN);
                              } else {
                                    echo "Error locking file!";
                              }
                              fclose($file);    
                              
                              echo "<img src=\"$myImage\" alt=\"$alt\">";
                      
                          }
                      
                      ?>

                      Free Open Source Live Aggregated Cams Script (FOSLACS)

                      Comment

                      • natkejs
                        Confirmed User
                        • Jan 2003
                        • 1640

                        #12
                        I think the easiest and fastest solution would be an internal URL rewrite.

                        domain.com/placeholder/<whatever>.jpg
                        to
                        domain.com/placeholder.jpg

                        RewriteEngine on
                        RewriteRule "^/placeholder/.+\.jpg$" "/placeholder.jpg" [PT]

                        Then you would just echo the page slug as the image name.

                        ie:
                        domain.com/placeholder/my-whatever-slug.jpg

                        Whether it's well invested time to set that up is for you to decide however ;)

                        Edit:
                        Just realized Directory and FallbackResource would be a better solution if talking Apache. Works out almost the same except your existing images and "dynamic placeholders" would exist in the same directory. Like the 404 solution suggested above but without 404 response codes.

                        Comment

                        Working...