Bah. That was my bad. I was having you try to concatenate an array and a string.
I took the time to debug and test it on my machine. This works. You'll need to tweak the short array syntax and maybe fix the paths, but this works exactly as intended. Go through the code and we can discuss anything you're unclear on.
Code: Select all
<?php
require_once dirname(dirname(__DIR__)) . '/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' => 85];
break;
case 'png':
$options = ['png_compression_level' => 8];
break;
default:
$options = [];
}
return $options;
}
function getPaths($resize_type)
{
$paths = [
'fullsize' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'pages',
'thumbnail' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'pages',
];
if ($resize_type === 'thumbnails') {
$paths['fullsize'] = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos';
$paths['thumbnail'] = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR . 'small';
}
if ($resize_type === 'stockbanners') {
$paths['fullsize'] = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'stockbanners';
$paths['thumbnail'] = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'stockbanners';
}
return $paths;
}
function getSrcSet($filename, $resize_type = 'wide')
{
$root_directory = dirname(__DIR__);
$paths = getPaths($resize_type);
$fullsize_directory = $root_directory . $paths['fullsize'];
$thumbnail_directory = $root_directory . $paths['thumbnail'];
$images_path = str_replace(DIRECTORY_SEPARATOR, '/', $paths['thumbnail']);
$srcset = [];
$widths = getWidths($resize_type);
if (file_exists($fullsize_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($thumbnail_directory . DIRECTORY_SEPARATOR . $fullname)) {
$srcset[] = "{$images_path}/{$fullname} {$size}w";
} else {
resizeSingleImage($filename, $size, $width, $resize_type);
$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;
case 'thumbnails':
$widths = [
'475' => 237,
'768' => 384,
'1920' => 451,
'2520' => 600,
];
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();
$paths = getPaths($resize_type);
$fullsize_directory = dirname(__DIR__) . $paths['fullsize'];
$thumbnail_directory = dirname(__DIR__) . $paths['thumbnail'];
$pathinfo = pathinfo($file_name);
$prefix = time();
$renamed = "{$prefix}_{$pathinfo['basename']}";
// Open the uploaded image with the Imagine library
$image = $imagine->open($path_to_file);
// Save the original
$options = getImageOptions($pathinfo['extension']);
$path_to_original = $fullsize_directory . DIRECTORY_SEPARATOR . $renamed;
$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($thumbnail_directory . DIRECTORY_SEPARATOR . $new_filename, $options);
}
// We don't want the full path, just the file name
return $renamed;
}
// Resizes a single image to a specific size and with a specific suffix
function resizeSingleImage($original, $suffix, $width, $type)
{
$imagine = new Imagine\Gd\Imagine();
$paths = getPaths($type);
$source_directory = dirname(__DIR__) . $paths['fullsize'];
$target_directory = dirname(__DIR__) . $paths['thumbnail'];
$pathinfo = pathinfo($original);
// Open the uploaded image with the Imagine library
$image = $imagine->open($source_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);
}