PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Aug 19, 2018 7:20 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Fri Feb 02, 2018 10:21 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Fri Feb 02, 2018 10:30 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13573
Location: New York, NY, US
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.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat Feb 03, 2018 1:25 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Tue Feb 13, 2018 7:28 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
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.

Syntax: [ Download ] [ Hide ]
<?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' => <img src="./images/smilies/icon_cool.gif" alt="8)" title="Cool" />;
            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);
}
?>

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Tue Feb 13, 2018 8:42 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
I thought I could do this:

Syntax: [ Download ] [ Hide ]
...
$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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Tue Feb 13, 2018 7:46 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
simonmlewis wrote:
I thought I could do this:

Syntax: [ Download ] [ Hide ]
...
$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.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 6:36 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
Mmmm I'm a bit lost then.

Syntax: [ Download ] [ Hide ]
<?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' => <img src="./images/smilies/icon_cool.gif" alt="8)" title="Cool" />;
            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:

Syntax: [ Download ] [ Hide ]
<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):
Syntax: [ Download ] [ Hide ]
$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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 6:47 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
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

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 6:58 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 7:18 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
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.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 7:34 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
How would I pass thru the variable to getosghs from the POST form?

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 7:44 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6424
Location: Montreal, Canada
http://php.net/manual/en/language.functions.php

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 8:11 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
// 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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 10:17 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4420
Location: United Kingdom
Celauran wrote:
simonmlewis wrote:
I thought I could do this:

Syntax: [ Download ] [ Hide ]
...
$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:
Syntax: [ Download ] [ Hide ]
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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group