-
-
Save ScrambledBits/4584359 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Code snippet to extract the icon from an exe file on a Linux system -- tested on Debian Wheezy | |
// Install icoutils on your system e.g. sudo apt-get install icoutils | |
// Web process must have write privileges to /tmp | |
///** Config **/// | |
$input_file = '/path/to/program.exe'; | |
$dest_file = '/path/to/image.png'; | |
$dest_size = 64; // Will get the best quality icon at or below this size. | |
///** End Config **/// | |
$temp_dir_base = '/tmp/iconextract/'; | |
$tmp_offset = 0; | |
while(file_exists($temp_dir_base.$tmp_offset.'/')){ //Ensure working directory is empty | |
$tmp_offset += 1; | |
} | |
$tmp_dir = $temp_dir_base.$tmp_offset.'/'; | |
mkdir($tmp_dir,0777,true); //Create a temporary folder to work in | |
exec('wrestool -x '.str_replace(' ','\\',$input_file).' -o '.$tmp_dir); //Extract ico files | |
$icon_files = glob($tmp_dir.'*.ico'); //Find all ico files | |
$max = 0; | |
$file = 0; | |
$index = 0; | |
foreach($icon_files as $file_offset=>$ico){ //loop each match | |
exec('icotool -l '.$ico.' --icon',$list); //get each icon inside the file | |
foreach($list as $i){ //Loop through each icon | |
preg_match_all('/--index=(?P<index>\d+) --width=(?P<width>\d+) --height=(?P<height>\d+)/',$i,$a); | |
if($a['width'][0] > $max and $a['width'][0] <= $dest_size){ | |
$max = $a['width'][0]; | |
$index = $a['index'][0]; | |
$file = $file_offset; | |
} | |
} | |
} | |
if($max > 0){ //If we found one, extract it to the destination | |
exec('icotool -x '.$icon_files[$file].' -i '.$index.' -o '.str_replace(' ','\\',$dest_file)); | |
} | |
else{ | |
exec('rm '.$tmp_dir.' -r'); //Clean out tmp files | |
$error = "Could not find a sutiable icon file"; | |
throw new Exception($error); | |
} | |
exec('rm /tmp/iconextract -r'); //Clean out tmp files | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment