2

after receiving the image from the client , I would like to resize it and then store it . I am using this function:

function PIPHP_ImageResize($image, $w, $h)
{
         $oldw = imagesx($image);
         $oldh = imagesy($image);
         $temp = imagecreatetruecolor($w, $h);
         imagecopyresampled($temp, $image, 0, 0, 0, 0,
            $w, $h, $oldw, $oldh);
         return $temp;
}
$image = PIPHP_ImageResize($_FILES['file']['tmp_name'],10,10);
move_uploaded_file($image, $newname);

unfortunately I got these warning:

  1. Warning: imagesx() expects parameter 1 to be resource, string given...
  2. Warning: imagesy() expects parameter 1 to be resource, string given ...
  3. Warning: imagecopyresampled() expects parameter 2 to be resource, string given ...
  4. Warning: move_uploaded_file() expects parameter 1 to be string, resource given ...

How could I fix the problem !!

3
  • You may want to read the documentation of how these functions work, what they expect in parameters; maybe even check the code examples on those docs pages! Commented Jun 15, 2014 at 7:14
  • @Maerlyn , I know that I don't give the function the correct parameter, you can see that from the warning( expects parameter 1 to be resource ) but I don't understand what the resource is ? Commented Jun 15, 2014 at 7:31
  • A resource is a variable instantiated with the correct image creator function. Commented Jun 15, 2014 at 7:36

1 Answer 1

7

imagesx expect an image resource as first parameter. You have to create one using the appropriate function, imagecreatefromjpeg or imagecreatefrompng for example.

function PIPHP_ImageResize($image, $w, $h)
{
    $oldw = imagesx($image);
    $oldh = imagesy($image);
    $temp = imagecreatetruecolor($w, $h);
    imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $oldw, $oldh);
    return $temp;
}

if (move_uploaded_file($_FILES['file']['tmp_name'], $newname)) {
    $uploadedImage = imagecreatefromjpeg($newname);
    if (!$uploadedImage) {
        throw new Exception('The uploaded file is corrupted (or wrong format)');
    } else {
        $resizedImage = PIPHP_ImageResize($uploadedImage,10,10);
        // save your image on disk
        if (!imagejpeg ($resizedImage, "new/filename/path")) {
              throw new Exception('failed to save resized image');
        }
    }
} else {
    throw new Exception('failed Upload');
}

I've added a minimal and insufficient error handling, you should check, for example, the format of the uploaded file and use the appropriate image creator function, or check if the uploaded file is an image.

Addendum

You can determine the type of the presumed uploaded image using getimagesize function.

getimagesize return an array. If the image type is supported the array[2] value return the type of the image. if the file is not an image, or its format is not supported, the array[0] value is zero. Once you know the file format of the image you can use one of the imagecreatefromxxx functions to create the appropriate image.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using many image file ... png,JPEG,JPG,x-png... is there an imgaeCreate function that suits to all the kind of images...thank you
@user3648409 I've just added some hints on determining the file format in the answer
The PHP docs recommends using FileInfo for file format detection: php.net/manual/en/book.fileinfo.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.