PHP Help - I'm a bi...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] PHP Help - I'm a bit stuck. STW PHP Gods, can you help?

27 Posts
7 Users
0 Reactions
117 Views
Posts: 8527
Free Member
Topic starter
 

I have a website with a directory for dumping the odd image in, for zoom meetings etc so I can give people a quick link.

I have a simple (not for me) bit code that I was helped with at Stackoverflow. Got my head kicked in over there :-(.

The code displays the images perfectly and sorted as server modified date. Great.

But what I cant work out is how to make really large images render smaller or automatically scale to the screen size.

Any coding gods here that could shed some light on the code or tell me what and how to add.

I assume its something todo with the Echo part.

Example
https://www.sidingstudios.com/mtb/

PHP Code of Index File
<?php
$files = glob('img/*');

$output = [];

foreach ($files as $f)
{
$output[filemtime($f)] = $f;
}
ksort($output);
$images = array_reverse($output);

foreach ($images as $image)
{
echo '<br />';
}
?>

This hopefully simple bit code operates bit like a Instagram layout without the comments etc. I just want my images on my server.


 
Posted : 19/04/2021 5:17 pm
Posts: 8527
Free Member
Topic starter
 

Trying to display code on this forum.

Code screen grab.


 
Posted : 19/04/2021 5:20 pm
 toby
Posts: 532
Full Member
 

The right way to do it would be to scale the images when you upload them to the directory with the ImageMagik library. However bandwidth is cheap these days, so big images are probably not as much of a problem.

Can you set a max-width on img tags with CSS on your page?


 
Posted : 19/04/2021 5:22 pm
Posts: 8527
Free Member
Topic starter
 

@toby
Just looking to use the index.php and leave in the given directory. I just upload to that directory using Rightload on the PC and AntFTP on the phone.

Fire and Forget, delete images at a later date. Sometimes I hav'nt got the time to go resizing.

Can the max width be set in the code above ?


 
Posted : 19/04/2021 5:28 pm
Posts: 8771
Full Member
 

Depends what you want to achieve by resizing as to where you do the resizing client side or server side? Server side achieves smaller downloads for client, client side achieves simpler code for you. None of it's going to be anywhere near as simple as that code up there though.


 
Posted : 19/04/2021 5:34 pm
Posts: 8527
Free Member
Topic starter
 

It's just for personnel use. The img directory will be deleted every now and again via FTP.

Just uploaded a large image as a demo. Looking to get the Bob Ross image to scale smaller or force to max width somehow.


 
Posted : 19/04/2021 5:44 pm
Posts: 8527
Free Member
Topic starter
 

@sirromj Thanks. If its to complex, I will stay with the code above and live with large images. Hoping it was going to something simple with the Echo. If not, so be it.


 
Posted : 19/04/2021 5:47 pm
Posts: 401
Free Member
 

Add width and height to img tag


 
Posted : 19/04/2021 5:49 pm
Posts: 401
Free Member
 

so width=“500px” for example


 
Posted : 19/04/2021 5:50 pm
 toby
Posts: 532
Full Member
 

If you use CSS max-width, it won't try and upscale images that are already small, and if you just set the width (ignoring the height) it should preserve the aspect ratio of each image. So in the code above, I'd add style="max-width: 200px;" to the echo on line 15 between the 'img' and the 'src=...' parts.

Edit to add: obviously, you can tweak the 200 number in that to get an appropriate width for what you're trying to achieve.


 
Posted : 19/04/2021 5:58 pm
Posts: 8527
Free Member
Topic starter
 

@toby
I'll try that now.

Edit: still stuck 🙁

How should line 15 look ?


 
Posted : 19/04/2021 7:49 pm
Posts: 8527
Free Member
Topic starter
 

?
style=”max-width: 200px;

echo '<img_style=”max-width: 200px; src="' . $image . '"><br />';


 
Posted : 19/04/2021 8:14 pm
Posts: 0
Free Member
 

No underscore between "img" and "style", Also your quotes aren't balanced. Try this

echo ‘<br />’;


 
Posted : 19/04/2021 8:21 pm
Posts: 0
Free Member
 

AAgh! the forum messed things up. Had to add a space after the <

echo ‘< img style=”max-width: 200px" src=”‘ . $image . ‘”>< br />’;


 
Posted : 19/04/2021 8:23 pm
Posts: 8527
Free Member
Topic starter
 

@whitestone

That works, awesome 🙂 Just what I'm after.

https://www.sidingstudios.com/mtb/

Thanks for your help guys 🙂

🙂

PS When I copied in your code, the apostrophies seemed to cock it up, made them look like the orginal code and all was good.

PPS All I want now is the ability to delete, an admin area, likes and share options, displaying file name and avatars all in the same one file ;-). Basically and Instagram Clone in one file. ONLY KIDDING.

Thanks again guys.


 
Posted : 19/04/2021 8:35 pm
Posts: 8527
Free Member
Topic starter
 

I've killed it 🙁

But I'm it will work again somehow 🙂


 
Posted : 19/04/2021 8:43 pm
Posts: 8527
Free Member
Topic starter
 

Getting there 🙂

https://www.sidingstudios.com/mtb/

Speech marks seemed to be a problem.

How do I hide the <br/>

RT


 
Posted : 19/04/2021 8:56 pm
Posts: 5297
Full Member
 

Remove the spaces.

<br> Not < br />

The slash you shouldn't need either. It's legacy from xhtml days.


 
Posted : 19/04/2021 11:08 pm
Posts: 30093
Full Member
 

Use % rather than px and it’ll make full use of your browser/device width… responsive layout on the cheap.


 
Posted : 19/04/2021 11:59 pm
Posts: 8527
Free Member
Topic starter
 

@buthcher
That worked nicely.

@kelvin
That looks very nice now.
https://www.sidingstudios.com/mtb/

Might go back to the grid layout. But I now have options 🙂

Thanks again everyone.


 
Posted : 20/04/2021 8:15 am
Posts: 8527
Free Member
Topic starter
 

Who needs Instagram 😉 with this code.


 
Posted : 20/04/2021 8:19 am
Posts: 30093
Full Member
 

Might go back to the grid layout.

Do both. Make it 50% and you’ll have at least two on row… 33% and you’ll have at least three… etc.


 
Posted : 20/04/2021 8:23 am
Posts: 8527
Free Member
Topic starter
 

Even better. Thanks.

I wish I could code. But I'm learning now 🙂


 
Posted : 20/04/2021 8:26 am
Posts: 8527
Free Member
Topic starter
 

Changed to 50% but still getting just a column. Is it something to do with the <BR> ?

https://www.sidingstudios.com/mtb/


 
Posted : 20/04/2021 8:32 am
Posts: 30093
Full Member
 

Yup, remove them. They 'break' the line.


 
Posted : 20/04/2021 8:32 am
Posts: 8527
Free Member
Topic starter
 

@kelvin

Thanks again.

I'll play with that later. Got a fencing panel to install now.

The bit of code could be bigger than Instagram 😉


 
Posted : 20/04/2021 8:37 am
Posts: 8527
Free Member
Topic starter
 

Could'nt not resist. All works like a treat now. Just what I was looking for.

Well chuffed,easy to please.

RT

https://www.sidingstudios.com/mtb/


 
Posted : 20/04/2021 8:40 am
Posts: 8527
Free Member
Topic starter
 

PS If anyone wants the file here is a link.

PS Change TXT to PHP. I'm sure there is a better was to do this for downloading, but there it is. Sorry about REMS. I just wanted to keep a record.

Might try and make the images linked in the future. But it works now and I'm well happy.

Must go now, its nearly 09:00.


 
Posted : 20/04/2021 8:44 am

6 DAYS LEFT
We are currently at 95% of our target!