if (!is_dir($img_path)) { mkdir($img_path, 0755, true); } $file_data = $img_path . 'data.png'; $dest_data = $img_path . 'data-fs8.png'; file_put_contents($file_data, $img_data); } //若传过来的是图片的id,那么从服务器获取 if ($pic_id) { $rela_img_path = 'data' . DIRECTORY_SEPARATOR . $pic_id . DIRECTORY_SEPARATOR; $img_path = $serverPath . $rela_img_path; $file_data = $img_path . 'data.png'; $dest_data = $img_path . 'data-fs8.png'; } $uncompSize = filesize($file_data); $compressfile = compress_png($file_data, $percentage); file_put_contents($dest_data, $compressfile); $compSize = filesize($dest_data); $return = $uncompSize . '|' . $compSize . '|' . $rela_img_path . 'data-fs8.png|' . $rela_img_path . 'data.png|' . $id; echo json_encode($return); } function compress_png($path_to_png_file, $max_quality = 90) { if (!file_exists($path_to_png_file)) { throw new Exception("File does not exist: {$path_to_png_file}"); } // guarantee that quality won't be worse than that. $min_quality = 50; // '-' makes it use stdout, required to save to $compressed_png_content variable // '<' makes it read from the given file path // escapeshellarg() makes this safe to use with any path
function output_file($file_item, $current_path, $type) { global $ignore_files, $template_exts, $total_reduce_size; if ($current_path !== '') { foreach ((array) $file_item['file'] as $file_name => $file_info) { if (false === strpos($current_path, 'views') && $file_name[0] === '_' || $file_name[0] == '.') { continue; } if (in_array($file_name, $ignore_files)) { continue; } $file_path = $current_path . '/' . $file_name; $result = true; $file_skiped = false; $ext = get_ext($file_name); if ('all' != $type && $ext != $type) { continue; } if ($ext == 'css') { if (strpos($file_name, 'min.') === 0) { $result = compress_css($file_path); } else { $file_skiped = true; } } elseif ($ext == 'js') { if (strpos($file_name, 'min.') === 0) { $result = compress_js($file_path); } else { $file_skiped = true; } } elseif ($ext == 'png') { compress_png($file_path); } elseif (in_array($ext, $template_exts)) { compress_template($file_path); } else { copy(ROOT_PATH . $file_path, OUTPUT_DIR . $file_path); } $file_size = 0; if ($file_skiped === false) { if (file_exists(OUTPUT_DIR . $file_path)) { $file_size = filesize(OUTPUT_DIR . $file_path); $total_reduce_size += $file_info['infor']['file_size'] - $file_size; if ($file_size <= 0) { $file_size = 'b style="color:#ff0000">' . $file_size . '</b>'; } else { $file_size = human_file_size($file_size); } } else { $result = false; } } if (false === $result) { echo '<p style="color:#ff0000;font-weight:bold">' . $file_path . ' Error</p>'; } else { if ($file_skiped === false) { echo $file_path . ' <b style="color:#1EBC16">OK</b> <span style="color:#888">(' . human_file_size($file_info['infor']['file_size']) . ' → ' . $file_size . ')</span><br />'; } else { echo '<span style="color:#aaa">' . $file_path . ' <b>Skiped</b></span><br />'; } } } } foreach ((array) $file_item['dir'] as $dir_name => $dir) { if ($dir_name[0] === '_' || $dir_name[0] == '.') { continue; } if (in_array($dir_name, $ignore_files)) { continue; } $dir_path = $current_path . '/' . $dir_name; echo '<h2>' . $dir_path . '</h2>'; make_dir($dir_path, OUTPUT_DIR); output_file($dir, $dir_path, $type); } }