Page 6 of 37

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

Posted: Tue Mar 07, 2017 10:48 am
by simonmlewis
sorry. what should I alter it to?

So I alter the composer.json file in my project directory... to what ??
And then go back to cmd, CD to the project root and run composer install.

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

Posted: Tue Mar 07, 2017 10:51 am
by Celauran
simonmlewis wrote:sorry. what should I alter it to?

Code: Select all

{
    "require": {
        "imagine/imagine": "^0.6.3"
    }
}

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

Posted: Tue Mar 07, 2017 10:54 am
by simonmlewis
issue.

Still get this error too:

Parse error: syntax error, unexpected 'require' (T_REQUIRE) in C:\xampp\phpMyAdmin\site-wide\includes\a_home.inc on line 114

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

Posted: Tue Mar 07, 2017 10:55 am
by simonmlewis
Maybe this is wrong??

Code: Select all

if ($usertype == "admin")
{
composer require imagine/imagine;
$todaydate = date('Y-m-d');

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

Posted: Tue Mar 07, 2017 10:57 am
by Celauran
  • Remove your composer.lock.
  • Remove your vendor directory.
  • Use the composer.json I provided above.
  • Run composer install.
  • Require vendor/autoload.php in your script.

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

Posted: Tue Mar 07, 2017 11:01 am
by simonmlewis
Ok that seems to have gone well.

[text]if ($usertype == "admin")
{
composer require imagine/imagine;
$todaydate = date('Y-m-d');[/text]
Is this correct?
I still get this on the main page:
[text]
Parse error: syntax error, unexpected 'require' (T_REQUIRE) in C:\xampp\phpMyAdmin\site-wide\includes\a_home.inc on line 114[/text]

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

Posted: Tue Mar 07, 2017 11:03 am
by Celauran
simonmlewis wrote:Ok that seems to have gone well.

Code: Select all

if ($usertype == "admin")
{
composer require imagine/imagine;
$todaydate = date('Y-m-d');
Is this correct?
No. That composer line is something you'd type at the command line. You want to be requiring the vendor/autoload.php file

Code: Select all

require_once dirname(__DIR__) . '/vendor/autoload.php';
or similar.

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

Posted: Tue Mar 07, 2017 11:09 am
by simonmlewis
Ok the good news is there are no errorsr at all.
It uploads the core image.

The bad news is, it is not uploading anything else at all. So when I search for the uploaded image's first few characters, it shows only that one file.

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

Posted: Tue Mar 07, 2017 11:12 am
by Celauran
Does the target directory exist? Is it writable? Are there any errors in your error log? What does your code look like now?

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

Posted: Tue Mar 07, 2017 11:14 am
by simonmlewis
Actually no! It says it's undefined variable.

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

Posted: Tue Mar 07, 2017 11:17 am
by Celauran
Can you please be more specific? I'm trying to help. I asked four questions but got one answer, and it's not clear which question that was an answer to.

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

Posted: Tue Mar 07, 2017 11:19 am
by simonmlewis
Does the target directory exist?
If I echo $target_directory, it shows an error of undefined variable.

Is it writable?
If it is directing correctly to /images/pages, yes it would be as the core image goes in there successfully.

Are there any errors in your error log?
Not sure where the error log is in xampp.

What does your code look like now?

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['homeimage']) && $_FILES['homeimage']['error'] !== 0) {
    $target_directory = __DIR__ . '/images/pages';
    $pathinfo = pathinfo($_FILES['homeimage']['name']);
    $prefix = (rand() % 99999999);

    // Open the uploaded image with the Imagine library
    $image = $imagine->open($_FILES['homeimage']['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 "$target_directory";
    }
  }
}}
}

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

Posted: Tue Mar 07, 2017 11:24 am
by Celauran
See how you have $target initially set?

Code: Select all

    $target = $_SERVER['DOCUMENT_ROOT']."/images/pages/";
What if you saved that as $target_directory and then updated a few lines down to look like this

Code: Select all

    $target = $target_directory . $newname;
Which is to say:

Code: Select all

    $target_directory = $_SERVER['DOCUMENT_ROOT']."/images/pages/";
    $random = (rand()%99999999);
    $pic=($_FILES['homeimage']['name']);
    $newname="$random"."$pic";
    $target = $target_directory . $newname;

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

Posted: Tue Mar 07, 2017 11:31 am
by simonmlewis
No errors now, but still not uploading the other photos to the folder.

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

Posted: Tue Mar 07, 2017 11:33 am
by Celauran
Is it saving the original correctly? Have you checked that it's looking for the correct key in $_FILES?