// Load the image from file $img = Image::make('path/to/image.jpg'); // Determine new width based on aspect ratio $width = 500; $height = intval($img->height() * ($width / $img->width())); // Resize the image and save to a new file $img->resize($width, $height)->save('path/to/new/image.jpg');
// Load the image from file $img = imagecreatefromjpeg('path/to/image.jpg'); // Determine the new dimensions based on aspect ratio $max_width = 500; $max_height = 500; $width = imagesx($img); $height = imagesy($img); // If the image is larger than the maximum size, resize it if ($width > $max_width || $height > $max_height) { $ratio = min($max_width/$width, $max_height/$height); $new_width = intval($width * $ratio); $new_height = intval($height * $ratio); $tmp_img = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagedestroy($img); $img = $tmp_img; } // Save the image to a new file imagejpeg($img, 'path/to/new/image.jpg'); imagedestroy($img);Overall, it's important to choose the appropriate library for your needs and make sure to handle errors or exceptions properly.