I’m in the process of creating a plugin for WordPress that requires ImageMagick. My hosting service has assured me that ImageMagick is available on their server, yet my code does not seem to produce the desired results.
Here’s the code I’m working with:
<?php
/*
Plugin Name: Image Alterer
*/
add_action('admin_menu', 'configure_menu');
function configure_menu() {
add_submenu_page('options-general.php', 'Image Modifier', 'Image Modifier', 'manage_options', 'image-alterer-settings', 'show_admin_page');
}
function show_admin_page() {
$image_source = site_url('/wp-content/plugins/image-alterer/example_image.png');
if($image_source) {
echo 'Image source retrieved';
} else {
echo 'Image not found';
}
$image_output = site_url('/wp-content/plugins/image-alterer/modified_image.png');
exec("convert $image_source $image_output");
exec("/usr/bin/convert $image_source $image_output");
}
?>
Unfortunately, no modified image is being generated in my plugin’s folder, and there are no error alerts either. The exec commands execute smoothly, but the expected modified image remains absent. What could be the issue? Am I neglecting a crucial part of my code?
Others covered the URL path issue, but also check your server’s PHP config. Most shared hosts disable exec() by default even with ImageMagick installed. Run phpinfo() and see if exec is in the disabled_functions list. If it’s blocked, use PHP’s Imagick extension instead - same functionality but works through PHP’s native interface rather than system calls. Some servers also need the full path to the convert binary, so try /usr/local/bin/convert instead of just convert. The silence you’re getting is typical when exec fails due to permissions or disabled functions.
Your problem is with site_url() - it generates HTTP URLs, whereas ImageMagick’s convert command requires actual file system paths. The exec function won’t work with HTTP URLs. Switch site_url() to plugin_dir_path(FILE) to retrieve the correct directory path of your plugin. For instance, set $plugin_path = plugin_dir_path(FILE); and then define $image_source and $image_output accordingly. It’s also advisable to implement error handling to troubleshoot issues: use exec(“convert $image_source $image_output”, $output, $return_var); and inspect $return_var and $output for debugging information.
ya, site_url() returns a url, not a path for exec. use WP_PLUGIN_DIR to get your plugin folder path! also, some hosts block exec calls for safety. try running which convert in terminal to ensure ImageMagick is set up right. good luck!