0

I want to resize my picture so that its not wider than 500px. The aspect ration must be the same. I don't see any changes in my image (960x960).

This is my code:

public static function resizeImage($imagename) {
        list($width, $height) = getimagesize($imagename);
        if($width < 500)
            return $imagename;

        $ratio = $width / $height;
        $new_width = 500;
        $new_height = $new_width / $ratio;

        $image_p = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefromjpeg($imagename);
        imagecopyresampled ($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        return $imagename;
    }

This is the input of imagecopyresampled
Resource id #25|Resource id #27|0|0|0|0|500|500|960|960|

HansElsen
  • 1,639
  • 5
  • 27
  • 47

1 Answers1

0

Try instead of returning name returning cropped picture:

//replace 
return $imagename;

//with 

return imagejpeg($image_p, null, 100);
Yunalescar
  • 70
  • 7
  • Thats not the goal of this function. I dont need the image as output. The name is just for convenience in other functions – HansElsen Nov 21 '12 at 09:13
  • thats right. 'imagejpeg($image_p, null, 100);' creates an Image outof information stored in $image_p so it returns the actual data of your image (You might watch into : http://php.net/manual/de/function.imagecopyresampled.php - Sample) – Yunalescar Nov 21 '12 at 09:18