Re: Image Resizing Script required - a better one...
Posted: Tue Mar 07, 2017 8:01 am
So that all runs after my initial upload is run?
I'd need to pass over the $resize variable to know what sizes are being asked for but still... ?
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 = $_SERVER['DOCUMENT_ROOT']."/images/pages/";
$random = (rand()%99999999);
$pic=($_FILES['homeimage']['name']);
$newname="$random"."$pic";
$target = $target . $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 dirname(__DIR__) . '/vendor/autoload.php';
$imagine = new Imagine\Gd\Imagine();
// An array of widths, keyed by target screen size
$widths = [
'475' => 237,
'768' => 384,
'1920' => 451,
];
// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['image']) && $_FILES['image']['error'] !== 0) {
$target_directory = __DIR__ . '/images/pages';
$pathinfo = pathinfo($_FILES['image']['name']);
$prefix = (rand() % 99999999);
// Open the uploaded image with the Imagine library
$image = $imagine->open($_FILES['image']['tmp_name']);
// Save the original
$image->save($target_directory . '/' . $pathinfo['basename']);
// Get image size
$box = $image->getSize();
// Resize
foreach ($widths as $key => $width) {
$ratio = $width / $box->getWidth();
$scaled_box = $box->scale($ratio);
$new_filename = "{$prefix}_{$pathinfo['filename']}{$key}.{$pathinfo['extension']}";
$image->resize($scaled_box)->save($target_directory . '/' . $new_filename);
}
}
//Writes the photo to the server
if(move_uploaded_file($_FILES['homeimage']['tmp_name'], $target))
{
echo "<script>
window.location.replace('/a_home')
</script>";
}
}
}}
}