DevKB
Web Development Knowledge Base
HOME | TOP 20 | WRITE AN ARTICLE |
Sections :



RSS RSS Feed

You too, please publish your useful code snippets in any programming language :
write an article !


Valid HTML 4.0 Transitional

Plateforme d'envoi de gros fichiers en ligne
Dépannage site web
Blog infogérance
Hébergement e-mail

Olivier Ligny - - 11/03/2008 - vue 7159 fois

Extraire la miniature d'un fichier .AI (Adobe Illustrator) en PHP

Les fichiers .AI (format Adobe Illustrator) contiennent une miniature intégrée, pour pouvoir les prévisualiser sans que le logiciel aie besoin de faire un rendu complet.

Voici un script fait maison pour extraire cette miniature, et la récupérer dans une image, que l'on peut ensuite retravailler ou écrire sur le disque.

Pour info, la miniature fait toujours 128 pixels de largeur ou de hauteur maximum, et est en 256 couleurs.

<?php

$thumbnail = Extract_AI_thumbnail('maquette.ai');
imagejpeg($thumbnail, 'maquette.jpg', 85);


function Extract_AI_thumbnail($AIfile) {
   
    // Extract thumbnails from .AI files
    // input : path to file
    // output : GD image
    // author : Olivier Ligny, antares7364 'at' yahoo.fr, 2007-09-20
    // source : a post on adobe.illustrator.windows newsgroup

    $f = fopen($AIfile, 'rb');
    $data = fread($f, 100000);
    fclose($f);

    $start = strpos($data, '%AI7_Thumbnail:');
    $end   = strpos($data, '%%EndData');
    if(!$start || !$end) return false;
    //echo "TN: from $start to $endrn";
    //die(substr($data, $start, $end-$start));
    $tnData = substr($data, $start, $end-$start);
    //die(substr($tnData, 0, 10));
    $header = substr($tnData, strpos($tnData,':')+1, 100);
    $header = substr($header, 0, strpos($header,"r"));
    //die(str_replace("r", "rn", $header));
    $imageSize = explode(' ', trim($header));
    //echo "Image size : ".$imageSize[0].' * '.$imageSize[1]."rn"; //exit;
    if($imageSize[0] * $imageSize[1] == 0) return false; //die("Error: invalid thumbnail header : $headerrn");
    $imgData = substr($tnData, strpos($tnData,'Hex Bytes')+strlen('Hex Bytes'), strlen($tnData));
    $imgData = trim($imgData);
    $imgData = str_replace("r", "", $imgData);
    $imgData = str_replace("n", "", $imgData);
    $imgData = str_replace("%", "", $imgData);
    $imgData = str_replace("", "", $imgData);
    $len = strlen($imgData);
    $binData = pack("H" . $len, $imgData);
    $arr = Array();
    for($i=0; $i<strlen($binData); $i++) $arr[$i] = ord($binData[$i]);
    $byteArray = $arr;
    $gdimg = imagecreatetruecolor($imageSize[0], $imageSize[1]);
    $colors = Array();
    for($c=0; $c<256*3; $c+=3) {
        $colors[] = imagecolorallocate($gdimg, $byteArray[$c+0], $byteArray[$c+1], $byteArray[$c+2]);
    }
    $refs = Array();
    if($binData[256*3+0].$binData[256*3+1].$binData[256*3+2]=='RLE') {
        for($c=256*3; $c<count($byteArray); $c++) {
            $b = $byteArray[$c];
            if($b != 0xFD) $refs[] = $b;
            else {
                $c++;
                $b = $byteArray[$c];
                if($b == 0xFD) $refs[] = $b;
                else {
                    $n = $b;
                    $c++;
                    $b = $byteArray[$c];
                    for($i=0; $i<$n; $i++) $refs[] = $b;
                }
            }
        }
    } else {
        for($c=256*3; $c<count($byteArray); $c++) {
            $refs[] = $byteArray[$c];
        }
    }
    for($y=0; $y<$imageSize[1]; $y++) {
        for($x=0; $x<$imageSize[0]; $x++) {
            $n = $refs[$y*$imageSize[0] + $x];
            @imagesetpixel($gdimg, $x, $y, $colors[$n]);
        }
    }
    return $gdimg;
}


?>




Write a comment :
Your name :     E-mail (optional) :

AntiSpam : what animal is visible on this picture ? :

Nos partenaires : iPhone 8 Cases & Protection