咱們作web系統的時候,每一個瀏覽器的tab這裏都會有一個圖標,這個圖標叫favicon圖標,favicon.ico文件放在系統的根目錄php
若是程序員沒有ICO製做工具,那麼要如何生成圖標呢?能夠用程序來實現生成啊!也能夠把圖標裁剪好,選擇在線生成,在線生成測試地址:www.vsoyo.com/ico程序員
下面和你們一塊兒探討下,php生成ICO圖標在線製做源碼,很簡單的原理,請看以下代碼web
下面是ICO圖標在線生成的主要代碼瀏覽器
<?php if(!function_exists("generate_favicon")){ function generate_favicon(){ // Create favicon. $postvars = array( "image" => trim($_FILES["image"]["name"]), "image_tmp" => $_FILES["image"]["tmp_name"], "image_size" => (int)$_FILES["image"]["size"], "image_dimensions" => (int)$_POST["image_dimensions"]); // Provide valid extensions and max file size $valid_exts = array("jpg","jpeg","gif","png"); $max_file_size = 179200; // 175kb $filenameParts = explode(".",$postvars["image"]); $ext = strtolower(end($filenameParts)); $directory = "./favicon/"; // Directory to save favicons. Include trailing slash. $rand = rand(1000,9999); $filename = $rand.$postvars["image"]; // Check not larger than max size. if($postvars["image_size"] <= $max_file_size){ // Check is valid extension. if(in_array($ext,$valid_exts)){ if($ext == "jpg" || $ext == "jpeg"){ $image = imagecreatefromjpeg($postvars["image_tmp"]); } else if($ext == "gif"){ $image = imagecreatefromgif($postvars["image_tmp"]); } else if($ext == "png"){ $image = imagecreatefrompng($postvars["image_tmp"]); } if($image){ list($width,$height) = getimagesize($postvars["image_tmp"]); $newwidth = $postvars["image_dimensions"]; $newheight = $postvars["image_dimensions"]; $tmp = imagecreatetruecolor($newwidth,$newheight); // Copy the image to one with the new width and height. imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height); // Create image file with 100% quality. if(is_dir($directory)){ if(is_writable($directory)){ imagejpeg($tmp,$directory.$filename,100) or die('Could not make image file'); if(file_exists($directory.$filename)){ // Image created, now rename it to its $ext_pos = strpos($rand.$postvars["image"],"." . $ext); $strip_ext = substr($rand.$postvars["image"],0,$ext_pos); // Rename image to .ico file rename($directory.$filename,$directory.$strip_ext.".ico"); return '<strong>圖標預覽:</strong><br/> <img src="'.$directory.$strip_ext.'.ico" border="0" title="Favicon 完畢預覽" style="padding: 4px 0px 4px 0px;background-color:#e0e0e0" /><br/> Favicon/ICO圖標製做完畢: <a href="'.$directory.$strip_ext.'.ico" target="_blank" name="右鍵另存爲下載!">點擊/右鍵另存下載,後重命名爲"favicon.ico"</a>'; } else { "File was not created."; } } else { return 'The directory: "'.$directory.'" is not writable.'; } } else { return 'The directory: "'.$directory.'" is not valid.'; } imagedestroy($image); imagedestroy($tmp); } else { return "Could not create image file."; } } else { return "圖標過大,不能超過175KB"; } } else { return "圖片格式只能是後面幾種 (jpg, jpeg, gif, png)."; } } } ?>
須要進一步技術交流的朋友 ,請加我微信吧:15889726201微信