Skip to content

Instantly share code, notes, and snippets.

@mrkmg
Last active February 28, 2017 02:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mrkmg/4568896 to your computer and use it in GitHub Desktop.
Save mrkmg/4568896 to your computer and use it in GitHub Desktop.
PHP Code snippet to extract the icon from an exe file on a Linux system as a png -- tested on Debian Wheezy
<?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
?>
@rwrz
Copy link

rwrz commented Jul 18, 2014

Cool feature! =)

Copy link

ghost commented Feb 28, 2017

(stare)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment