|
Кусок кода - должен получать с сайта иконку по HTTP/Request, определять её тип, менять размер и сохранять. Всё осложняется тем, что тип файла надо опредеять по его содержимому. Тут используется finfo, но этот magic на хосте не стоит, нужно изворачиваться как-то иначе.getimagesize не врубается в ICO, mime-content-type почему-то возвращает plain\text.Кто поможет ? За рабочий код - с меня 10$ =)Просто код function getImageMime($data) { $fi = new finfo(FILEINFO_MIME | FILEINFO_PRESERVE_ATIME | FILEINFO_SYMLINK | FILEINFO_DEVICES | FILEINFO_COMPRESS); return $fi->buffer($data); }Просто код function httpRequestIcon($url) { _debug('Fetching icon: '.$url); require_once('HTTP/Request.php'); $req =& new HTTP_Request($url, array('allowRedirects' => true)); $req->addHeader('User-Agent', 'Blvogosphere/1.0 Beta (http://www.gate.lv)'); //$req->addHeader('User-Agent', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.11) Gecko/20071204 Firefox/2.0.0.11'); //$req->addHeader('Accept', 'image/png,*/*;q=0.5'); $response = $req->sendRequest(); if (PEAR::isError($response)) { _debug('Fetching icon failed: got error'); return false; } $code = $req->getResponseCode(); if ($code !== 200) { _debug('Fetching icon failed: got response code='.$code); return false; } $response = $req->getResponseBody(); $mime_type = getImageMime($response); if (! in_array($mime_type, array('application/octet-stream', 'image/gif', 'image/png', 'image/bmp', 'image/jpeg'))) { _debug("Unsupported icon mime type: ".$mime_type); return false; } _debug('Icon MIME:'.$mime_type); return $response; }Просто код function getIconImage($icon_data, $icon_url, $icon_filename) { $tmp_filename = tempnam(TMPDIR, 'ico'); file_put_contents($tmp_filename, $icon_data); $mime_type = getImageMime($icon_data); if (file_exists(ICONS_FOLDER.'/'.$icon_filename)) unlink(ICONS_FOLDER.'/'.$icon_filename); if (in_array($mime_type, array('image/gif', 'image/png', 'image/jpeg', 'image/bmp'))) { $im_new = imagecreatetruecolor(16, 16); $white = imagecolorallocate($im_new, 255, 255, 255); imagefill($im_new, 0, 0, $white); if (in_array($mime_type, array('image/gif', 'image/png', 'image/jpeg'))) { // GIF or smth $im_icon = imagecreatefromstring($icon_data); } else { // BMP if (! defined('PHPTHUMB_LOADED')) { define('PHPTHUMB_LOADED', true); require(LIB.'/phpthumb/phpthumb.functions.php'); require(LIB.'/phpthumb/phpthumb.bmp.php'); } $thumb = new phpthumb_bmp(); $im_icon = $thumb->phpthumb_bmp2gd($icon_data); } $w = imagesx($im_icon); $h = imagesy($im_icon); if (($w == 16) && ($h == 16)) imagecopy($im_new, $im_icon, 0, 0, 0, 0, 16, 16); else imagecopyresampled($im_new, $im_icon, 0, 0, 0, 0, 16, 16, $w, $h); } else if ($mime_type == 'application/octet-stream') { if (! defined('FLOICON_LOADED')) { define('FLOICON_LOADED', true); require(LIB.'/floicon/floIcon.php'); } $ico = new floIcon(); $ico->readICO($tmp_filename); $im_icon = $ico->getBestImage(16, 16); $im_new = imagecreatetruecolor(16, 16); $white = imagecolorallocate($im_new, 255, 255, 255); imagefill($im_new, 0, 0, $white); imagecopy($im_new, $im_icon, 0, 0, 0, 0, 16, 16); } else { unlink($tmp_filename); return false; } unlink($tmp_filename); imagepng($im_new, ICONS_FOLDER.'/'.$icon_filename); return true; }
|