Image Resizing Script required - a better one...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

My god... I've got it.
That's it. It works live.
In the early stages of this, we had the prefix in there with a _. I think it still does that "on the fly". But it's not doing that on initial upload.
Is that because of a difference in the functions?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

simonmlewis wrote:My god... I've got it.
That's it. It works live.
In the early stages of this, we had the prefix in there with a _. I think it still does that "on the fly". But it's not doing that on initial upload.
Is that because of a difference in the functions?
I don't know off hand. Let's take a look. Everything should be calling the same function.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

I think it's this:

Code: Select all

$path_to_original = $target_directory . DIRECTORY_SEPARATOR . "{$prefix}{$pathinfo['basename']}";
My old version for banner upload had the _ in the code. This doesn't.

??

Code: Select all

$path_to_original = $target_directory . DIRECTORY_SEPARATOR . "{$prefix}."_".{$pathinfo['basename']}";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

Should be easy enough to fix, then. Just make sure that everything goes through that one function so you're only needing to make the changes in one place.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

The great news is it works on the live server - first time ever!
So I will tweak that _ section, and try it on the other images.
I should be able to test the on-the-fly usage later when we have the core images for categories.

I think I need to check that part of the code to look for productimages/small.
Good little test for me!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

It's nearly there.
It's the Replace Banner code that is failing. And the upload image code.

It's because I am trying to get the _ in there, I think it is missing it in areas.

Code: Select all

if (!empty($_FILES) && isset($_FILES['homeimage'])) {
    $saved_filename = resizeImage($_FILES['homeimage']['tmp_name'], $_FILES['homeimage']['name'], $resize);
}
This is where it feeds back the name, but there is no _ in there. So I assume this is the wrong way to do it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

Looks like the right way to do it. Check inside your resizeImage code for how it's setting the name.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

Found it.

return "{$prefix}_{$pathinfo['basename']}";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

I'm now trying to get this to work nicely on the Add Product script, without duplicating code.
Ideally, putting it all into the same function.

The issue here, is that the core photo is NOT resize, and is placed in /images/productphotos, while the thumbnails all go into /images/productphotos/small.

Not sure how to put both those strings into the functions file, so have done it outside for now. But I know you will think this is daft when we have a function that can handle it all.

As of now, this is not uploading the images anyway!

Code: Select all

elseif ($pic != NULL) 
{
    $target_directory = $_SERVER['DOCUMENT_ROOT']."/images/productphotos/";
    $random = time();
    $pic=($_FILES['photoprimary']['name']);
    $newname= $random . "_". $pic;
    $target = $target_directory . $newname;

$posted_description = (get_magic_quotes_gpc()) ? stripslashes($_POST['description']) : $_POST['description'];
$description=mysql_real_escape_string($posted_description);

$posted_title = (get_magic_quotes_gpc()) ? stripslashes($title) : $title;
$title=mysql_real_escape_string($posted_title);

mysql_query("INSERT INTO products
(....) VALUES 
(....)");

require_once dirname(__DIR__) . '/vendor/autoload.php';
$imagine = new Imagine\Gd\Imagine();
    // An array of widths, keyed by target screen size
$widths = array(
    '475' => 237,
    '768' => 384,
    '1920' => 451,
    '2520' => 600,
);
return widths();

// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['photoprimary'])) {

    // Resize
    foreach ($widths as $key => $width) {
    $pathinfo = pathinfo($_FILES['photoprimary']['name']);

    // Open the uploaded image with the Imagine library
    $image = $imagine->open($_FILES['photoprimary']['tmp_name']);

    // Get image size
    $box = $image->getSize();
        $ratio = $width / $box->getWidth();
        $scaled_box = $box->scale($ratio);
        
        $new_filename = "{$random}_{$pathinfo['filename']}_{$key}.{$pathinfo['extension']}";
        $image->resize($scaled_box)->save($target_directory . '/small/' . $new_filename, array('jpeg_quality' => 95));
    }
}


$newid = mysql_insert_id();
    $result = mysql_query ("SELECT title, catname, subname, id FROM products WHERE id = '$newid'");
      while ($row = mysql_fetch_object($result))
      {
 $title = "$row->title"; 
 $findtitle ="/ /"; 
 $replacetitle ="-"; 
 $titlereplace = preg_replace ($findtitle, $replacetitle, $title); 
 
 $categ = "$row->catname"; 
 $findcateg ="/ /"; 
 $replacecateg ="-"; 
 $categreplace = preg_replace ($findcateg, $replacecateg, $categ); 

 $subcateg = "$row->subname"; 
 $findsubcateg ="/ /"; 
 $replacesubcateg ="-"; 
 $subcategreplace = preg_replace ($findsubcateg, $replacesubcateg, $subcateg); 
            
echo "<meta http-equiv='Refresh' content='0 ;URL=/product/$categreplace/$subcategreplace/$row->id/$titlereplace'>";
      }
     mysql_free_result($result);
}
Also, the URL at the end is long winded in the calculation, but that easily sorted.
Note there is a fourth size for the thumbnails here too. This is because the original thumb nails were 600. so I want to include that bigger screens.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

First thing I notice is you've still got

Code: Select all

return $widths
. Return statements should only be used inside functions.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

Moving on, how is the save location determined? Can you see a way to extract that to a function call also, similar to what we've done with getWidths?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

Return removed.
So to do this inside the function, I need to tell the function this is a 'thumbnail' image being resized.
But I also need to tell it of TWO storing locations, and which to use where.

Ideally they would all be in the same folder, but on this site, they are not. So I need to tell it which goes where.

mmmmmmmmmmmm.................
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Resizing Script required - a better one...

Post by Celauran »

simonmlewis wrote:Return removed.
So to do this inside the function, I need to tell the function this is a 'thumbnail' image being resized.
But I also need to tell it of TWO storing locations, and which to use where.

Ideally they would all be in the same folder, but on this site, they are not. So I need to tell it which goes where.

mmmmmmmmmmmm.................
So if you were to write a function to return save directories, and if that function returned a directory for full sized images and a directory for thumbnails...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing Script required - a better one...

Post by simonmlewis »

I think the function would need to have two variables - one for each directory.
But I wouldn't quite know where to start on "beginning" a new function.

It needs to be called from within resizeImage. So it has to be above that function? (tho maybe it doesn't matter where it is.

Code: Select all

function saveProductPhotos
{
$root_directory = dirname(__DIR__);
    $images_directory = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . "productphotos";
$images_path = $root_director . $images_directory;

    $thumbnails_directory = DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR . 'small';
$thumbnails_path = $root_director . $thumbnails_directory;
}
This looks more like a standard PHP query than a function tho. But it puts them into their various directories. Not sure though how I tell the resizeimage function what to do with this. Bearing in mine the image that goes into the /productphotos folder (not small), is NOT resized.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply