Celauran wrote:simonmlewis wrote:Exactly. Trying to make it work locally, with the () style of script, but I tweaked it uploaded it and it failed. Made the mistake of not checking here.
Let's take a step back, then. What happened to the function call? That's still the best technique to employ as it's the most easily re-used. We can tweak the inner workings of the function a little if we need to, but let's be careful not to undo what we had achieved last week. Can you roll it back to a last known good state and we'll work through the modifications needed to get it working on 5.3?
That's good about Composer.
This is the Function.
Code: Select all
<?php
require_once '/vendor/autoload.php';
// Returns required quality settings for Imagine based on image's extension
function getImageOptions($extension)
{
switch ($extension) {
case 'jpg':
case 'jpeg':
$options = ['jpeg_quality' => 97];
break;
case 'png':
$options = ['png_compression_level' => 8];
break;
default:
$options = [];
}
return $options;
}
function getSrcSet($filename, $resize_type = 'wide')
{
switch($resize_type) {
case 'stockbanners':
$actualfolder = 'stockbanners';
break;
case 'thumbnails':
$actualfolder = 'productphotos/small';
break;
default:
$actualfolder = 'pages';
break;
}
$root_directory = dirname(__DIR__);
$images_directory = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $actualfolder;
$images_path = "/images/{$actualfolder}"; // Still need this for srcset output
$source_directory = $root_directory . $images_directory;
$srcset = [];
$widths = getWidths($resize_type);
if (file_exists($source_directory . DIRECTORY_SEPARATOR . $filename)) {
$basename = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
foreach ($widths as $size => $width) {
$fullname = $basename . '_' . $size . '.' . $extension;
if (file_exists($source_directory . DIRECTORY_SEPARATOR . $fullname)) {
$srcset[] = "{$images_path}/{$fullname} {$size}w";
} else {
resizeSingleImage($filename, $size, $width, $actualfolder);
$srcset[] = "{$images_path}/{$fullname} {$size}w";
}
}
}
return implode(', ', $srcset);
}
// Returns an array of widths, keyed by target screen size
function getWidths($resize_option)
{
switch($resize_option) {
case 'categories':
case 'products':
case 'stockbanners':
$widths = array(
'475' => 237,
'768' => 384,
'1920' => 451,
);
break;
case 'wide':
$widths = array(
'475' => 237,
'768' => 384,
'1920' => 950,
);
break;
case 'desktopslide':
$widths = array(
'475' => 475,
'768' => 768,
'1920' => 1920,
);
break;
case 'thumbnails':
$widths = array(
'475' => 237,
'768' => 384,
'1920' => 451,
'2520' => 600,
);
break;
default:
$widths = array();
break;
}
return $widths;
}
// Takes input from a form post and saves original image plus all required resizes
function resizeImage($path_to_file, $file_name, $resize_type)
{
$imagine = new Imagine\Gd\Imagine();
switch($resize_type) {
case 'stockbanners':
$actualfolder = 'stockbanners';
break;
case 'thumbnails':
$actualfolder = 'productphotos/small';
break;
default:
$actualfolder = 'pages';
break;
}
$target_directory = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $actualfolder;
$pathinfo = pathinfo($file_name);
$prefix = time();
// Open the uploaded image with the Imagine library
$image = $imagine->open($path_to_file);
// Save the original
$options = getImageOptions($pathinfo['extension']);
$path_to_original = $target_directory . DIRECTORY_SEPARATOR . "{$prefix}{$pathinfo['basename']}";
$image->save($path_to_original, $options);
// Resize
// Get the size of the original image
$box = $image->getSize();
// Get the sizes we need
$widths = getWidths($resize_type);
// Create resized images
foreach ($widths as $key => $width) {
$ratio = $width / $box->getWidth();
$scaled_box = $box->scale($ratio);
$new_filename = "{$prefix}{$pathinfo['filename']}_{$key}.{$pathinfo['extension']}";
// Re-open the original for scaling so we're not creating a larger image from a smaller
$source = $imagine->open($path_to_original);
$source->resize($scaled_box)->save($target_directory . DIRECTORY_SEPARATOR . $new_filename, $options);
}
}
// Resizes a single image to a specific size and with a specific suffix
function resizeSingleImage($original, $suffix, $width, $save_path)
{
$imagine = new Imagine\Gd\Imagine();
$target_directory = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $save_path;
$pathinfo = pathinfo($original);
// Open the uploaded image with the Imagine library
$image = $imagine->open($target_directory . DIRECTORY_SEPARATOR . $original);
// Get the size of the original image
$box = $image->getSize();
$ratio = $width / $box->getWidth();
$scaled_box = $box->scale($ratio);
$new_filename = "{$pathinfo['filename']}_{$suffix}.{$pathinfo['extension']}";
$options = getImageOptions($pathinfo['extension']);
$image->resize($scaled_box)->save($target_directory . DIRECTORY_SEPARATOR . $new_filename, $options);
}
?>
I had to add a new set for the Product Photo Thumbnails. For their location and size.
Then I went to test on live and you told me about the () requirements. So I added that.
But the Resize tool when admin upload a photo doesn't use this.
If I add the function include to my categ.inc file, that WORKS. I see the images being generated, and the page loads up.
So I think we need to resolve the Upload tool.
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 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));
}
}
//Writes the photo to the server
if(move_uploaded_file($_FILES['homeimage']['tmp_name'], $target))
{
echo "<script>
window.location.replace('/a_home')
</script>";
}
}
}}
}