This is it at the moment.
I keep seeing differences between yours and mine. I've never had code before that has $variable,$variable2,$variable3... in a row like that. So trying to get a grasp on how each area uses that sort of string.
This is still product big errors.
When I get errors in my own code I tend to know the cause and go straight to it. I am assuming with 'functions' you are very similar.
I have never used functions, but this lesson is helping me a lot. Just trying to grasp it, learn it, and see the magic it can do.
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;
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 = [
'475' => 237,
'768' => 384,
'1920' => 451,
];
break;
case 'wide':
$widths = [
'475' => 237,
'768' => 384,
'1920' => 950,
];
break;
case 'desktopslide':
$widths = [
'475' => 475,
'768' => 768,
'1920' => 1920,
];
break;
default:
$widths = [];
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;
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);
}
?>
[text]Fatal error: Uncaught exception 'Imagine\Exception\InvalidArgumentException' with message 'File C:\xampp\phpMyAdmin\site-wide\images\stockbanners\ does not exist.' in C:\xampp\phpMyAdmin\site-wide\vendor\imagine\imagine\lib\Imagine\Image\AbstractImagine.php:72 Stack trace: #0 C:\xampp\phpMyAdmin\site-wide\vendor\imagine\imagine\lib\Imagine\Gd\Imagine.php(86): Imagine\Image\AbstractImagine->checkPath('C:\\xampp\\phpMyA...') #1 C:\xampp\phpMyAdmin\site-wide\functions\functionConsumerResize.php(148): Imagine\Gd\Imagine->open('C:\\xampp\\phpMyA...') #2 C:\xampp\phpMyAdmin\site-wide\functions\functionConsumerResize.php(49): resizeSingleImage(NULL, 475, 237, 'stockbanners') #3 C:\xampp\phpMyAdmin\site-wide\includes\page.inc(310): getSrcSet(NULL, 'stockbanners') #4 C:\xampp\phpMyAdmin\site-wide\index.php(42): include('C:\\xampp\\phpMyA...') #5 C:\xampp\phpMyAdmin\site-wide\index.php(437): getPage(Object(PDO)) #6 {main} thrown in C:\xampp\phpMyAdmin\site-wide\vendor\imagine\imagine\lib\Imagine\Image\AbstractImagine.php on line 72[/text]