GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Is this possible to do using php? (https://gfy.com/showthread.php?t=1354546)

Publisher Bucks 05-01-2022 04:00 PM

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.

Webster01 05-01-2022 04:48 PM

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! :thumbsup
But I´m not too technical so I cannot tell you exactly how :upsidedow

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 :thumbsup

fuzebox 05-01-2022 05:05 PM

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

zijlstravideo 05-01-2022 07:44 PM

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. :pimp

zijlstravideo 05-01-2022 07:54 PM

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).

lock 05-02-2022 03:10 AM

https://stackoverflow.com/questions/...d-image-in-php

zijlstravideo 05-02-2022 04:36 AM

Quote:

Originally Posted by lock (Post 22997300)

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

NoWhErE 05-02-2022 06:20 AM

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;

Publisher Bucks 05-02-2022 07:36 AM

Quote:

Originally Posted by NoWhErE (Post 22997331)
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 :thumbsup

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 :thumbsup

zijlstravideo 05-02-2022 09:00 AM

Quote:

Originally Posted by NoWhErE (Post 22997331)
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.

blackmonsters 05-02-2022 10:49 AM

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\">";

    }

?>


:2 cents:

natkejs 05-02-2022 07:48 PM

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.


All times are GMT -7. The time now is 03:02 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc