Image Resizing Script required - a better one...
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...
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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));
}
}So we can replace this:
Code: Select all
require_once dirname(__DIR__) . '/vendor/autoload.php';
$imagine = new Imagine\Gd\Imagine();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.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;
}Code: Select all
$widths = getWidths($resize);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));
}
}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);
}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);
}Re: Image Resizing Script required - a better one...
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.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.
-
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...
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>";
}
}
}}
}Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
Looks about right. Is that working for you?
Re: Image Resizing Script required - a better one...
Although, it looks like you're saving the original image twice, and with two different prefixes.
-
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...
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?
Then if it files a file has been uploaded to $target, it redirects us back to this page.
Is that it?
Am I ???Although, it looks like you're saving the original image twice, and with two different prefixes.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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.simonmlewis wrote:Am I ???Although, it looks like you're saving the original image twice, and with two different prefixes.
-
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...
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>";
}I could just put the window.location there on it's own ...?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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?
-
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...
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?
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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
Which part are you referring to? Can you include code snippets so we know we're talking about the same thing?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...).
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.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?
-
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...
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>";
}
}
}}
}
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;But this function is being called AFTER all that....?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Image Resizing Script required - a better one...
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.-
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...
So I move the DB query to be below the function....?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.