bergkid
bergkid

Reputation: 1

php resizing image then cut it in pieces and show it, doesnt work

I'm making a website for fun but I cant seem to figure out why this piece of code wont work:

// error control
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
// loading in the image.
$img = imagecreatefromjpeg(".\image\ons\Ons-foto\mayor.jpg");

if($img != null){
    $width = imagesx($img);
    $heigth = imagesy($img);
    $calw = 134*7;
    $calh = 122*7;
    $newimg = null;
    if($width != null && $heigth != null){

        // a check to see if the image is the right size

        if($width > $calw || $width < $calh || $heigth < $calh || $heigth > $calh)
        {
        // here i save the resized image into the variable $newimg

        imagecopyresized($newimg, $img, 0,0,0,0, $calw, $calh, $width, $heigth);
        } else {
        $newimg = $img;
        }
        $tempimg = null;
        // a nested loop to automatically cut the image into pieces

        for($w = 0; $w < $calw; $w+134){
            for($h = 0; $h < $calh; $h+122){
                // taking a piece of the image and save it into $tempimg

                imagecopy($tempimg, $newimg, 0, 0, $w, $h, 134, 122);

                // showing the image on screen
                echo '<div class="blokken" style="margin: 1px;">';
                imagejpeg($tempimg);             
                echo '</div>';
            }
        }
    }
}

what I am trying to do is:

  1. load in the image and resize it to the right size.
  2. cut it in pieces
  3. show it piece by piece with a little bit of space between them (i do that in a .css file hence the div tag)

these are the errors that i get:

Warning: imagecopyresized(): supplied argument is not a valid Image resource in root\ons.php on line 52

Warning: imagecopy(): supplied argument is not a valid Image resource in root\ons.php on line 63

Warning: imagejpeg(): supplied argument is not a valid Image resource in root\ons.php on line 67

Warning: imagecopy(): supplied argument is not a valid Image resource in root\ons.php on line 63

I thought that these functions might want a path to the image so I have also tried to make the image that was resized and the ones that were cut to save in this map .\image\ons\Ons-foto\ but that didn't work either. it didn't save the images nor did it load them back in to display them on the screen.

What it also could be is that imagecreatefromjpeg() doesn't output what I think it outputs and saves in $img but fopen() didn't work either and gave the same error so I cant figure it out

Could someone take a look at this and tell me why it doesn't work? to those who do, thank you

Upvotes: 0

Views: 586

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

You need to create $newimg and $tempimg before you can copy into them. All the other errors are cascading from this problem.

Upvotes: 1

Related Questions