How can I add in a file renamer to this function?
Posted: Thu Feb 15, 2018 6:18 am
This is about another thread, but this question is much more defined - hence a new thread.
I want to make sure new image filenames uploaded, have hyphens added where there is a space. This is especially important if the user wishes to use the image as a background image.
I am trying to see where in the code I can look for spaces, and replace with hyphens.
I suppose I could do it before it reaches this script, but be better if it was part of it.
I want to make sure new image filenames uploaded, have hyphens added where there is a space. This is especially important if the user wishes to use the image as a background image.
I am trying to see where in the code I can look for spaces, and replace with hyphens.
I suppose I could do it before it reaches this script, but be better if it was part of it.
Code: Select all
<?php
require_once (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 = array('jpeg_quality' => 95);
break;
case 'png':
$options = array('png_compression_level' => 8);
break;
default:
$options = array();
}
return $options;
}
function getPaths($resize_type)
{
$folder = isset($_POST['folder']) ? $_POST['folder'] : null;
// default for all paths,.
$paths = array(
'fullsize' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
'thumbnail' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
);
return $paths;
}
function getSrcSet($filename, $resize_type = 'wide')
{
$root_directory = dirname(__DIR__);
$paths = getPaths($resize_type);
// sets the source path for the big images
$fullsize_directory = $root_directory . $paths['fullsize'];
// sets the source path for the thumbnail images (prod photos)
$thumbnail_directory = $root_directory . $paths['thumbnail'];
$images_path = str_replace(DIRECTORY_SEPARATOR, '/', $paths['thumbnail']);
$srcset = array();
$widths = getWidths($resize_type);
// Does the original image exist?
if (file_exists($fullsize_directory . DIRECTORY_SEPARATOR . $filename)) {
// If it does, are we dealing with productphotos? We don't want to
// accidentally delete a full-sized image from /pages or /stockbanners
// ENABLE this script when we are ready to definitely delete the old thumbnails.
// if ($resize_type === 'thumbnails') {
// File with the same name as the original but in the thumbs directory
// $old_thumbnail = $thumbnail_directory . DIRECTORY_SEPARATOR . $filename;
// Does this file exist?
// if (file_exists($old_thumbnail)) {
// Delete it
// unlink($old_thumbnail);
// }
// }
$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";
}
}
if ($resize_type == "uploads")
{
$srcset[] = "{$images_path}/{$filename} 2560w";
}
}
return implode(', ', $srcset);
}
// Returns an array of widths, keyed by target screen size
function getWidths($resize_option)
{
switch($resize_option) {
case 'thumbnails':
$widths = array(
'475' => 237,
'768' => 384,
);
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();
$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);
}
return $renamed;
}
// 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();
$paths = getPaths($save_path);
// get the source file ready for renaming
$source_directory = dirname(__DIR__) . $paths['fullsize'];
// where is the file going to be saved?
$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);
}
?>