Jpg Loop Produces N Times Last Picture
Posted 24 May 2011 - 1:19 PM
I have a simple foreach loop accessing an array of up to 8 stored jpgs and I have also tested this with 3 simple repetitive entries. The objective of this script is to use one subroutine to generate new pictures at a chosen new width on the fly.
For each value (the master picture name) in the array I set 2 session variables and pass them to '_a_makePicByWidth.php', which is inside image tags.
// repeated test code start
$_SESSION['picName'] = $picListArray['7']['1'] . '.jpg' ;
$_SESSION['newWidth'] = 120 ;
echo '<img src="_a_makePicByWidth.php" />' ;
//repeated code end
// for testing repeated 3 times to display 3 pictures at different sizes, varying the '7' and the '120'.
I have tested with echos and all the parameters are available in the '_a_makePicByWidth.php' script, but the output is 3 times the same last picture at the last size specified, and the same happens with the foreach loop. There's 8 picture names in the test array, and the last one is shown 8 times. To further clarify, I can change the $newWidth parameter in the 3rd entry, then the output displays the last picture 3 times at the new size, so the iteration is occuring
Here's an extract from the '_a_makePicByWidth.php' script:
// there are other session parameters, these are just the relevant variables
$newWidth = $_SESSION['newWidth'] ;
$picName = $_SESSION['picName'] ;
// If I put echoes in here, I get only the last values of $picName and $newWidth whether it is the 3 test entries
// or the array/foreach loop with 8 values of filename.
// more or less straight out of the book
$srcPic = imagecreatefromjpeg($filePath . $picName);
list($width, $height) = getimagesize($filePath . $picName) ;
$newHeight = round($height * $newWidth/$width) ;
$newPic = ImageCreateTrueColor($newWidth, $newHeight) ;
imagecopyresampled($newPic, $srcPic, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
//serve the new picture
header('Content-Type: image/jpg') ;
// none of this below has any effect!
$_SESSION['newWidth'] = '' ;
$_SESSION['picName'] = '';
$newWidth = '' ;
$picName = '' ;
$newPic = '' ;
// end of script
It's been hours and few more that I've spent on this, cannot see why the same last picture name and size are repeated. Any suggestions gratefully received.
Posted 24 May 2011 - 1:27 PM
For me it would be easier if you posted all your code, as I like to get a proper feel for it
Posted 24 May 2011 - 9:46 PM
Anyway, you're probably going to have to look into the innerworkings of $_SESSION, and see exactly what's going on behind the scenes, or you could try using the GET method instead. For example, you could change the following line as follows:
echo '<img src="_a_makePicByWidth.php" />' ; echo '<img src="_a_makePicByWidth.php?picName=ronald_mcdonald.jpg" />';
And then you could retrieve that info in the PHP script as follows:
$picName = $_GET['picName'];
Naturally, you'd have to add the info for the width as well.
Anyway, that's just one possible solution. To be honest, there are two things I would recommend not doing:
1) Not using $_SESSION for this kind of thing.
2) While you can call a PHP script for img src, I wouldn't recommend it. Maybe it's totally kosher, but something about it really bugs me.
Well, hope that helps out a little.
Posted 25 May 2011 - 2:15 AM
Posted 25 May 2011 - 2:58 AM
According to the W3C specs for img tags in HTML 4.01 (and I imagine HTML5 is the same), the src attribute is used as follows:
src = uri [CT]
This attribute specifies the location of the image resource. Examples of widely recognized image formats include GIF, JPEG, and PNG.
So that right there tells me that (even if they are usable) PHP scripts should not be referenced from a src attribute. Also, I found the following on the W3C wiki:
src = URL potentially surrounded by spaces
Specifies a URL referencing a non-interactive, optionally animated, image that is neither paged nor scripted.
Again, "image" to me does not mean "PHP script". As we all know, HTML let's us get away with a lot of things that we shouldn't do. Also, looking at the XHTML specs, I don't see anything indicating anything different.
Furthermore, if you want to display images via a PHP script, there just seem like so many other (easier) ways.
For example, writing and calling a function that outputs an image to the screen, or simply doing something like the following:
<?php ... <img src="' . $imagePath . '"> ... ?>
I mean, really, is it that hard? The one exception would be images not in the web root, I suppose, but again, that is not really relevant to this discussion.
Well, that's just my two cents. Admittedly, I'm kinda trying to just find a reason because you asked, but something tells me it's odd, regardless.
Posted 25 May 2011 - 6:35 AM
A couple of answers:
- We don't want to expose the URL of the pictures, so they are stored above the webroot. This means they can't be called from within the html using a simple path (by either us or a picture-stealer-person). Also we want to use a single master picture and resize it as required (on the fly) so if the site design changes we can make simple changes to the code to get a new size. The alternative would mean keeping or remaking many pictures and storing them accordingly.
- I had considered the $_GET superglobal, but initial tests had not proved successful so I had been concentrating on fixing the $_SESSION problem. However, since there doesn't seem to be a way of getting the $_SESSION to work, I have now recoded using the $_GET method for the width and name parameters. And it works! About which I am both relieved and pleased! It's crazy though as other session variables are recovered and used, so it does seem to be down to the repetitive nature of the code to display multiple pictures.
- I don't think there is a problem using <img src="aScript.php" /> method and I'm not aware of any other way of doing it. Given an alt attribute it certainly validates.
So many thanks for the stimulus of your responses. I'd prefer not to even expose the picture's name, but given the dead-end of the $_SESSION route, at least the $_GET method is up and working.
Thank you all.
Posted 25 May 2011 - 6:56 AM
As for wanting the images outside the web root, well, okay. It seems like a lot of unnecessary headaches to protect some information that seems unnecessary to protect, but then again, I'm not aware of the nature of your site.
Anyway, I looked into it a bit more thoroughly, and while (according to the W3C) scripts are not valid values for the src attribute, it obviously works, and is pretty much the only way to do what you want to do.
Well, good luck with that all. I think at this point, you have enough to go on. You still might want to try reading more about the $_SESSION superglobal. Here's a link to get you started:
Posted 25 May 2011 - 11:32 AM
I'm going to stick with sizing on the fly for now, and see how the server copes if this site gets busy. For now I need the flexibility until the design is stable (probably never...).
Thanks for that link. There's a comment at the bottom of the page from 'pushedx' which would seem to reveal the cause of the problem with $_SESSION. It suggests that the session variables are only written (= updated) at the end of the script, so my 3rd or nth entry will be the only one used, the previous values will be overwritten. So it takes the last set of values and iterates n times with those values. In this case, the $_GET array would appear to be the only way.
Btw, Jonathan - yes the headers already sent is a problem I've encountered when serving a jpg file via a script. That's why the external script must not even have any white space above the php tags - even a space is 'output'.
all the best
Posted 25 May 2011 - 12:20 PM
Yes tonywuk I'm aware of this, but thanks for taking the time to reply.
Posted 25 May 2011 - 7:18 PM
Regardless, I would be curious to hear what Larry has to say.
Also, tony, good find in that SESSION article. I didn't actually read the whole thing, but I figured that some pertinent info had to be in there somewhere.
I guess we all learned something with this topic. Thanks.
Posted 1 June 2011 - 11:02 AM
If HartleySan, or anyone, doesn't care for a practice, just because it rubs you the wrong way, that's completely understandable and certainly okay, and there are always 12 ways of doing things, with very few clearly better than others, but I definitely believe it's fine to use this approach when the need arises.
Posted 2 June 2011 - 1:21 AM
Anyway, regarding proxy PHP scripts for accessing images outside of the web root, I honestly could not think of any other way to do it, so I guess it'll have to be fine. Not to mention the fact that it just plain works, which is indisputable.
Larry, if you don't mind me asking, if you use a proxy PHP script to load images outside of the web root, is it pretty straightforward to load a lot of images from one script, and then place HTML all around those images?
Posted 2 June 2011 - 11:07 PM
Posted 2 June 2011 - 11:10 PM