/** * @param $path path to non-web-image file * @param $format the format to convert to * @param $timeout seconds to wait to for conversion default 20 * * @return the path of the converted file, or false if conversion fails */ function convert_to_web_image($path, $format, $timeout = 20) { $sharpen = true; $new_path = change_extension($path, $format); $args = array('convert', '-flatten'); if ($sharpen) { $args = array_merge($args, array('-sharpen', '1')); } $args[] = escapeshellarg($path); $args[] = escapeshellarg($new_path); $output = array(); $command = implode(' ', $args); $exit_status = -1; exec_with_timeout($command, $timeout, $output); if (file_exists($new_path)) { return $new_path; } else { // We know some psds don't work -- don't actually report these errors if (get_extension($path) != 'psd') { trigger_error('convert_to_web_image failed: Attempted this: "' . $command . '"'); } return false; } }
/* If uploading an image, but provide a non-web-friendly type (i.e. pdf), convert it and update all of its corresponding paths. Note 1=>'gif',2=>'jpg',3=>'png' */ $web_acceptable_types = array(1,2,3); if( (!$img_info || !in_array($file_type, $web_acceptable_types) ) && (!empty($constraints['convert_to_image'])) && $constraints['convert_to_image'] ) { // from image_funcs $convert_to = 'png'; if( $temp_path = convert_to_image($temp_path, $convert_to) ) { $img_info = @getimagesize($temp_path); $temp_uri = change_extension( $temp_uri, $convert_to ); $filename = change_extension( $filename, $convert_to ); $filesize = filesize($temp_path); } else { final_response(501, 'Unable to convert the uploaded file to a web-friendly image'); } } if ($img_info) { // fix a permission idiosyncrasy so the permissions are consistent @copy($temp_path, $temp_path.".tmp"); @rename($temp_path.".tmp", $temp_path); list($orig_width, $orig_height) = $img_info; list($width, $height) = $img_info;