Page 23 of 37
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:35 am
by simonmlewis
I'm not clear tho why suddenly... it's not working. It's possibly because of an issue with the braces. But it's not showing errors on screen locally like it normally does.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:41 am
by Celauran
Code: Select all
require_once dirname(__DIR__) . '/vendor/autoload.php';
$imagine = new Imagine\Gd\Imagine();
if (isset($resize))
{
if ($resize == "categories" || $resize == "products")
{
// An array of widths, keyed by target screen size
$widths = array(
'475' => 237,
'768' => 384,
'1920' => 451,
);
}
elseif ($resize == "wide")
{
// An array of widths, keyed by target screen size
$widths = array(
'475' => 237,
'768' => 384,
'1920' => 950,
);
}
elseif ($resize == "desktopslide")
{
// An array of widths, keyed by target screen size
$widths = array(
'475' => 475,
'768' => 768,
'1920' => 1920,
);
}
return $widths;
}
// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['homeimage'])) {
// Resize
foreach ($widths as $key => $width) {
$pathinfo = pathinfo($_FILES['homeimage']['name']);
// Open the uploaded image with the Imagine library
$image = $imagine->open($_FILES['homeimage']['tmp_name']);
// Get image size
$box = $image->getSize();
$ratio = $width / $box->getWidth();
$scaled_box = $box->scale($ratio);
$new_filename = "{$random}_{$pathinfo['filename']}_{$key}.{$pathinfo['extension']}";
$image->resize($scaled_box)->save($target_directory . '/' . $new_filename, array('jpeg_quality' => 95));
}
}
Let's try getting rid of all of this. The Composer autoloader is already being required in your functions file and we want to leverage that anyway, so let's start there.
So we can replace this:
Code: Select all
require_once dirname(__DIR__) . '/vendor/autoload.php';
$imagine = new Imagine\Gd\Imagine();
with this:
Code: Select all
require_once '/path/to/functions.php'; // The file with getWidths etc. I don't know what it's called or where it lives.
Next
Code: Select all
if (isset($resize))
{
if ($resize == "categories" || $resize == "products")
{
// An array of widths, keyed by target screen size
$widths = array(
'475' => 237,
'768' => 384,
'1920' => 451,
);
}
elseif ($resize == "wide")
{
// An array of widths, keyed by target screen size
$widths = array(
'475' => 237,
'768' => 384,
'1920' => 950,
);
}
elseif ($resize == "desktopslide")
{
// An array of widths, keyed by target screen size
$widths = array(
'475' => 475,
'768' => 768,
'1920' => 1920,
);
}
return $widths;
}
We're just setting widths here based on the value of $resize. We already have a function that does just that. So instead,
Finally we get to the part where we're handling the upload:
Code: Select all
// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['homeimage'])) {
// Resize
foreach ($widths as $key => $width) {
$pathinfo = pathinfo($_FILES['homeimage']['name']);
// Open the uploaded image with the Imagine library
$image = $imagine->open($_FILES['homeimage']['tmp_name']);
// Get image size
$box = $image->getSize();
$ratio = $width / $box->getWidth();
$scaled_box = $box->scale($ratio);
$new_filename = "{$random}_{$pathinfo['filename']}_{$key}.{$pathinfo['extension']}";
$image->resize($scaled_box)->save($target_directory . '/' . $new_filename, array('jpeg_quality' => 95));
}
}
Again, we've already got a function that does just that, so why not use it?
Code: Select all
// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['homeimage'])) {
resizeImage($_FILES['homeimage']['tmp_name'], $_FILES['homeimage']['name'], $resize);
}
But wait. resizeImage already gets the widths for you, so we don't even need that. Let's remove it and we're left with:
Code: Select all
require_once '/path/to/functions.php'; // The file with getWidths etc. I don't know what it's called or where it lives.
if (!empty($_FILES) && isset($_FILES['homeimage'])) {
resizeImage($_FILES['homeimage']['tmp_name'], $_FILES['homeimage']['name'], $resize);
}
Much cleaner.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:42 am
by Celauran
simonmlewis wrote:I'm not clear tho why suddenly... it's not working. It's possibly because of an issue with the braces. But it's not showing errors on screen locally like it normally does.
I can't run the code locally, obviously, so there's a limit to what I can see and what I can debug. Did you remove the return statement I pointed out earlier? That certainly doesn't belong. Take a look at the code I just posted above. See if it works and see if it makes sense to you.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:45 am
by simonmlewis
Like this?
Code: Select all
// ADD ALL BANNERS (SQUARE AND DOUBLE)
if ($update == "addbanner")
{
if (isset($layertype))
{
$result = mysql_query ("SELECT id FROM products WHERE romancode = '$searchurl'");
$num_result = mysql_num_rows($result);
if ($num_result > 1)
{
$disableupload = "yes";
echo "<script>
window.location.replace('/a_home&status=duplicatecode')
</script>";
}
}
if (!isset($disableupload))
{
if ($stockbanner == "yes")
{
if ($searchurl != "")
{
mysql_query("INSERT INTO homepage (url, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$searchurl', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
elseif ($searchurl == "")
{
mysql_query("INSERT INTO homepage (url, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$url', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
echo "<script>
window.location.replace('/a_home')
</script>";
}
else
{
$target_directory = $_SERVER['DOCUMENT_ROOT']."/images/pages/";
$random = (rand()%99999999);
$pic=($_FILES['homeimage']['name']);
$newname= $random . "_". $pic;
$target = $target_directory . $newname;
if ($searchurl != "")
{
mysql_query("INSERT INTO homepage(url, image, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$searchurl', '$newname', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
elseif ($searchurl == "")
{
mysql_query("INSERT INTO homepage(url, image, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$url', '$newname', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
require_once '/functions/functionConsumerResize.php';
// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['homeimage'])) {
resizeImage($_FILES['homeimage']['tmp_name'], $_FILES['homeimage']['name'], $resize);
}
//Writes the photo to the server
if(move_uploaded_file($_FILES['homeimage']['tmp_name'], $target))
{
echo "<script>
window.location.replace('/a_home')
</script>";
}
}
}}
}
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:47 am
by Celauran
Looks about right. Is that working for you?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:48 am
by Celauran
Although, it looks like you're saving the original image twice, and with two different prefixes.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:49 am
by simonmlewis
I think I See what is going on ther etoo. If the is a file being uploaded, it runs the resizeImage script in the included functions file. And uses the $resize value that I am uploaded in the form.
Then if it files a file has been uploaded to $target, it redirects us back to this page.
Is that it?
Although, it looks like you're saving the original image twice, and with two different prefixes.
Am I ???
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:51 am
by Celauran
simonmlewis wrote:Although, it looks like you're saving the original image twice, and with two different prefixes.
Am I ???
Yes. You've got $target = and move_uploaded_file which generate a name and move the file, but resizeImage is also doing the same thing but generating its own prefix.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:52 am
by simonmlewis
Code: Select all
//Writes the photo to the server
if(move_uploaded_file($_FILES['homeimage']['tmp_name'], $target))
{
echo "<script>
window.location.replace('/a_home')
</script>";
}
Is this now redundant?
I could just put the window.location there on it's own ...?!
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:55 am
by Celauran
Well, sort of. Look at the query above. You're saving $newname based on what's being defined inline. Might be better to have resizeImage return the name of the new image, update the database after saving the images, and then redirect the user. Make sense?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 8:57 am
by simonmlewis
So that part at the top renames the imae and uploads it? (tho frankly I don't see any form of Saving code in there...).
I agree it might be better to do that. Can it return the name of the image from resizeImage in that function, back to this page? So it can then be used in either of those two database scripts?
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 9:00 am
by Celauran
simonmlewis wrote:So that part at the top renames the imae and uploads it? (tho frankly I don't see any form of Saving code in there...).
Which part are you referring to? Can you include code snippets so we know we're talking about the same thing?
simonmlewis wrote:I agree it might be better to do that. Can it return the name of the image from resizeImage in that function, back to this page? So it can then be used in either of those two database scripts?
Sure, why not? We're generating the prefix, saving the image, and currently not returning anything. Should be easy enough to store the new file name as a variable and return it, then capture the return value from the called function and use that to update the database. I'll let you take a stab at that first and will be here to help if you get stuck.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 9:07 am
by simonmlewis
Code: Select all
// ADD ALL BANNERS (SQUARE AND DOUBLE)
if ($update == "addbanner")
{
if (isset($layertype))
{
$result = mysql_query ("SELECT id FROM products WHERE romancode = '$searchurl'");
$num_result = mysql_num_rows($result);
if ($num_result > 1)
{
$disableupload = "yes";
echo "<script>
window.location.replace('/a_home&status=duplicatecode')
</script>";
}
}
if (!isset($disableupload))
{
if ($stockbanner == "yes")
{
if ($searchurl != "")
{
mysql_query("INSERT INTO homepage (url, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$searchurl', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
elseif ($searchurl == "")
{
mysql_query("INSERT INTO homepage (url, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$url', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
echo "<script>
window.location.replace('/a_home')
</script>";
}
else
{
// this is the area you are talking about. I don't see HERE where it SAVES the image. Only whree it puts them into variables.
$target_directory = $_SERVER['DOCUMENT_ROOT']."/images/pages/";
$random = (rand()%99999999);
$pic=($_FILES['homeimage']['name']);
$newname= $random . "_". $pic;
$target = $target_directory . $newname;
// somewhere here I assume it needs to get the resize $newname from the function and use that instead, on here in the database entries.
// we need just the ONE filename
if ($searchurl != "")
{
mysql_query("INSERT INTO homepage(url, image, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$searchurl', '$newname', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
elseif ($searchurl == "")
{
mysql_query("INSERT INTO homepage(url, image, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$url', '$newname', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
}
require_once '/functions/functionConsumerResize.php';
// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['homeimage'])) {
resizeImage($_FILES['homeimage']['tmp_name'], $_FILES['homeimage']['name'], $resize);
}
//Writes the photo to the server
if(move_uploaded_file($_FILES['homeimage']['tmp_name'], $target))
{
echo "<script>
window.location.replace('/a_home')
</script>";
}
}
}}
}
I think this is the new filename: $new_filename.
So I am assuming since that is put into a variable in this process, I don't need to "step into" that function to get it.
So can I remove:
Code: Select all
$target_directory = $_SERVER['DOCUMENT_ROOT']."/images/pages/";
$random = (rand()%99999999);
$pic=($_FILES['homeimage']['name']);
$newname= $random . "_". $pic;
$target = $target_directory . $newname;
And just place $new_filename into the database entries?
But this function is being called AFTER all that....?!
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 9:10 am
by Celauran
Your comments are spot on. The ordering of things needs to be reworked some because we obviously can't know the filename generated by resizeImage until that function has been called. Also keep function scope in mind. Anything declared in resizeImage only exists in the context of resizeImage. This is where return comes in.
Re: Image Resizing Script required - a better one...
Posted: Mon Mar 13, 2017 9:12 am
by simonmlewis
So I move the DB query to be below the function....?