Image Resizing Script required - a better one...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

New file:

Code: Select all

<?php
function getSrcSet($filename, $desired_sizes = [475, 768, 1920])
{
    $root_directory = __DIR__;
    $images_directory = '/images/pages';
    $source_directory = $root_directory . $images_directory;
    $srcset = [];

    if (file_exists($source_directory . '/' . $filename)) {
        $basename = pathinfo($filename, PATHINFO_FILENAME);
        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        foreach ($desired_sizes as $size) {
            $fullname = $basename . '-' . $size . '.' . $extension;
            if (file_exists($source_directory . '/' . $fullname)) {
                $srcset[] = "{$images_directory}/{$fullname} {$size}w";
            }
        }
    }

    return implode(', ', $srcset);
}
?>

Code: Select all

<a href='$row->url'>";

require_once '/functions/functionConsumerResize.php';
$image = $row->image;
echo "<img src='/images/pages/$image' srcset='"; getSrcSet($image); echo "'>
<p>$row->freetext</p>";

if ( $detect->isMobile() && !$detect->isTablet() ) 
  {
  }
  else
  {
  if (($countsquare % 4) == 0)
    {
     echo "<div style='clear:both'></div>";
    }
  }

echo "</a>
Produces:

[text]<a href='/categ/shirts'><img src='/images/pages/25644881shirts.jpg' srcset=''>
<p>Shirt</p></a>[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

I specifically said not to just copy/paste my code, yet that is precisely what you did. This means you're almost certainly looking in the wrong directory (__DIR__ is the same directory as the current file), and looking for files with -475 rather than the _475 you're using. Additionally, you're just calling the function, neither capturing nor echoing its output.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

So this is the external file:

Code: Select all

<?php
function getSrcSet($filename, $desired_sizes = [475, 768, 1920])
{
    $root_directory = __DIR__;
    $images_directory = '/images/pages';
    $source_directory = $root_directory . $images_directory;
    $srcset = [];

    if (file_exists($source_directory . '/' . $filename)) {
        $basename = pathinfo($filename, PATHINFO_FILENAME);
        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        foreach ($desired_sizes as $size) {
            $fullname = $basename . '_' . $size . '.' . $extension;
            if (file_exists($source_directory . '/' . $fullname)) {
                $srcset[] = "{$images_directory}/{$fullname}_{$size}";
            }
        }
    }

    return implode(', ', $srcset);
}
?>
I have now altered it correctly with the right _475 in it. Rather than a hyphen.

But how is the rest of it wrong?

Code: Select all

require_once 'functions/functionConsumerResize.php';
$image = $row->image;
echo "<img src='/images/pages/$image' srcset='"; getSrcSet($image); echo "'>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

Have you fixed the value of $root_directory yet? This file appears to live in your functions directory, but that is not the parent of your images directory. Finally, as I mentioned earlier, the function just returns a string. You need to do something with it. You're echoing everything else on the line where it's being called, you likely want to be echoing its output as well.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

It may be beneficial to take a step back. Don't worry about the integration of this function into the rest of your code until you have the function itself working.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

I need to run for a few hours. I'll check in on this once I'm back and we can pick back up tomorrow if needed. Must be getting late over there by now.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

yes it is. And I've been on this since about 10am! IT's now early 9pm.
Thanks for your help and support so far. I know that once this onthefly resizing is cracked, I will be able to roll it out over the other pages.

All the best. Catch up tomorrow. Thx.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

Been looking at this code this morning. Noticed a few omissions from myself, such as the vendor include.

Code: Select all

require_once dirname(__DIR__) . '/vendor/autoload.php';
$image = $row->image;
function getSrcSet($filename, $desired_sizes = [475, 768, 1920])
{
    $root_directory = __DIR__;
    $images_directory = '/images/pages';
    $source_directory = $root_directory . $images_directory;
    $srcset = [];

    if (file_exists($source_directory . '/' . $filename)) {
        $basename = pathinfo($filename, PATHINFO_FILENAME);
        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        foreach ($desired_sizes as $size) {
            $fullname = $basename . '_' . $size . '.' . $extension;
            if (file_exists($source_directory . '/' . $fullname)) {
                $srcset[] = "{$images_directory}/{$fullname}_{$size}.{$extension}";
            }
        }
    }

    return implode(', ', $srcset);
}
echo "<img src='/images/pages/$image' srcset='"; getSrcSet($image); echo "'>
<p>$row->freetext</p>";
It states:
[text]
Fatal error: Cannot redeclare getSrcSet() (previously declared in C:\xampp\phpMyAdmin\site-wide\includes\page.inc:128) in C:\xampp\phpMyAdmin\site-wide\includes\page.inc on line 128[/text]
I did try to adjust it to your style of coding but same issue.
Should $image in the srcset be $filename??

Trying to see where this issue is.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

Update:
The function was my bad - I was putting it in each rendered db response.
So it is now out of the way at the top of the page.
So now, it's showing only the src images, not srcset. Which I think was your plan for now....
Next...?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

Second update:
I'd made a mistake in the function.
This is how it now shows:

Code: Select all

require_once dirname(__DIR__) . '/vendor/autoload.php';
function getSrcSet($filename, $desired_sizes = [475, 768, 1920])
{
    $root_directory = $_SERVER['DOCUMENT_ROOT'];
    $images_directory = '/images/pages';
    $source_directory = $root_directory . $images_directory;
    $srcset = [];

    if (file_exists($source_directory . '/' . $filename)) {
        $basename = pathinfo($filename, PATHINFO_FILENAME);
        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        foreach ($desired_sizes as $size) {
            $fullname = $basename . '_' . $size . '.' . $extension;
            if (file_exists($source_directory . '/' . $fullname)) {
                $srcset[] = "{$images_directory}/{$fullname} {$size}w";
            }
        }
    }

    return implode(', ', $srcset);
}  
I'd edited the final $srcset[] bit in a bad way. It's now correct. But the srcset is still echoing nothing in the code.
I have altered the DIR to be a method I use, but that makes no difference.
Should I be seeing the srcset filename in my rendered code, particularly if an image IS there?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

simonmlewis wrote:Been looking at this code this morning. Noticed a few omissions from myself, such as the vendor include.
Seeing as it's a require_once call, it shouldn't be required everywhere. If your .htaccess sends all requests through to index.php, for example, calling it at the top of index.php should cover the whole app. Just a thought. Again, because it's require_once, it won't be included repeatedly anyway.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

should it not still work tho, like it does on the admin page?
I'm lost now.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

simonmlewis wrote:But the srcset is still echoing nothing in the code...
Should I be seeing the srcset filename in my rendered code, particularly if an image IS there?
Are you echoing out the results of the function call?

You've posted this a few times:

Code: Select all

echo "<img src='/images/pages/$image' srcset='"; getSrcSet($image); echo "'>
<p>$row->freetext</p>";
Note the absence of echo before getSrcSet?

Contrast that with the sample code I had posted:

Code: Select all

<img src="/images/pages/<?= $image; ?>" srcset="<?= getSrcSet($image); ?>">
simonmlewis wrote:I have altered the DIR to be a method I use, but that makes no difference.
Agreed. So long as the method knows the full path to your images directory, it doesn't much matter how it knows it.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

I just code things a little different from you. But the result is the same like this:

Code: Select all

echo "><a href='$row->url'>";
$image = $row->image;
?>
<img src="/images/pages/<?= $image; ?>" srcset="<?= getSrcSet($image); ?>">
<?php
echo "<p>$row->freetext</p>";
It renders:
srcset=""
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

<?= is just shorthand for <?php echo. So now you've got an echo inside an echo. Modifying your code, try something like this:

Code: Select all

echo "<img src='/images/pages/$image' srcset='"; echo getSrcSet($image); echo "'>
<p>$row->freetext</p>";
Post Reply