Page 1 of 1
Disabling Image use remotely...
Posted: Fri Feb 02, 2018 9:21 am
by simonmlewis
Hi. We are looking at setting up an a fully custom image hosting platform.
The idea is they create a free account securely, that pings an email to us. We use our Subscription gateway to them submit them a form to setup their monthly account costs.
On receipt, we enable their account to begin uploading image files.
Each file is in the database and stored to their unique ID.
The issue is, if they decide to stop paying, how do we stop them from using the URL of the images they have uploaded? The only way I could think was to use hotlinking, to prevent it, but I don't know how to do that "per image".
Or, one button that would disable their access to their images screen, but also renames the file with a character at the start, thus meaning the image URL they have used somewhere, it now invalid.
Is this a good way, or is there a better way?
Once they pay again, we can click a button which would remove those first 2-3 characters off the filename and thus unlock it all.
Re: Disabling Image use remotely...
Posted: Fri Feb 02, 2018 9:30 pm
by Christopher
Why not just move their image directory out of public or remove the symbolic link in public to their image directory. That would be faster than renaming a bunch of files. If the account is inactive it is in /inactive if it is active it is in /www. Or serve all the images through a proxy and authenticate there.
Re: Disabling Image use remotely...
Posted: Sat Feb 03, 2018 12:25 pm
by simonmlewis
No idea about proxy.
I could serve them through their own url, and just change that url virtual folder. Tho not sure that would
Work. Worth a try.
Re: Disabling Image use remotely...
Posted: Tue Feb 13, 2018 6:28 am
by simonmlewis
The plan is now to create a folder on the fly as they create an account. It's a numeric folder name.
The name goes into the database.
When they upload a folder, it gets added to their folder. Here lies an issue. How do I get the system to know what folder they are using.
This is the function being used to process it, but I cannot see how to pass thru the folder name from the form, to this PHP function.
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)
{
// default for all paths,.
$paths = array(
'fullsize' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'uploads',
'thumbnail' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'uploads',
);
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);
}
?>
Re: Disabling Image use remotely...
Posted: Tue Feb 13, 2018 7:42 am
by simonmlewis
I thought I could do this:
Code: Select all
...
$folder = isset($_POST['folder']) ? $_POST['folder'] : null;
function getPaths($resize_type)
{
// default for all paths,.
$paths = array(
'fullsize' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
'thumbnail' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
);
return $paths;
}
...
But it doesn't work. It doesn't see the variable, so it puts the images in the images folder.
Re: Disabling Image use remotely...
Posted: Tue Feb 13, 2018 6:46 pm
by Celauran
simonmlewis wrote:I thought I could do this:
Code: Select all
...
$folder = isset($_POST['folder']) ? $_POST['folder'] : null;
function getPaths($resize_type)
{
// default for all paths,.
$paths = array(
'fullsize' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
'thumbnail' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
);
return $paths;
}
...
But it doesn't work. It doesn't see the variable, so it puts the images in the images folder.
That's outside the scope of any of the functions, so it's just sitting there doing nothing. If you want to use something inside a function, it needs to either be declared inside the function or passed in as an argument. This also doesn't necessarily look like the right place to be doing it.
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 5:36 am
by simonmlewis
Mmmm I'm a bit lost then.
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)
{
// 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);
}
?>
Form here:
Code: Select all
<form method='POST' action='/my79' enctype='multipart/form-data'>
<input type='file' name='photoupload'><br/>
<input type='hidden' name='resize' value='thumbnails'>
<input type='hidden' name='folder' value='$folder'>
<input type='submit' value='Upload'>
</form>
Processing bit here (update the sql_query will be done):
Code: Select all
$resize = isset($_POST['resize']) ? $_POST['resize'] : null;
$folder = isset($_POST['folder']) ? $_POST['folder'] : null;
require_once 'functions/functionConsumerResize.php';
// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['photoupload'])) {
$saved_filename = resizeImage($_FILES['photoupload']['tmp_name'], $_FILES['photoupload']['name'], $resize);
}
mysql_query("INSERT INTO uploads(userid, filename) VALUES ('$userid', '$saved_filename')");
}
I just don't see how to pass over the $folder name through to the function.
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 5:47 am
by Celauran
You're calling resizeImage, so you'll need to modify that function to accept a folder as an optional parameter. If a folder is provided, you can do whatever you need with it. You could pass it through to getPaths, but I think a better option would be to leave that alone and append the folder, if you have one, inside resizeImage
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 5:58 am
by simonmlewis
How do I add that into resizeImage?
Both fullsize and thumbnail are going to the same place. So sort of wondering why I am using this function at all, for this purpose. It's original purpose was to store various sizes in various folders. But now, I want to store various sizes in /images but then add on the /$folder/ on the end of it, based on the settings for each user.
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 6:18 am
by Celauran
If you're storing everything in the same directory, or all of a user's images in a directory just for them, then maybe getPaths ought to be modified. I expect most of the rest of the functionality can remain unchanged.
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 6:34 am
by simonmlewis
How would I pass thru the variable to getosghs from the POST form?
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 6:44 am
by Celauran
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 7:11 am
by simonmlewis
Code: Select all
// 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'];
.....
}
I'm using these within the form:
<input type='hidden' name='resize' value='thumbnails'>
<input type='hidden' name='folder' value='$folder'>
I don't see where $resize is being fed into, as it was a while back this code was done.
I need to do $folder in the same way, and then tag it on the end of the two directories - just a bit stuck on how.
Re: Disabling Image use remotely...
Posted: Wed Feb 14, 2018 9:17 am
by simonmlewis
Celauran wrote:simonmlewis wrote:I thought I could do this:
Code: Select all
...
$folder = isset($_POST['folder']) ? $_POST['folder'] : null;
function getPaths($resize_type)
{
// default for all paths,.
$paths = array(
'fullsize' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
'thumbnail' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $folder,
);
return $paths;
}
...
But it doesn't work. It doesn't see the variable, so it puts the images in the images folder.
That's outside the scope of any of the functions, so it's just sitting there doing nothing. If you want to use something inside a function, it needs to either be declared inside the function or passed in as an argument. This also doesn't necessarily look like the right place to be doing it.
This worked:
Code: Select all
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;
}
Simply moving that variable.