PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Jun 25, 2017 3:54 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 546 posts ]  Go to page Previous  1 ... 33, 34, 35, 36, 37  Next
Author Message
PostPosted: Wed Mar 15, 2017 8:37 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6249
Location: Montreal, Canada
simonmlewis wrote:
I See. So outside of that loop, I then add this into the srcset, if the resize_type is one of the three.

Syntax: [ Download ] [ Hide ]
$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 == "categories" || $resize_type == "products" || $resize_type == "wide")
  {
    $srcset[] = "{$images_path}/{$filename} {$size}w";
  }
}

return implode(', ', $srcset);
}

Better. Where is $size coming from in this instance?

simonmlewis wrote:
But how do I tell it this is for 2560 ?

What's 2560? You keep mentioning this number but you aren't resizing an image to that size. You've got a list of the desired smaller sizes. You just want to add the size of the original image to the srcset list. So again, what's this 2560?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:41 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
$size is within that overall function. Is that not putting the $size into the srcset code?
2560 is the resolution, like we have 475, 768 and 1920. Do I need to add it into the list at the top? Tho I thought that was about fetching the sizing information to tell it the pixel width to use.

As you say, we are not resizing this image, but we do need to tell it /images/pages/123.jpg 2560w.... don't we?

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


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:42 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
I think I need to add it to th top case list. But each of those has a number assigned to it, to resize the image. We aren't resizing.

This is why I am a bit confused.

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


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:43 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6249
Location: Montreal, Canada
simonmlewis wrote:
As you say, we are not resizing this image, but we do need to tell it /images/pages/123.jpg 2560w.... don't we?

Would it not make more sense to tell it the actual width of the image?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:44 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6249
Location: Montreal, Canada
simonmlewis wrote:
I think I need to add it to th top case list. But each of those has a number assigned to it, to resize the image. We aren't resizing.

This is why I am a bit confused.

You don't want to put it in getWidths. That's for resizing. As you say, we aren't resizing this. Doesn't belong there.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:45 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
it would vary. But I think that code is in that script, asking what it is.. is it not?
Why is that important?

And no, didn't think it should go in there. Just wondered since that gathers sizing info.

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


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:47 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6249
Location: Montreal, Canada
simonmlewis wrote:
it would vary. But I think that code is in that script, asking what it is.. is it not?
Why is that important?

It's not clear what you're replying to here. What would vary? Why is what important?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:50 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
Celauran wrote:
simonmlewis wrote:
As you say, we are not resizing this image, but we do need to tell it /images/pages/123.jpg 2560w.... don't we?

Would it not make more sense to tell it the actual width of the image?

Why would putting in the actual size be right? why not put in 2560?

I know something has to go on the end there where $size is. Does it not?
Are you suggesting that rather than put in $size, we put in $currentimagesize ?

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


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:52 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6249
Location: Montreal, Canada
simonmlewis wrote:
Celauran wrote:
simonmlewis wrote:
As you say, we are not resizing this image, but we do need to tell it /images/pages/123.jpg 2560w.... don't we?

Would it not make more sense to tell it the actual width of the image?

Why would putting in the actual size be right? why not put in 2560?

That was my understanding of how srcset is meant to work (link). Give it a list of images and their widths. You can hardcode 2560 if you prefer.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:54 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
The only reason I am saying 2560, is because the others are labelled correctly. If I had whatever the current size is, it would make less sense. So I think I am very close. Do I just hardcode in:
Syntax: [ Download ] [ Hide ]
if ($resize_type == "categories" || $resize_type == "products" || $resize_type == "wide")
  {
    $srcset[] = "{$images_path}/{$filename} 2560w";
  }
}

?

So that line goes on the end of the array, for those three images?

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


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:56 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6249
Location: Montreal, Canada
simonmlewis wrote:
The only reason I am saying 2560, is because the others are labelled correctly. If I had whatever the current size is, it would make less sense. So I think I am very close. Do I just hardcode in:
Syntax: [ Download ] [ Hide ]
if ($resize_type == "categories" || $resize_type == "products" || $resize_type == "wide")
  {
    $srcset[] = "{$images_path}/{$filename} 2560w";
  }
}

?

So that line goes on the end of the array, for those three images?

That would accomplish what you're trying to achieve, yes. Still not sure that you aren't meant to put in the actual width of the image, but that's neither here nor there.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 8:58 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
We aren't putting in the size of ANY of the actual images, unless we are resizing them to a specific size. I think in the source code of the rendered page, it would make way more sense to see 475, 768, 1920, 2560. Rather than, 576, 768, 1920, 650/750 etc. As that would probably confuse. It would tell us the size of it, but that area is to tell the browser (and me) what image is being used, and where.

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


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 9:01 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6249
Location: Montreal, Canada
As I've stated, front end isn't my strong suit, so I'm not going to argue with how it's meant to work or not work. What I can do is try to help you accomplish the goal you've set out for yourself. If you would like to have 2560w for every full sized image, then hardcoding that seems to me to be the simplest solution.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Mar 15, 2017 9:03 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
Yeah - since it will be used ONLY for that size of screen. Well, that and above one day!

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


Top
 Profile  
 
PostPosted: Fri Mar 24, 2017 6:58 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4220
Location: United Kingdom
Trying to see if there is a simple way to NOT run any compression if the image is 2560....?

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' => 90);
            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 . 'pages',
        'thumbnail' => DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'pages',
    );
// set for product photo uploads
    if ($resize_type === 'thumbnails') {
        $paths['fullsize'] = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos';
        $paths['thumbnail'] = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR . 'small';
    }
// set for stock banner uploads
    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);
    // 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 == "categories" || $resize_type == "products" || $resize_type == "wide" || $resize_type == "stockbanners" || $resize_type == "desktopslide")
  {
    $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 'categories':
        case 'products':
        case 'stockbanners':
            $widths = array(
                '475' => 237,
                '768' => 384,
                '1920' => 451,
            );
            break;
        case 'wide':
            $widths = array(
                '475' => 237,
                '768' => 384,
                '1920' => 950,
            );
            break;
        case 'desktopslide':
            $widths = array(
                '475' => 475,
                '768' => 768,
                '1920' => 1920,
            );
            break;
        case 'thumbnails':
            $widths = array(
                '475' => 237,
                '768' => 384,
                '1920' => 451,
                '2520' => 600,
            );
            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  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 546 posts ]  Go to page Previous  1 ... 33, 34, 35, 36, 37  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 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