How does the call_user_func() function play a role in the provided PHP script for resizing images to thumbnails?
The call_user_func() function is used in the provided PHP script to dynamically call a function that resizes images to thumbnails based on the specified image type. This function plays a crucial role in allowing the script to call the appropriate image resizing function without hardcoding each function call. By using call_user_func(), the script gains flexibility and scalability in handling different image types for resizing.
function resize_image($image_path, $image_type) {
$function_name = 'resize_' . $image_type;
if(function_exists($function_name)) {
call_user_func($function_name, $image_path);
} else {
echo 'Error: Image type not supported.';
}
}
function resize_jpg($image_path) {
// Resize JPG image to thumbnail
echo 'Resizing JPG image to thumbnail...';
}
function resize_png($image_path) {
// Resize PNG image to thumbnail
echo 'Resizing PNG image to thumbnail...';
}
$image_path = 'image.jpg';
$image_type = 'jpg';
resize_image($image_path, $image_type);