Page 2 of 37

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

Posted: Tue Mar 07, 2017 8:01 am
by simonmlewis
So that all runs after my initial upload is run?

Code: Select all

// ADD ALL BANNERS (SQUARE AND DOUBLE)
if ($update == "addbanner") 
{
  if (isset($layertype))
  {
    $result = mysql_query ("SELECT id FROM products WHERE romancode = '$searchurl'");
    $num_result = mysql_num_rows($result);
    if ($num_result > 1)
    {
    $disableupload = "yes";
    echo "<script>
  window.location.replace('/a_home&status=duplicatecode')
  </script>";
    }
  }
  
  if (!isset($disableupload))
  {
    if ($stockbanner == "yes")
    {
      if ($searchurl != "")
      {
      mysql_query("INSERT INTO homepage (url, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$searchurl', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
      }
      elseif ($searchurl == "")
      {
      mysql_query("INSERT INTO homepage (url, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$url', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
      }
      echo "<script>
  window.location.replace('/a_home')
  </script>";
    }
    else
    {
    $target = $_SERVER['DOCUMENT_ROOT']."/images/pages/";
    $random = (rand()%99999999);
    $pic=($_FILES['homeimage']['name']);
    $newname="$random"."$pic";
    $target = $target . $newname;
    if ($searchurl != "")
  {
  mysql_query("INSERT INTO homepage(url, image, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$searchurl', '$newname', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
  }
  elseif ($searchurl == "")
  {
  mysql_query("INSERT INTO homepage(url, image, section, freetext, priority, content, homepromocolor, homepromotextcolor, stockbanner) VALUES ('$url', '$newname', '$section', '$freetext', '$priority', '$content', '$homepromocolor', '$homepromotextcolor', '$stockbanner')");
  }
  
  require_once dirname(__DIR__) . '/vendor/autoload.php';

$imagine = new Imagine\Gd\Imagine();

// An array of widths, keyed by target screen size
$widths = [
    '475' => 237,
    '768' => 384,
    '1920' => 451,
];

// If we have an uploaded image without errors
if (!empty($_FILES) && isset($_FILES['image']) && $_FILES['image']['error'] !== 0) {
    $target_directory = __DIR__ . '/images/pages';
    $pathinfo = pathinfo($_FILES['image']['name']);
    $prefix = (rand() % 99999999);

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

    // Save the original
    $image->save($target_directory . '/' . $pathinfo['basename']);

    // Get image size
    $box = $image->getSize();

    // Resize
    foreach ($widths as $key => $width) {
        $ratio = $width / $box->getWidth();
        $scaled_box = $box->scale($ratio);
        $new_filename = "{$prefix}_{$pathinfo['filename']}{$key}.{$pathinfo['extension']}";
        $image->resize($scaled_box)->save($target_directory . '/' . $new_filename);
    }
}

  //Writes the photo to the server
  if(move_uploaded_file($_FILES['homeimage']['tmp_name'], $target))
    {
echo "<script>
  window.location.replace('/a_home')
  </script>";
    }
  }
}}
}
I'd need to pass over the $resize variable to know what sizes are being asked for but still... ?

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

Posted: Tue Mar 07, 2017 8:10 am
by Celauran
More or less. $random and $prefix are the same, so you can remove one of those and update the references accordingly. My $target_directory reflects the directory path on my server, so you'll want to tweak that some. Otherwise it seems that it should work.

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

Posted: Tue Mar 07, 2017 8:11 am
by Celauran
There does appear to be an extraneous closing brace in there, though that may be simply the result of a copy/paste.

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

Posted: Tue Mar 07, 2017 8:14 am
by Celauran
It's also probably worth requiring the Composer autoloader much earlier on to be able to more easily leverage other third-party libraries, but that's not specific to the problem at hand, just a general observation.

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

Posted: Tue Mar 07, 2017 8:15 am
by simonmlewis
Good news. It's working, to a point.

It's missing the period before the fileextension in the name.
So one file is: 2824678231-LAUNCHERS-jbg2-768jpg

Also, how do I insert the result of my $resize variable into this:

Code: Select all

// An array of widths, keyed by target screen size
$widths = [
    '475' => 237,
    '768' => 384,
    '1920' => 451,
];
One way would be like this:

Code: Select all

if ($resize == "categories" || $resize == "products")
{
$widths = [
    '475' => 237,
    '768' => 384,
    '1920' => 451,
];
}
if ($resize == "wide")
{
$widths = [
    '475' => 237,
    '768' => 450,
    '1920' => 950,
];
}

Unless you know of a more efficient way?

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

Posted: Tue Mar 07, 2017 8:20 am
by Celauran
Wrap it in a function in case you need to add more later.

Something like this, though you'd also want to handle the default case.

Code: Select all

function getWidthsArray($resize_type)
{
    switch ($resize_type) {
        case 'categories':
        case 'products':
            return [
                '475' => 237,
                '768' => 384,
                '1920' => 451,
            ];
        case 'wide':
            return [
                '475' => 237,
                '768' => 450,
                '1920' => 950,
            ];
    }
}

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

Posted: Tue Mar 07, 2017 8:21 am
by simonmlewis
What about the file extension? It's missing it in the final results. I mean the .jpg, as it's just jpg.

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

Posted: Tue Mar 07, 2017 8:24 am
by Celauran
simonmlewis wrote:What about the file extension? It's missing it in the final results. I mean the .jpg, as it's just jpg.
Shouldn't be...

Code: Select all

$new_filename = "{$prefix}_{$pathinfo['filename']}{$key}.{$pathinfo['extension']}";
I forgot the hyphen before the width key, but the dot is clearly there.

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

Posted: Tue Mar 07, 2017 8:27 am
by simonmlewis
Yes, I can see it is there. I cannot fault it. But when I run it, it DOES upload three new files, but they are non-descrip, as they have 2342342_bannerjpg as their filename for example. Rather than 2342342_banner.jpg.

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

Posted: Tue Mar 07, 2017 8:29 am
by Celauran
Hmm. Paste your code again? I don't see any typos in the above but I can try running your code locally and see what's amiss.

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

Posted: Tue Mar 07, 2017 8:39 am
by simonmlewis
Ok this is a bit bizarre.

I thought I had done something wrong, as it was echoing the filenames on screen, and that missing dot was bizarre, considering i could see how your code works correctly.

So I started over. And now I get this error:

Warning: require_once(C:\xampp\phpMyAdmin\site-wide/vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\phpMyAdmin\site-wide\includes\a_home.inc on line 616

Fatal error: require_once(): Failed opening required 'C:\xampp\phpMyAdmin\site-wide/vendor/autoload.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\phpMyAdmin\site-wide\includes\a_home.inc on line 616

Line 616 is this:
require_once dirname(__DIR__) . '/vendor/autoload.php';

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

Posted: Tue Mar 07, 2017 8:41 am
by Celauran
Did you install composer? Did you require imagine? Is your autoloader in another location? I wrote the code based on my setup and some basic assumptions I had made.

What is your site's path? What is this file's path? Where is composer's vendor directory?

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

Posted: Tue Mar 07, 2017 8:43 am
by simonmlewis
I'm stumped. It did work! Maybe there was something in my code that had started working, without it, that interfered. But it was working, except for the full stop before the extension.
I don't know how to install Composer on my local machine, and darent touch it on the live server in case it brings a rather large site down!

Hence why I Am trying to do it all with simple local PHP code.

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

Posted: Tue Mar 07, 2017 8:46 am
by Celauran
I posted a link to the Composer documentation earlier. It's really quite simple and they have instructions for Windows, MacOS, and Linux. Moreover, leveraging composer packages is essential for modern PHP development. That said, I understand your apprehension about doing this on a live server. That's what development environments are for.

Windows installation instructions: https://getcomposer.org/doc/00-intro.md ... on-windows

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

Posted: Tue Mar 07, 2017 8:49 am
by simonmlewis
This is frustrating. I was nearly there. The files had uploaded, but missed the dots. I think I can even see how the dots were missing.
I am happy to try and install Composer on my windows machine. But adding it on the live server will be an EXTREMELY nervous situation for me.

Hence why I am trying to also see if I can tweak my own existing code to do it for me.